made ReportCache write a new cache entry for the last reporting period to read if there is no cached value for that yet - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 3ebb3b9e324bd9dde944cd37855be48c8801e9b6
 (DIR) parent 4aa4217ae76fc2852ede223de89beb5dbfe2ceff
 (HTM) Author: marcoow <marco.otte-witte@simplabs.com>
       Date:   Tue, 16 Dec 2008 03:04:10 +0800
       
       made ReportCache write a new cache entry for the last reporting period to read if there is no cached value for that yet
       
       Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Diffstat:
         M lib/kvlr/reports_as_sparkline/repo… |      30 +++++++++++++++++++-----------
       
       1 file changed, 19 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_as_sparkline/report_cache.rb
       @@ -36,28 +36,36 @@ module Kvlr #:nodoc:
                  reporting_period = ReportingPeriod.new(report.grouping)
                  while reporting_period != last_reporting_period_to_read
                    data = new_data.detect { |data| data[0] == reporting_period }
       -            cached = self.new(
       -              :model_name       => report.klass.to_s,
       -              :report_name      => report.name.to_s,
       -              :grouping         => report.grouping.identifier.to_s,
       -              :aggregation      => report.aggregation.to_s,
       -              :reporting_period => reporting_period.date_time,
       -              :value            => (data ? data[1] : 0.0)
       -            )
       +            cached = build_cached_data(report, reporting_period, data ? data[1] : 0.0)
                    cached.save! unless no_cache
                    result << [reporting_period.date_time, cached.value]
                    reporting_period = reporting_period.previous
                  end
                  data = (new_data.first && new_data.first[0] == last_reporting_period_to_read) ? new_data.first : nil
                  unless no_cache
       -            cached = cached_data.last || nil
       -            cached.update_attributes!(:value => data[1]) unless cached.nil? || data.nil?
       +            if data && cached = cached_data.last
       +              cached.update_attributes!(:value => data[1])
       +            else
       +              cached = build_cached_data(report, last_reporting_period_to_read, data ? data[1] : 0.0)
       +              cached.save!
       +              result << [last_reporting_period_to_read.date_time, cached.value]
       +            end
                  end
       -          result << [last_reporting_period_to_read.date_time, data ? data[1] : 0.0]
                  result += (cached_data.map { |cached| [cached.reporting_period, cached.value] }).reverse
                  result
                end
        
       +        def self.build_cached_data(report, reporting_period, value)
       +          self.new(
       +            :model_name       => report.klass.to_s,
       +            :report_name      => report.name.to_s,
       +            :grouping         => report.grouping.identifier.to_s,
       +            :aggregation      => report.aggregation.to_s,
       +            :reporting_period => reporting_period.date_time,
       +            :value            => value
       +          )
       +        end
       +
            end
        
          end