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
これはどういう意味ですか?どうすれば修正できますか?
ベストアンサー1
割り当て分岐条件(ABC)サイズは、メソッドのサイズの測定値です。基本的には、あ課題、B牧場、そしてC条件文。(もっと詳しく..)
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