Make complex method a bit more readable

This commit is contained in:
Pau Perez
2019-06-18 19:15:25 +02:00
parent 951787d456
commit 812d8cac4a

View File

@@ -18,11 +18,17 @@ module OpenFoodNetwork
def group_and_sort(rule, remaining_rules, items)
branch = {}
groups = items.group_by { |item| rule[:group_by].call(item) }
sorted_groups = groups.sort_by { |key, _value| rule[:sort_by].call(key) }
sorted_groups.each do |property, items_by_property|
branch[property] = build_tree(items_by_property, remaining_rules)
branch[property][:summary_row] = { items: items_by_property, columns: rule[:summary_columns] } unless rule[:summary_columns].nil? || is_leaf_node(branch[property])
unless rule[:summary_columns].nil? || is_leaf_node(branch[property])
branch[property][:summary_row] = { items: items_by_property, columns: rule[:summary_columns] }
end
end
branch
end