简体   繁体   中英

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. Today it gave me an error like this : 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. It is essentially determined by counting the number of A ssignments, B ranches, and C onditional statements. (more detail..)

To reduce ABC score, you could move some of those assignments into before_action calls:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM