'Assignment Branch Condition Size too high' 是什么意思,如何解决?

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、Branches 和 Conditional 语句的数量来确定的。 (more detail..)

要降低 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