Ruby-on-rails “分配分支条件大小太高”是什么意思以及如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30932732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is meant by 'Assignment Branch Condition Size too high' and how to fix it?
提问by THpubs
In my Rails app, I use Rubocopto check for problems. Today it gave me an error like this : Assignment Branch Condition size for show is too high. Here's my code :
在我的 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
What does this mean and how can I fix it?
这是什么意思,我该如何解决?
采纳答案by chad_
Assignment Branch Condition (ABC) size is a measurement of the size of a method. It is essentially determined by counting the number of Assignments, Branches, and Conditional statements. (more detail..)
分配分支条件 (ABC) 大小是对方法大小的度量。它基本上是通过计算A任务、B牧场和C条件语句的数量来确定的。 (更多详情..)
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

