| now supporting custom conditions as Array as well as as Hash; always run tests … | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 9a449b3f4445570df2178ccb549b16124f22cc18 | |
| parent cabd6ea34c0922bdb62628218399f34f7857e2dd | |
| Author: marcoow <[email protected]> | |
| Date: Fri, 5 Dec 2008 21:40:23 +0800 | |
| now supporting custom conditions as Array as well as as Hash; always run tests … | |
| Signed-off-by: Marco Otte-Witte <[email protected]> | |
| 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(-) | |
| --- | |
| diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar… | |
| @@ -27,9 +27,7 @@ module Kvlr #:nodoc: | |
| options = @options.merge(options) | |
| grouping = Grouping.new(options[:grouping]) | |
| ReportCache.cached_transaction(self, grouping, options[:limit], option… | |
| - conditions = [options[:conditions][0], *options[:conditions][1..-1]] | |
| - conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + options[… | |
| - conditions << begin_at | |
| + conditions = setup_conditions(begin_at, options[:date_column_name], … | |
| @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..… | |
| + end | |
| + conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ') + date_col… | |
| + conditions << begin_at | |
| + end | |
| + | |
| def ensure_valid_options(options) | |
| options.each_key do |k| | |
| raise ArgumentError.new("Invalid option #{k}") unless [:limit, :ag… | |
| 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'… | |
| databases = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'databa… | |
| -# 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')) | |
| 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 }) }.… | |
| + end | |
| + | |
| + it 'should raise an error if an invalid aggregation is specified' do | |
| + lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| + end | |
| + | |
| + it 'should raise an error if an invalid grouping is specified' do | |
| + lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| + end | |
| + | |
| + it 'should raise an error if malformed conditions are specified' do | |
| + lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.sho… | |
| + lambda { @report.send(:ensure_valid_options, { :conditions => { :user_na… | |
| + 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 c… | |
| + begin_at = Time.now | |
| + | |
| + @report.send(:setup_conditions, begin_at, 'created_at').should == ['crea… | |
| end | |
| - it 'should raise an error if an unsupported option is specified' do | |
| - lambda { @report.send(:ensure_valid_options, { :invalid => :option }) }.… | |
| + it 'should return conditions for date_column_name >= begin_at only if an e… | |
| + begin_at = Time.now | |
| + | |
| + @report.send(:setup_conditions, begin_at, 'created_at', {}).should == ['… | |
| end | |
| - it 'should raise an error if an invalid aggregation is specified' do | |
| - lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| + it 'should return conditions for date_column_name >= begin_at only if an e… | |
| + begin_at = Time.now | |
| + | |
| + @report.send(:setup_conditions, begin_at, 'created_at', []).should == ['… | |
| end | |
| - it 'should raise an error if an invalid grouping is specified' do | |
| - lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| + it 'should correctly include custom conditions if they are specified as a … | |
| + begin_at = Time.now | |
| + custom_conditions = { :first_name => 'first name', :last_name => 'last n… | |
| + | |
| + conditions = @report.send(:setup_conditions, begin_at, 'created_at', cus… | |
| + #cannot check for equality of complete conditions array since hashes are… | |
| + 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 }) }.sho… | |
| - lambda { @report.send(:ensure_valid_options, { :conditions => { :user_na… | |
| + it 'should correctly include custom conditions if they are specified as an… | |
| + begin_at = Time.now | |
| + custom_conditions = ['first_name = ? AND last_name = ?', 'first name', '… | |
| + | |
| + @report.send(:setup_conditions, begin_at, 'created_at', custom_condition… | |
| + 'first_name = ? AND last_name = ? AND created_at >= ?', | |
| + 'first name', | |
| + 'last name', | |
| + begin_at | |
| + ] | |
| end | |
| end |