簡體   English   中英

什么是'分配分支條件大小太高'以及如何解決它?

[英]What is meant by 'Assignment Branch Condition Size too high' and how to fix it?

在我的Rails應用程序中,我使用Rubocop來檢查問題。 今天它給我一個這樣的錯誤: Assignment Branch Condition size for show is too high 這是我的代碼:

def show
  @category = Category.friendly.find(params[:id])
  @categories = Category.all
  @search = @category.products.approved.order(updated_at: :desc).ransack(params[:q])
  @products = @search.result.page(params[:page]).per(50)
  rate
end

這是什么意思,我該如何解決?

分配分支條件(ABC)大小是方法大小的度量。 它主要通過計算A ssignments, B ranches和C onditional語句的數量來確定。 (更多詳情..)

要降低ABC分數,您可以將其中一些分配移動到before_action調用中:

before_action :fetch_current_category, only: [:show,:edit,:update] 
before_action :fetch_categories, only: [:show,:edit,:update] 
before_action :fetch_search_results, only: [:show,:edit,:update] #or whatever

def show
  rate
end

private

def fetch_current_category
  @category = Category.friendly.find(params[:id])
end

def fetch_categories
  @categories = Category.all
end

def fetch_search_results
  @search = category.products.approved.order(updated_at: :desc).ransack(params[:q])
  @products = @search.result.page(params[:page]).per(50)
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM