From 7b5eeddec18225307701e89773fc734315f7e4c9 Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Tue, 31 Mar 2015 08:56:40 +0200 Subject: [PATCH] use ActiveRecord to summarize cost entries --- lib/open_project/costs/attributes_helper.rb | 17 +---------------- .../hooks/work_packages_show_attributes.rb | 10 +++++----- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/lib/open_project/costs/attributes_helper.rb b/lib/open_project/costs/attributes_helper.rb index 48c4925022..2fd01c1e1e 100644 --- a/lib/open_project/costs/attributes_helper.rb +++ b/lib/open_project/costs/attributes_helper.rb @@ -46,22 +46,7 @@ module OpenProject::Costs end def compute_summarized_cost_entries - return {} if cost_entries.blank? || !user_allowed_to?(:view_cost_entries, :view_own_cost_entries) - - last_cost_type = "" - - cost_entries.sort_by(&:id).each_with_object({}) do |entry, hash| - if entry.cost_type == last_cost_type - hash[last_cost_type][:units] += entry.units - else - last_cost_type = entry.cost_type - - hash[last_cost_type] = {} - hash[last_cost_type][:units] = entry.units - hash[last_cost_type][:unit] = entry.cost_type.unit - hash[last_cost_type][:unit_plural] = entry.cost_type.unit_plural - end - end + cost_entries.calculate(:sum, :units, group: :cost_type) end def time_entries diff --git a/lib/open_project/costs/hooks/work_packages_show_attributes.rb b/lib/open_project/costs/hooks/work_packages_show_attributes.rb index 37329a4d0a..10d7332ad5 100644 --- a/lib/open_project/costs/hooks/work_packages_show_attributes.rb +++ b/lib/open_project/costs/hooks/work_packages_show_attributes.rb @@ -65,18 +65,18 @@ module OpenProject::Costs::Hooks def summarized_cost_entry_links(cost_entries, work_package, create_link=true) str_array = [] - cost_entries.each do |k, v| - txt = pluralize(v[:units], v[:unit], v[:unit_plural]) + cost_entries.each do |cost_type, units| + txt = pluralize(units, cost_type.unit, cost_type.unit_plural) if create_link # TODO why does this have project_id, work_package_id and cost_type_id params? str_array << link_to(txt, { :controller => '/costlog', :action => 'index', :project_id => work_package.project, :work_package_id => work_package, - :cost_type_id => k }, - { :title => k.name }) + :cost_type_id => cost_type }, + { :title => cost_type.name }) else - str_array << "#{txt}" + str_array << "#{txt}" end end str_array.join(", ").html_safe