From e31d4991df5313db737964fb3003d8398ad59cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 19 Oct 2016 13:08:32 +0200 Subject: [PATCH] Calculate key/values and report them in a single method This lowers the indirection and complexity. --- app/models/chargeback.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/chargeback.rb b/app/models/chargeback.rb index 44781dc008f..5e9020533aa 100644 --- a/app/models/chargeback.rb +++ b/app/models/chargeback.rb @@ -138,11 +138,7 @@ def calculate_costs(metric_rollup_records, rates, hours_in_interval) cost = r.cost(metric_value) * hours_in_interval end - reportable_metric_and_cost_fields(r.rate_name, r.group, metric_value, cost).each do |k, val| - next unless self.class.attribute_names.include?(k) - self[k] ||= 0 - self[k] += val - end + reportable_metric_and_cost_fields(r.rate_name, r.group, metric_value, cost) end end end @@ -162,7 +158,11 @@ def reportable_metric_and_cost_fields(rate_name, rate_group, metric, cost) [cost_key, cost_group_key, 'total_cost'].each { |col| col_hash[col] = cost } end - col_hash + col_hash.each do |k, val| + next unless self.class.attribute_names.include?(k) + self[k] ||= 0 + self[k] += val + end end private :reportable_metric_and_cost_fields