now supporting custom conditions as Array as well as as Hash; always run tests on sqlite3 - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 9a449b3f4445570df2178ccb549b16124f22cc18
 (DIR) parent cabd6ea34c0922bdb62628218399f34f7857e2dd
 (HTM) Author: marcoow <marco.otte-witte@simplabs.com>
       Date:   Fri,  5 Dec 2008 21:40:23 +0800
       
       now supporting custom conditions as Array as well as as Hash; always run tests on sqlite3
       
       Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Diffstat:
         M lib/kvlr/reports_as_sparkline/repo… |      18 +++++++++++++++---
         M spec/boot.rb                        |       1 -
         M spec/other/report_spec.rb           |      88 ++++++++++++++++++++++++-------
       
       3 files changed, 84 insertions(+), 23 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_sparkline/report.rb
       @@ -27,9 +27,7 @@ module Kvlr #:nodoc:
                options = @options.merge(options)
                grouping = Grouping.new(options[:grouping])
                ReportCache.cached_transaction(self, grouping, options[:limit], options[:date_column_name]) do |begin_at|
       -          conditions = [options[:conditions][0], *options[:conditions][1..-1]]
       -          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + options[:date_column_name].to_s} >= ?"
       -          conditions << begin_at
       +          conditions = setup_conditions(begin_at, options[:date_column_name], options[:conditions])
                  @klass.send(options[:aggregation],
                    options[:value_column_name].to_s,
                    :conditions => conditions,
       @@ -41,6 +39,20 @@ module Kvlr #:nodoc:
        
              private
        
       +        def setup_conditions(begin_at, date_column_name, custom_conditions = [])
       +          conditions = ['']
       +          if custom_conditions.is_a?(Hash)
       +            conditions = [
       +              custom_conditions.map{ |k, v| "#{k.to_s} = ?" }.join(' AND '),
       +              *custom_conditions.map{ |k, v| v }
       +            ]
       +          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 << begin_at
       +        end
       +
                def ensure_valid_options(options)
                  options.each_key do |k|
                    raise ArgumentError.new("Invalid option #{k}") unless [:limit, :aggregation, :grouping, :date_column_name, :value_column_name, :conditions].include?(k)
 (DIR) diff --git a/spec/boot.rb b/spec/boot.rb
       @@ -19,6 +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')))
       -# TODO: connect to test database of rails project if exists
        ActiveRecord::Base.establish_connection(databases['sqlite3'])
        load(File.join(File.dirname(__FILE__), 'db', 'schema.rb'))
 (DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
       @@ -6,8 +6,42 @@ describe Kvlr::ReportsAsSparkline::Report do
            @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
          end
        
       +  share_as :OptionValidation do
       +
       +    it 'should not raise an error if valid options are specified' do
       +      lambda { @report.send(:ensure_valid_options, {
       +        :limit             => 100,
       +        :aggregation       => :count,
       +        :grouping          => :day,
       +        :date_column_name  => 'created_at',
       +        :value_column_name => 'id',
       +        :conditions        => []
       +      }) }.should_not raise_error(ArgumentError)
       +    end
       +
       +    it 'should raise an error if an unsupported option is specified' do
       +      lambda { @report.send(:ensure_valid_options, { :invalid => :option }) }.should raise_error(ArgumentError)
       +    end
       +
       +    it 'should raise an error if an invalid aggregation is specified' do
       +      lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
       +    end
       +
       +    it 'should raise an error if an invalid grouping is specified' do
       +      lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
       +    end
       +
       +    it 'should raise an error if malformed conditions are specified' do
       +      lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)
       +      lambda { @report.send(:ensure_valid_options, { :conditions => { :user_name => 'username' } }) }.should raise_error(ArgumentError)
       +    end
       +
       +  end
       +
          describe '.run' do
        
       +    include OptionValidation
       +
            it 'should invoke the default aggregation method on the model' do
              User.should_receive(:count).once.and_return([])
        
       @@ -65,34 +99,50 @@ describe Kvlr::ReportsAsSparkline::Report do
        
          end
        
       -  share_as :OptionValidation do
       +  describe '.setup_conditions' do
        
       -    it 'should not raise an error if valid options are specified' do
       -      lambda { @report.send(:ensure_valid_options, {
       -        :limit             => 100,
       -        :aggregation       => :count,
       -        :grouping          => :day,
       -        :date_column_name  => 'created_at',
       -        :value_column_name => 'id',
       -        :conditions        => []
       -      }) }.should_not raise_error(ArgumentError)
       +    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]
            end
        
       -    it 'should raise an error if an unsupported option is specified' do
       -      lambda { @report.send(:ensure_valid_options, { :invalid => :option }) }.should raise_error(ArgumentError)
       +    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]
            end
        
       -    it 'should raise an error if an invalid aggregation is specified' do
       -      lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
       +    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]
            end
        
       -    it 'should raise an error if an invalid grouping is specified' do
       -      lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
       +    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)
       +      #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 = ?')
       +      conditions[0].should include('created_at >= ?')
       +      conditions.should include('first name')
       +      conditions.should include('last name')
       +      conditions.should include(begin_at)
            end
        
       -    it 'should raise an error if malformed conditions are specified' do
       -      lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)
       -      lambda { @report.send(:ensure_valid_options, { :conditions => { :user_name => 'username' } }) }.should raise_error(ArgumentError)
       +    it 'should correctly include custom conditions if they are specified as an Array' 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 == [
       +        'first_name = ? AND last_name = ? AND created_at >= ?',
       +        'first name',
       +        'last name',
       +        begin_at
       +      ]
            end
        
          end