代码
def set_default_limit
params[:limit] = params[:limit].to_i > 0 ? params[:limit] : AppSettings.api[:default_limit]
params[:limit] = AppSettings.api[:max_limit] if params[:limit].to_i > AppSettings.api[:max_limit]
end
警告错误
app/controllers/application_controller.rb:66:3: C: Assignment Branch Condition size for set_default_limit is too high. [18.22/15]
def set_default_limit
^^^
答案 0 :(得分:1)
您想要使用此方法:
params[:limit]
超过最大限制,请将其设置为最大限制params[:limit]
小于或等于0,请将其设置为默认限制代码:
def set_default_limit
api_settings = AppSettings.api
if params[:limit].to_i > api_settings[:max_limit]
params[:limit] = api_settings[:max_limit]
elsif params[:limit].to_i <= 0
params[:limit] = api_settings[:default_limit]
end
end
Rubocop结果:
$ cat test.rb
def set_default_limit
api_settings = AppSettings.api
if params[:limit].to_i > api_settings[:max_limit]
params[:limit] = api_settings[:max_limit]
elsif params[:limit].to_i <= 0
params[:limit] = api_settings[:default_limit]
end
end
$ rubocop test.rb
Inspecting 1 file
.
1 file inspected, no offenses detected