我的購買控制器中有一個簡單的方法,可以在應用強引數之前從引數哈希調整用戶表單輸入。
def sanitize_params
params[:purchase][:invoice] = nil if params[:purchase][:invoice] == ''
params[:purchase][:note] = nil if params[:purchase][:note] == ''
params[:purchase][:product_id] = nil if params[:purchase][:product_id].blank?
end
Rubocop 逮捕了Metrics/AbcSize… [<3, 19, 5> 19.87/17]. ABC 指標中的“分支”如何計算為 19?
我可以重構為:
def sanitize_params
unsanitized_purchase_params = params[:purchase]
params[:purchase][:invoice] = nil if unsanitized_purchase_params[:invoice] == ''
params[:purchase][:note] = nil if unsanitized_purchase_params[:note] == ''
params[:purchase][:product_id] = nil if unsanitized_purchase_params[:product_id].blank?
end
然后通過,但以犧牲可讀性和額外代碼為代價。為什么'Branch' 19 在原始代碼中?我應該關心嗎?(我的實際用例有一條額外的相似線,因此那里的指標更高)。有更好的方法嗎?謝謝丹尼爾
uj5u.com熱心網友回復:
如檔案所示:
分支——顯式轉發程式分支超出范圍——函式呼叫、類方法呼叫或新運算子
因此,ABC 中的“分支”被計算為“跳出當前方法的任何內容”。
在你描述的方法中,
def sanitize_params
params[:purchase][:invoice] = nil if params[:purchase][:invoice] == ''
params[:purchase][:note] = nil if params[:purchase][:note] == ''
params[:purchase][:product_id] = nil if params[:purchase][:product_id].blank?
end
這些是需要離開當前方法的 19 個呼叫:
01. params (on line 1, before the assignment)
02. params[:purchase] (on line 1, before the assignment)
03. params[:purchase][:invoice] (on line 1, before the assignment)
04. params (on line 1, after the assignment)
05. params[:purchase] (on line 1, after the assignment)
06. params[:purchase][:invoice] (on line 1, after the assignment)
07. params (on line 2, before the assignment)
08. params[:purchase] (on line 2, before the assignment)
09. params[:purchase][:note] (on line 2, before the assignment)
10. params (on line 2, after the assignment)
11. params[:purchase] (on line 2, after the assignment)
12. params[:purchase][:note] (on line 2, after the assignment)
13. params (on line 3, before the assignment)
14. params[:purchase] (on line 3, before the assignment)
15. params[:purchase][:product_id] (on line 3, before the assignment)
16. params (on line 3, after the assignment)
17. params[:purchase] (on line 3, after the assignment)
18. params[:purchase][:product_id] (on line 3, after the assignment)
19. params[:purchase][:product_id].blank? (on line 3, after the assignment)
重要的 ponts:每當您呼叫 時params,您都在訪問不同的方法,并且每次您訪問散列上的索引時,您都是在此散列上呼叫一個方法來訪問所需的索引。
關于你是否應該關心:ABC 大小是衡量代碼質量的標準,所以當然,只要它有意義,堅持它通常是一個很好的選擇。
但是有一些方法可以重構代碼以降低 ABC 復雜性。一個建議如下:
def sanitize_params
params.tap do |params|
params[:purchase][:invoice] = nil if params.dig(:purchase, :invoice) == ''
params[:purchase][:note] = nil if params.dig(:purchase, :note) == ''
params[:purchase][:product_id] = nil if params.dig(:purchase, :product_id).blank?
end
end
在這個例子中,Branch 的復雜性降低了很多,因為我們只呼叫了一次外部并應用了塊params內的所有更改。tap此外,通過使用dig引數上的方法,我們只呼叫一種方法來達到我們想要的深度。
例如,另一個想法是將這種方法分解為更具體的方法來清理每個屬性。
我希望這會有所幫助。
uj5u.com熱心網友回復:
至于現在 Rails 引數不實作#deep_transform_values!,但您可以執行以下操作:
def sanitize_params
%i[invoice note product_id].each do |param_name|
params[:purchase][param_name] = params[:purchase][param_name].presence
end
end
要么:
def sanitize_params
params[:purchase] = params[:purchase].transform_values(&:presence)
end
更新。您也可以考慮使用https://github.com/rmm5t/strip_attributes - 這個 gem 將在模型級別用 nils 替換空值
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/455277.html
上一篇:使用proxy_pass時如何重定向到index.html
下一篇:引導模式ajax在提交時發送兩次
