From 38fba91dbe26d93c9fc56f068e29c956f3750dd5 Mon Sep 17 00:00:00 2001 From: Nimish Date: Fri, 28 Jul 2017 22:41:02 +0530 Subject: [PATCH] Remove hard dependency between order of results and observations Some reports may have results being interleaved by date instead of categorywise by date, which causes current populator to skip them. Fixing it by replacing with a find and fill approach. --- app/reports/spree/report.rb | 2 +- app/reports/spree/report/date_slicer.rb | 6 +++--- app/reports/spree/report/timed_result.rb | 16 ++-------------- app/reports/spree/shipping_cost_report.rb | 4 ++-- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/app/reports/spree/report.rb b/app/reports/spree/report.rb index bcc4259..a06573a 100644 --- a/app/reports/spree/report.rb +++ b/app/reports/spree/report.rb @@ -65,7 +65,7 @@ def get_results query = query.order(active_record_sort) if sortable_attribute.present? query_sql = query.to_sql - r = ActiveRecord::Base.connection.exec_query(query_sql) + ActiveRecord::Base.connection.exec_query(query_sql) end def set_sortable_attributes(options, default_sortable_attribute) diff --git a/app/reports/spree/report/date_slicer.rb b/app/reports/spree/report/date_slicer.rb index d472b66..2cc946b 100644 --- a/app/reports/spree/report/date_slicer.rb +++ b/app/reports/spree/report/date_slicer.rb @@ -15,7 +15,7 @@ def self.slice_into(start_date, end_date, time_scale, klass) def self.slice_hours_into(start_date, end_date, klass) current_date = start_date slices = [] - while current_date < end_date + while current_date <= end_date slices << (0..23).collect do |hour| obj = klass.new obj.date = current_date @@ -30,7 +30,7 @@ def self.slice_hours_into(start_date, end_date, klass) def self.slice_days_into(start_date, end_date, klass) current_date = start_date slices = [] - while current_date < end_date + while current_date <= end_date obj = klass.new obj.date = current_date slices << obj @@ -42,7 +42,7 @@ def self.slice_days_into(start_date, end_date, klass) def self.slice_months_into(start_date, end_date, klass) current_date = start_date slices = [] - while current_date < end_date + while current_date <= end_date obj = klass.new obj.date = current_date slices << obj diff --git a/app/reports/spree/report/timed_result.rb b/app/reports/spree/report/timed_result.rb index a2fac3b..7bc0279 100644 --- a/app/reports/spree/report/timed_result.rb +++ b/app/reports/spree/report/timed_result.rb @@ -13,21 +13,9 @@ def build_empty_observations end def populate_observations - observation_iter = @observations.each - current_observation = @observations.present? ? observation_iter.next : nil @results.each do |result| - if current_observation.present? - begin - until current_observation.describes? result, time_scale - current_observation = observation_iter.next - end - - current_observation.populate(result) - current_observation = observation_iter.next - rescue StopIteration - break - end - end + matching_observation = @observations.find { |observation| observation.describes? result, time_scale } + matching_observation.populate result end end diff --git a/app/reports/spree/shipping_cost_report.rb b/app/reports/spree/shipping_cost_report.rb index 7097e90..2372246 100644 --- a/app/reports/spree/shipping_cost_report.rb +++ b/app/reports/spree/shipping_cost_report.rb @@ -26,7 +26,7 @@ class Observation < Spree::Report::TimedObservation observation_fields [:name, :shipping_charge, :revenue, :shipping_cost_percentage] def describes?(result, time_scale) - (name = result['name']) && super + (name == result['name']) && super end def shipping_cost_percentage @@ -50,7 +50,7 @@ def report_query 'shipping_charge', 'shipping_method_id', 'name' - ) + ).order('shipping_method_id', *time_scale_columns) end private def order_with_shipments