Merge :cacheable changes back in - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 833bedd80fe51828d065a33490979c39a69ce5a1
(DIR) parent e2a282013a4ad968a58806bb0fc8e3efbab5bd8d
(HTM) Author: HD Moore <hd_moore@rapid7.com>
Date: Mon, 8 Sep 2014 01:19:24 -0500
Merge :cacheable changes back in
Diffstat:
M lib/saulabs/reportable/report.rb | 2 +-
M lib/saulabs/reportable/report_cach… | 10 +++++++---
M spec/classes/report_spec.rb | 6 +++---
3 files changed, 11 insertions(+), 7 deletions(-)
---
(DIR) diff --git a/lib/saulabs/reportable/report.rb b/lib/saulabs/reportable/report.rb
@@ -156,7 +156,7 @@ module Saulabs
case context
when :initialize
options.each_key do |k|
- raise ArgumentError.new("Invalid option #{k}!") unless [:limit, :aggregation, :grouping, :distinct, :include, :date_column, :value_column, :conditions, :live_data, :end_date].include?(k)
+ raise ArgumentError.new("Invalid option #{k}!") unless [:limit, :cacheable, :aggregation, :grouping, :distinct, :include, :date_column, :value_column, :conditions, :live_data, :end_date].include?(k)
end
raise ArgumentError.new("Invalid aggregation #{options[:aggregation]}!") if options[:aggregation] && ![:count, :sum, :maximum, :minimum, :average].include?(options[:aggregation])
raise ArgumentError.new('The name of the column holding the value to sum has to be specified for aggregation :sum!') if [:sum, :maximum, :minimum, :average].include?(options[:aggregation]) && !options.key?(:value_column)
(DIR) diff --git a/lib/saulabs/reportable/report_cache.rb b/lib/saulabs/reportable/report_cache.rb
@@ -96,9 +96,12 @@ module Saulabs
if options[:cacheable] and cached = cached_data.find { |cached| reporting_period == cached[0] }
result << [cached[0].date_time, cached[1]]
elsif reporting_period.last_date_time.past?
- new_cached = build_cached_data(report, options[:grouping], options[:conditions], reporting_period, find_value(new_data, reporting_period))
- new_cached.save!
- result << [reporting_period.date_time, new_cached.value]
+ value = find_value(new_data, reporting_period)
+ if options[:cacheable]
+ new_cached = build_cached_data(report, options[:grouping], options[:conditions], reporting_period, value))
+ new_cached.save!
+ end
+ result << [reporting_period.date_time, value]
else
result << [reporting_period.date_time, find_value(new_data, reporting_period)]
end
@@ -138,6 +141,7 @@ module Saulabs
end
def self.read_cached_data(report, options)
+ return [] if not options[:cacheable]
conditions = build_conditions_for_reading_cached_data(report, options)
conditions.limit(options[:limit]).order('reporting_period ASC')
end
(DIR) diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -22,7 +22,7 @@ describe Saulabs::Reportable::Report do
Saulabs::Reportable::ReportCache.should_receive(:process).once.with(
@report,
- { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :include => [], :live_data => false, :end_date => false, :distinct => false }
+ { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :include => [], :live_data => false, :end_date => false, :distinct => false, :cacheable => true }
)
@report.run
@@ -31,7 +31,7 @@ describe Saulabs::Reportable::Report do
it 'should process the data with the report cache when custom conditions are given' do
Saulabs::Reportable::ReportCache.should_receive(:process).once.with(
@report,
- { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :include => [], :live_data => false, :end_date => false, :distinct => false }
+ { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :include => [], :live_data => false, :end_date => false, :distinct => false, :cacheable => true }
)
@report.run(:conditions => { :some => :condition })
@@ -48,7 +48,7 @@ describe Saulabs::Reportable::Report do
Saulabs::Reportable::Grouping.should_receive(:new).once.with(:month).and_return(grouping)
Saulabs::Reportable::ReportCache.should_receive(:process).once.with(
@report,
- { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false, :distinct => false, :include => [] }
+ { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false, :distinct => false, :include => [], :cacheable => true }
)
@report.run(:grouping => :month)