added updating of last cached reporting period if new data has been read - reportable - Unnamed repository; edit this file 'description' to name the repository. (DIR) Log (DIR) Files (DIR) Refs (DIR) README --- (DIR) commit 415849fbf6a05c044df8465162a4218165f1d773 (DIR) parent ca91ca842c410fa1afea209b550ac8ed2d812e35 (HTM) Author: marcoow <marco.otte-witte@simplabs.com> Date: Fri, 12 Dec 2008 00:48:11 +0800 added updating of last cached reporting period if new data has been read Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com> Diffstat: M lib/kvlr/reports_as_sparkline/repo… | 6 ++++-- M spec/boot.rb | 2 +- M spec/other/report_cache_spec.rb | 14 +++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) --- (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_as_sparkline/report_cache.rb @@ -50,8 +50,10 @@ module Kvlr #:nodoc: result << [cached.reporting_period.date_time, cached.value] reporting_period = reporting_period.previous end while reporting_period != last_reporting_period_to_read - if !no_cache && cached = cached_data.detect { |cached| cached.reporting_period == last_reporting_period_to_read } && data = new_data.detect { |data| data[0] == last_reporting_period_to_read } - cached.update_attributes!(:value => data[1]) + unless no_cache + cached = cached_data.last || nil + data = (new_data.first && new_data.first[0] == last_reporting_period_to_read) ? new_data.first : nil + cached.update_attributes!(:value => data[1]) unless cached.nil? || data.nil? end result end (DIR) diff --git a/spec/boot.rb b/spec/boot.rb @@ -19,5 +19,5 @@ FileUtils.mkdir_p File.join(File.dirname(__FILE__), 'log') ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'log', 'spec.log')) databases = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml'))) -ActiveRecord::Base.establish_connection(databases['sqlite3']) +ActiveRecord::Base.establish_connection(databases['postgresql']) load(File.join(File.dirname(__FILE__), 'db', 'schema.rb')) (DIR) diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb @@ -168,7 +168,19 @@ describe Kvlr::ReportsAsSparkline::ReportCache do result.length.should == 10 end - #TODO: add specs for update of record in cache fpr last_reporting_period_to_read if present + it 'should update the last cached record if new data has been read for the last reporting period to read' do + Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(@last_reporting_period_to_read) + @cached.should_receive(:update_attributes!).once.with(:value => 1.0) + + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report) + end + + it 'should not update the last cached record if new data has been read for the last reporting period to read but no_cache is specified' do + Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(@last_reporting_period_to_read) + @cached.should_not_receive(:update_attributes!) + + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report, true) + end end