tweaked generation of conditions - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit b09837e7524ec6d84868fb2ab67917794548157c
 (DIR) parent a48dd3186dd2cf21e90f5b2056eca0ab9570991e
 (HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Date:   Thu, 16 Apr 2009 09:55:02 +0200
       
       tweaked generation of conditions
       
       Diffstat:
         M lib/simplabs/reports_as_sparkline/… |      17 ++---------------
         M spec/classes/report_spec.rb         |      33 ++++++++++---------------------
       
       2 files changed, 12 insertions(+), 38 deletions(-)
       ---
 (DIR) diff --git a/lib/simplabs/reports_as_sparkline/report.rb b/lib/simplabs/reports_as_sparkline/report.rb
       @@ -76,21 +76,8 @@ module Simplabs #:nodoc:
                end
        
                def setup_conditions(begin_at, end_at, custom_conditions = [])
       -          conditions = ['']
       -          if custom_conditions.is_a?(Hash)
       -            conditions = [custom_conditions.map do |k, v|
       -              if v.nil?
       -                "#{k.to_s} IS NULL"
       -              elsif v.is_a?(Array) || v.is_a?(Range)
       -                "#{k.to_s} IN (?)"
       -              else
       -                "#{k.to_s} = ?"
       -              end
       -            end.join(' AND '), *custom_conditions.map { |k, v| v }.compact]
       -          elsif custom_conditions.size > 0
       -            conditions = [(custom_conditions[0] || ''), *custom_conditions[1..-1]]
       -          end
       -          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + @date_column.to_s} "
       +          conditions = [@klass.send(:sanitize_sql_for_conditions, custom_conditions) || '']
       +          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ')}\"#{@klass.table_name}\".\"#{@date_column.to_s}\" "
                  conditions[0] += if begin_at && end_at
                    'BETWEEN ? AND ?'
                  elsif begin_at
 (DIR) diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
       @@ -380,15 +380,15 @@ describe Simplabs::ReportsAsSparkline::Report do
            end
        
            it 'should return conditions for date_column BETWEEN begin_at and end_at only when no custom conditions are specified and both begin and end date are specified' do
       -      @report.send(:setup_conditions, @begin_at, @end_at).should == ['created_at BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at).should == ['"users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should return conditions for date_column >= begin_at when no custom conditions and a begin_at are specified' do
       -      @report.send(:setup_conditions, @begin_at, nil).should == ['created_at >= ?', @begin_at]
       +      @report.send(:setup_conditions, @begin_at, nil).should == ['"users"."created_at" >= ?', @begin_at]
            end
            
            it 'should return conditions for date_column <= end_at when no custom conditions and a end_at are specified' do
       -      @report.send(:setup_conditions, nil, @end_at).should == ['created_at <= ?', @end_at]
       +      @report.send(:setup_conditions, nil, @end_at).should == ['"users"."created_at" <= ?', @end_at]
            end
            
            it 'should raise an argument error when neither begin_at or end_at are specified' do
       @@ -396,49 +396,36 @@ describe Simplabs::ReportsAsSparkline::Report do
            end
        
            it 'should return conditions for date_column BETWEEN begin_at and end_date only when an empty Hash of custom conditions is specified' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, {}).should == ['created_at BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, {}).should == ['"users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should return conditions for date_column BETWEEN begin_at and end_date only when an empty Array of custom conditions is specified' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, []).should == ['created_at BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, []).should == ['"users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should correctly include custom conditions if they are specified as a Hash' do
              custom_conditions = { :first_name => 'first name', :last_name => 'last name' }
        
              conditions = @report.send(:setup_conditions, @begin_at, @end_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 BETWEEN ? AND ?')
       -      conditions.should include('first name')
       -      conditions.should include('last name')
       -      conditions.should include(@begin_at)
       -      conditions.should include(@end_at)
       +      conditions.should == ['"users"."first_name" = \'first name\' AND "users"."last_name" = \'last name\' AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should correctly translate { :column => nil }' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, { :column => nil }).should == ['column IS NULL AND created_at BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, { :column => nil }).should == ['"users"."column" IS NULL AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should correctly translate { :column => [1, 2] }' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, { :column => [1, 2] }).should == ['column IN (?) AND created_at BETWEEN ? AND ?', [1, 2], @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, { :column => [1, 2] }).should == ['"users"."column" IN (1,2) AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should correctly translate { :column => (1..3) }' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, { :column => (1..3) }).should == ['column IN (?) AND created_at BETWEEN ? AND ?', (1..3), @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, { :column => (1..3) }).should == ['"users"."column" BETWEEN 1 AND 3 AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
            it 'should correctly include custom conditions if they are specified as an Array' do
              custom_conditions = ['first_name = ? AND last_name = ?', 'first name', 'last name']
        
       -      @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions).should == [
       -        'first_name = ? AND last_name = ? AND created_at BETWEEN ? AND ?',
       -        'first name',
       -        'last name',
       -        @begin_at,
       -        @end_at
       -      ]
       +      @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions).should == ['first_name = \'first name\' AND last_name = \'last name\' AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
            end
        
          end