0
줄이는 방법 :리팩토링과 나는 다음과 같은 코드가 복잡
@model = "ford"
@attribute = "name"
def grouped
grouped = {}
@array.each do |r|
field = r.car.send(@model)
if field.is_a? ActiveRecord::Associations::CollectionProxy
field.each do |obj|
key = obj.send(@attribute)
grouped[key] = [] unless grouped.has_key?(key)
grouped[key].push(r)
end
else
key = field.send(@attribute)
grouped[key] = [] unless grouped.has_key?(key)
grouped[key].push(r)
end
end
grouped
end
그 결과입니다 :
{ford: [a, b, c]}
codeclimate은인지 적 복잡성을 가지고 있다고 말한다.
어떻게하면이 방법을 더 깨끗하게 리팩터링 할 수 있습니까?
작동 코드는 더 잘 맞는 https://codereview.stackexchange.com/ –
에 대한'어디 그것에서 array' 왔는가 @ 무엇입니까? 가능하다면 범위를 사용하고 ActiveRecord에서 일부 작업을 수행합니다. –
codereview.stackexchange.com – mmichael