the Report#run method only accepts :conditions and _limit options - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 6a2014d4aef05d98e682c1f0db51caa6e885be45
 (DIR) parent 9a449b3f4445570df2178ccb549b16124f22cc18
 (HTM) Author: marcoow <marco.otte-witte@simplabs.com>
       Date:   Fri,  5 Dec 2008 23:49:37 +0800
       
       the Report#run method only accepts :conditions and _limit options
       
       Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Diffstat:
         M lib/kvlr/reports_as_sparkline/repo… |      31 +++++++++++++++----------------
         M lib/kvlr/reports_as_sparkline/repo… |      21 +++++++++++----------
         M spec/db/schema.rb                   |      16 ++++++++++------
         M spec/other/cumulated_report_spec.rb |       3 ++-
         M spec/other/report_cache_spec.rb     |      10 +++++-----
         M spec/other/report_spec.rb           |      17 +++++++++--------
       
       6 files changed, 52 insertions(+), 46 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_sparkline/report.rb
       @@ -4,18 +4,18 @@ module Kvlr #:nodoc:
        
            class Report
        
       -      attr_reader :klass, :name
       +      attr_reader :klass, :name, :date_column_name, :value_column_name, :grouping, :aggregation
        
              def initialize(klass, name, options = {})
                ensure_valid_options(options)
       -        @klass = klass
       -        @name  = name
       +        @klass             = klass
       +        @name              = name
       +        @date_column_name  = (options[:date_column_name] || 'created_at').to_s
       +        @value_column_name = (options[:value_column_name] || (options[:aggregation] != :sum ? 'id' : @name)).to_s
       +        @aggregation       = options[:aggregation] || :count
       +        @grouping          = Grouping.new(options[:grouping] || :day)
                @options = {
                  :limit             => options[:limit] || 100,
       -          :aggregation       => options[:aggregation] || :count,
       -          :grouping          => options[:grouping] || :day,
       -          :date_column_name  => (options[:date_column_name] || 'created_at').to_s,
       -          :value_column_name => (options[:value_column_name] || (options[:aggregation] != :sum ? 'id' : @name)).to_s,
                  :conditions        => options[:conditions] || ['']
                }
                @options.merge!(options)
       @@ -25,21 +25,20 @@ module Kvlr #:nodoc:
              def run(options = {})
                ensure_valid_options(options)
                options = @options.merge(options)
       -        grouping = Grouping.new(options[:grouping])
       -        ReportCache.cached_transaction(self, grouping, options[:limit], options[:date_column_name]) do |begin_at|
       -          conditions = setup_conditions(begin_at, options[:date_column_name], options[:conditions])
       -          @klass.send(options[:aggregation],
       -            options[:value_column_name].to_s,
       +        ReportCache.cached_transaction(self, options[:limit]) do |begin_at|
       +          conditions = setup_conditions(begin_at, options[:conditions])
       +          @klass.send(@aggregation,
       +            @value_column_name,
                    :conditions => conditions,
       -            :group => grouping.to_sql(options[:date_column_name]),
       -            :order => "#{grouping.to_sql(options[:date_column_name])} DESC"
       +            :group => @grouping.to_sql(@date_column_name),
       +            :order => "#{@grouping.to_sql(@date_column_name)} DESC"
                  )
                end
              end
        
              private
        
       -        def setup_conditions(begin_at, date_column_name, custom_conditions = [])
       +        def setup_conditions(begin_at, custom_conditions = [])
                  conditions = ['']
                  if custom_conditions.is_a?(Hash)
                    conditions = [
       @@ -49,7 +48,7 @@ module Kvlr #:nodoc:
                  elsif custom_conditions.size > 0
                    conditions = [(custom_conditions[0] || ''), *custom_conditions[1..-1]]
                  end
       -          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + date_column_name.to_s} >= ?"
       +          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + @date_column_name.to_s} >= ?"
                  conditions << begin_at
                end
        
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_as_sparkline/report_cache.rb
       @@ -4,25 +4,25 @@ module Kvlr #:nodoc:
        
            class ReportCache < ActiveRecord::Base
        
       -      def self.cached_transaction(report, grouping, limit, date_column_name, &block)
       +      def self.cached_transaction(report, limit, &block)
                raise ArgumentError.new('A block must be given') unless block_given?
                self.transaction do
                  cached_data = self.find(
                    :all,
       -            :conditions => { :model_name => report.klass.to_s, :report_name => report.name.to_s, :report_grouping => grouping.identifier.to_s },
       +            :conditions => { :model_name => report.klass.to_s, :report_name => report.name.to_s, :grouping => report.grouping.identifier.to_s, :aggregation => report.aggregation.to_s },
                    :limit => limit,
       -            :order => "#{date_column_name.to_s} DESC"
       +            :order => "#{report.date_column_name.to_s} DESC"
                  )
       -          last_reporting_period_to_read = get_last_reporting_period(cached_data, grouping, grouping.first_reporting_period(limit))
       +          last_reporting_period_to_read = get_last_reporting_period(cached_data, report.grouping, limit)
                  new_data = yield(last_reporting_period_to_read)
       -          return update_cache(new_data, cached_data, report, grouping)
       +          return update_cache(new_data, cached_data, report)
                end
              end
        
              private
        
       -        def self.get_last_reporting_period(cached_data, grouping, acc)
       -          return acc if cached_data.empty?
       +        def self.get_last_reporting_period(cached_data, grouping, limit)
       +          return grouping.first_reporting_period(limit) if cached_data.empty?
                  puts cached_data[0].reporting_period.class.inspect
                  period = grouping.to_reporting_period(cached_data[0].reporting_period)
                  cached_data[1..-2].each_with_index do |cached, i|
       @@ -33,7 +33,7 @@ module Kvlr #:nodoc:
                  return grouping.to_reporting_period(cached_data[-1].reporting_period)
                end
        
       -        def self.update_cache(new_data, cached_data, report, grouping)
       +        def self.update_cache(new_data, cached_data, report)
                  rows_to_write = (0..-1)
                  if cached_data.size > 0 && new_data.size > 0
                    cached_data.last.update_attributes!(:value => new_data.first[1])
       @@ -43,8 +43,9 @@ module Kvlr #:nodoc:
                    self.create!(
                      :model_name => report.klass.to_s,
                      :report_name => report.name.to_s,
       -              :report_grouping => grouping.identifier.to_s,
       -              :reporting_period => grouping.to_reporting_period(DateTime.parse(row[0])),
       +              :grouping => report.grouping.identifier.to_s,
       +              :aggregation => report.aggregation.to_s,
       +              :reporting_period => report.grouping.to_reporting_period(DateTime.parse(row[0])),
                      :value => row[1]
                    )
                  end
 (DIR) diff --git a/spec/db/schema.rb b/spec/db/schema.rb
       @@ -10,7 +10,8 @@ ActiveRecord::Schema.define(:version => 1) do
          create_table :report_caches, :force => true do |t|
            t.string   :model_name,       :null => false
            t.string   :report_name,      :null => false
       -    t.string   :report_grouping,  :null => false
       +    t.string   :grouping,         :null => false
       +    t.string   :aggregation,      :null => false
            t.float    :value,            :null => false, :default => 0
            t.datetime :reporting_period, :null => false
        
       @@ -19,12 +20,15 @@ ActiveRecord::Schema.define(:version => 1) do
          add_index :report_caches, [
            :model_name,
            :report_name,
       -    :report_grouping
       -  ], :name => 'name_klass_grouping'
       +    :grouping,
       +    :aggregation
       +  ], :name => 'name_model_grouping_agregation'
          add_index :report_caches, [
       -    :model_name, :report_name,
       -    :report_grouping,
       +    :model_name,
       +    :report_name,
       +    :grouping,
       +    :aggregation,
            :reporting_period
       -  ], :unique => true, :name => 'name_klass_grouping_period'
       +  ], :unique => true, :name => 'name_model_grouping_aggregation_period'
        
        end
 (DIR) diff --git a/spec/other/cumulated_report_spec.rb b/spec/other/cumulated_report_spec.rb
       @@ -24,7 +24,8 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
              end
        
              it 'should return correct data for :aggregation => :sum' do
       -        result = @report.run(:aggregation => :sum, :value_column_name => :profile_visits).to_a
       +        @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :sum, :value_column_name => :profile_visits)
       +        result = @report.run().to_a
        
                result[0][1].should == 1
                result[1][1].should == 6
 (DIR) diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb
       @@ -10,14 +10,14 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
        
            it 'should raise an ArgumentError if no block is given' do
              lambda do
       -        Kvlr::ReportsAsSparkline::ReportCache.cached_transaction(@report, :count, 100, 'created_at')
       +        Kvlr::ReportsAsSparkline::ReportCache.cached_transaction(@report, 100)
              end.should raise_error(ArgumentError)
            end
        
            it 'sould start a transaction' do
              Kvlr::ReportsAsSparkline::ReportCache.should_receive(:transaction)
        
       -      Kvlr::ReportsAsSparkline::ReportCache.cached_transaction(@report, :count, 100, 'created_at') {}
       +      Kvlr::ReportsAsSparkline::ReportCache.cached_transaction(@report, 100) {}
            end
        
          end
       @@ -38,7 +38,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
                :get_last_reporting_period,
                cached_data,
                @grouping,
       -        @grouping.first_reporting_period(3)
       +        10
              ).should == @grouping.to_reporting_period(Time.now - 2.days)
            end
        
       @@ -47,8 +47,8 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
                :get_last_reporting_period,
                [],
                @grouping,
       -        @grouping.first_reporting_period(3)
       -      ).should == @grouping.to_reporting_period(Time.now - 3.days)
       +        10
       +      ).should == @grouping.to_reporting_period(Time.now - 10.days)
            end
        
          end
 (DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
       @@ -64,9 +64,9 @@ describe Kvlr::ReportsAsSparkline::Report do
              end
        
              it 'should validate the specified options' do
       -        @report.should_receive(:ensure_valid_options).once.with(:aggregation => :sum, :value_column_name => :profile_visits, :limit => 3)
       +        @report.should_receive(:ensure_valid_options).once.with(:limit => 3)
        
       -        result = @report.run(:aggregation => :sum, :value_column_name => :profile_visits, :limit => 3)
       +        result = @report.run(:limit => 3)
              end
        
              it 'should return correct data for :aggregation => :count' do
       @@ -77,7 +77,8 @@ describe Kvlr::ReportsAsSparkline::Report do
              end
        
              it 'should return correct data for :aggregation => :sum' do
       -        result = @report.run(:aggregation => :sum, :value_column_name => :profile_visits).to_a
       +        @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :sum, :value_column_name => :profile_visits)
       +        result = @report.run().to_a
        
                result[0][1].should == 1
                result[1][1].should == 5
       @@ -104,26 +105,26 @@ describe Kvlr::ReportsAsSparkline::Report do
            it 'should return conditions for date_column_name >= begin_at only if no custom conditions are specified' do
              begin_at = Time.now
        
       -      @report.send(:setup_conditions, begin_at, 'created_at').should == ['created_at >= ?', begin_at]
       +      @report.send(:setup_conditions, begin_at).should == ['created_at >= ?', begin_at]
            end
        
            it 'should return conditions for date_column_name >= begin_at only if an empty Hash of custom conditions is specified' do
              begin_at = Time.now
        
       -      @report.send(:setup_conditions, begin_at, 'created_at', {}).should == ['created_at >= ?', begin_at]
       +      @report.send(:setup_conditions, begin_at, {}).should == ['created_at >= ?', begin_at]
            end
        
            it 'should return conditions for date_column_name >= begin_at only if an empty Array of custom conditions is specified' do
              begin_at = Time.now
        
       -      @report.send(:setup_conditions, begin_at, 'created_at', []).should == ['created_at >= ?', begin_at]
       +      @report.send(:setup_conditions, begin_at, []).should == ['created_at >= ?', begin_at]
            end
        
            it 'should correctly include custom conditions if they are specified as a Hash' do
              begin_at = Time.now
              custom_conditions = { :first_name => 'first name', :last_name => 'last name' }
        
       -      conditions = @report.send(:setup_conditions, begin_at, 'created_at', custom_conditions)
       +      conditions = @report.send(:setup_conditions, begin_at, custom_conditions)
              #cannot check for equality of complete conditions array since hashes are not ordered (thus it is unknown whether first_name or last_name comes first)
              conditions[0].should include('first_name = ?')
              conditions[0].should include('last_name = ?')
       @@ -137,7 +138,7 @@ describe Kvlr::ReportsAsSparkline::Report do
              begin_at = Time.now
              custom_conditions = ['first_name = ? AND last_name = ?', 'first name', 'last name']
        
       -      @report.send(:setup_conditions, begin_at, 'created_at', custom_conditions).should == [
       +      @report.send(:setup_conditions, begin_at, custom_conditions).should == [
                'first_name = ? AND last_name = ? AND created_at >= ?',
                'first name',
                'last name',