简体   繁体   English

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

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

In my Rails app, I use Rubocop to check for problems. 在我的Rails应用程序中,我使用Rubocop来检查问题。 Today it gave me an error like this : Assignment Branch Condition size for show is too high . 今天它给我一个这样的错误: Assignment Branch Condition size for show is too high Here's my code : 这是我的代码:

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

What does this mean and how can I fix it? 这是什么意思,我该如何解决?

Assignment Branch Condition (ABC) size is a measurement of the size of a method. 分配分支条件(ABC)大小是方法大小的度量。 It is essentially determined by counting the number of A ssignments, B ranches, and C onditional statements. 它主要通过计算A ssignments, B ranches和C onditional语句的数量来确定。 (more detail..) (更多详情..)

To reduce ABC score, you could move some of those assignments into before_action calls: 要降低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.

相关问题 分配分支条件太高 - Assignment Branch Condition is too high rails查询对象中的“调用的分配分支条件大小太高” - "Assignment Branch Condition size for call is too high" in rails query object Rubocop:方法的分配分支条件大小太高。 我怎样才能减少方法? - Rubocop: Assignment Branch Condition size for method is too high. How can I reduce the method? Metrics/AbcSize:fill_arrays 的分配分支条件大小太高。 [&lt;9, 21, 0&gt; 22.85/17] - Metrics/AbcSize: Assignment Branch Condition size for fill_arrays is too high. [<9, 21, 0> 22.85/17] 重构代码以减少分配分支条件大小 - Refactor the code to reduce Assignment Branch Condition size 将Postgres连接池大小设置得太高会有什么风险? - What's the risk in setting the Postgres connection pool size too high? 这种方法如何超过Rubocop账户分支条件大小? - How is this method exceeding the Rubocop Account Branch Condition size? 辅助分支条件太高 robocop 进攻 - assigenment branching condition is too high robocop offense 如何解决Rails的批量分配问题? - How to fix the Rails mass assignment issue? 如果条件为true,如何分配条件(查询),如果条件为false,如何进行另一分配? - How can I do an assignment of the condition (a query) if is true, or do another assignment if the condition is false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM