| report execution can now handle conditions like :column => [1,2,3] or :column =… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit c3fb5b3acc00333cfe3b2b5d61963a15e7859760 | |
| parent 4456641ee6fc1c363ab38ccac57eea159b1e2a56 | |
| Author: Marco Otte-Witte <[email protected]> | |
| Date: Mon, 23 Feb 2009 15:57:42 +0100 | |
| report execution can now handle conditions like :column => [1,2,3] or :column =… | |
| Diffstat: | |
| M README.rdoc | 4 ++-- | |
| M generators/reports_as_sparkline_mi… | 4 ++-- | |
| M lib/kvlr/reports_as_sparkline.rb | 2 +- | |
| M lib/kvlr/reports_as_sparkline/repo… | 18 +++++++++++++----- | |
| M rdoc/classes/Kvlr/ReportsAsSparkli… | 6 ++++-- | |
| M rdoc/classes/Kvlr/ReportsAsSparkli… | 10 ++++++---- | |
| M rdoc/classes/Kvlr/ReportsAsSparkli… | 52 ++++++++++++++++-----------… | |
| M rdoc/created.rid | 2 +- | |
| M rdoc/files/lib/kvlr/reports_as_spa… | 2 +- | |
| M rdoc/files/lib/kvlr/reports_as_spa… | 2 +- | |
| M rdoc/files/lib/kvlr/reports_as_spa… | 2 +- | |
| M spec/classes/report_spec.rb | 89 +++++++++++++++++++++++++----… | |
| 12 files changed, 132 insertions(+), 61 deletions(-) | |
| --- | |
| diff --git a/README.rdoc b/README.rdoc | |
| @@ -9,7 +9,7 @@ to it with the following options: | |
| * :date_column - The name of the date column on that the records are aggregated | |
| * :value_column - The name of the column that holds the value to sum for aggre… | |
| -* :aggregation - The aggregation to use (either :count or :sum); when using :s… | |
| +* :aggregation - The aggregation to use (one of :count, :sum, :minimum, :maxim… | |
| * :grouping - The period records are grouped on (:hour, :day, :week, :month); … | |
| * :limit - The number of periods to get (see :grouping) | |
| * :conditions - Conditions like in ActiveRecord::Base#find; only records that … | |
| @@ -101,4 +101,4 @@ If you are on PostgreSQL, you should add functional indices: | |
| If you want ot suggest any new features or report bugs, do so at http://simpla… | |
| -© 2008-2009 Martin Kavalar, Marco Otte-Witte (http://simplabs.com/#open-sourc… | |
| +© 2008-2009 Martin Kavalar, Marco Otte-Witte (http://simplabs.com/#projects),… | |
| diff --git a/generators/reports_as_sparkline_migration/templates/migration.erb … | |
| @@ -28,8 +28,8 @@ class <%= class_name %> < ActiveRecord::Migration | |
| end | |
| def self.down | |
| - remove_index :name_model_grouping_agregation | |
| - remove_index :name_model_grouping_aggregation_period | |
| + remove_index :report_caches, :name => :name_model_grouping_agregation | |
| + remove_index :report_caches, :name => :name_model_grouping_aggregation_per… | |
| drop_table :report_caches | |
| end | |
| diff --git a/lib/kvlr/reports_as_sparkline.rb b/lib/kvlr/reports_as_sparkline.rb | |
| @@ -18,7 +18,7 @@ module Kvlr #:nodoc: | |
| # | |
| # * <tt>:date_column</tt> - The name of the date column on that the reco… | |
| # * <tt>:value_column</tt> - The name of the column that holds the value… | |
| - # * <tt>:aggregation</tt> - The aggregation to use (either :count or :su… | |
| + # * <tt>:aggregation</tt> - The aggregation to use (one of :count, :sum,… | |
| # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,… | |
| # * <tt>:limit</tt> - The number of periods to get (see :grouping) | |
| # * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; o… | |
| diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar… | |
| @@ -15,7 +15,7 @@ module Kvlr #:nodoc: | |
| # | |
| # * <tt>:date_column</tt> - The name of the date column on that the reco… | |
| # * <tt>:value_column</tt> - The name of the column that holds the value… | |
| - # * <tt>:aggregation</tt> - The aggregation to use (either :count or :su… | |
| + # * <tt>:aggregation</tt> - The aggregation to use (one of :count, :sum,… | |
| # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,… | |
| # * <tt>:limit</tt> - The number of periods to get (see :grouping) | |
| # * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; o… | |
| @@ -25,8 +25,8 @@ module Kvlr #:nodoc: | |
| @klass = klass | |
| @name = name | |
| @date_column = (options[:date_column] || 'created_at').to_s | |
| - @value_column = (options[:value_column] || (options[:aggregation] != :… | |
| @aggregation = options[:aggregation] || :count | |
| + @value_column = (options[:value_column] || (@aggregation == :count ? '… | |
| @options = { | |
| :limit => options[:limit] || 100, | |
| :conditions => options[:conditions] || [], | |
| @@ -69,7 +69,15 @@ module Kvlr #:nodoc: | |
| def setup_conditions(begin_at, custom_conditions = []) | |
| conditions = [''] | |
| if custom_conditions.is_a?(Hash) | |
| - conditions = [custom_conditions.map{ |k, v| "#{k.to_s} = ?" }.join… | |
| + 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..… | |
| end | |
| @@ -83,8 +91,8 @@ module Kvlr #:nodoc: | |
| options.each_key do |k| | |
| raise ArgumentError.new("Invalid option #{k}") unless [:limit,… | |
| end | |
| - raise ArgumentError.new("Invalid aggregation #{options[:aggregat… | |
| - raise ArgumentError.new('The name of the column holding the valu… | |
| + raise ArgumentError.new("Invalid aggregation #{options[:aggregat… | |
| + raise ArgumentError.new('The name of the column holding the valu… | |
| when :run | |
| options.each_key do |k| | |
| raise ArgumentError.new("Invalid option #{k}") unless [:limit,… | |
| diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/ClassMethods.html b/rdoc/clas… | |
| @@ -134,8 +134,10 @@ aggregated | |
| for aggregation :sum | |
| </li> | |
| -<li><tt>:aggregation</tt> - The aggregation to use (either :count or :sum); | |
| -when using :sum, :value_column must also be specified | |
| +<li><tt>:aggregation</tt> - The aggregation to use (one of :count, :sum, | |
| +:minimum, :maximum or :average); when using anything other than :count, | |
| +:value_column must also be specified (<b>If you really want to e.g. sumon | |
| +the ‘id’ column, you have to explicitely say so.</b>) | |
| </li> | |
| <li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, | |
| diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/Report.html b/rdoc/classes/Kv… | |
| @@ -187,8 +187,10 @@ aggregated | |
| for aggregation :sum | |
| </li> | |
| -<li><tt>:aggregation</tt> - The aggregation to use (either :count or :sum); | |
| -when using :sum, :value_column must also be specified | |
| +<li><tt>:aggregation</tt> - The aggregation to use (one of :count, :sum, | |
| +:minimum, :maximum or :average); when using anything other than :count, | |
| +:value_column must also be specified (<b>If you really want to e.g. sumon | |
| +the ‘id’ column, you have to explicitely say so.</b>) | |
| </li> | |
| <li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, | |
| @@ -220,8 +222,8 @@ false) | |
| 25: <span class="ruby-ivar">@klass</span> = <span class="ruby-i… | |
| 26: <span class="ruby-ivar">@name</span> = <span class="ruby-i… | |
| 27: <span class="ruby-ivar">@date_column</span> = (<span class="ruby-… | |
| -28: <span class="ruby-ivar">@value_column</span> = (<span class="ruby-… | |
| -29: <span class="ruby-ivar">@aggregation</span> = <span class="ruby-i… | |
| +28: <span class="ruby-ivar">@aggregation</span> = <span class="ruby-i… | |
| +29: <span class="ruby-ivar">@value_column</span> = (<span class="ruby-… | |
| 30: <span class="ruby-ivar">@options</span> = { | |
| 31: <span class="ruby-identifier">:limit</span> =<span class="r… | |
| 32: <span class="ruby-identifier">:conditions</span> =<span class="r… | |
| diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/ReportingPeriod.html b/rdoc/c… | |
| @@ -246,19 +246,19 @@ hour/day/month/year) | |
| onclick="toggleCode('M000008-source');return false;">[Source]</a><… | |
| <div class="method-source-code" id="M000008-source"> | |
| <pre> | |
| - <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report… | |
| -54: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword k… | |
| -55: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw… | |
| -56: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -57: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -58: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -59: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -60: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -61: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -62: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -63: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -64: <span class="ruby-keyword kw">end</span> | |
| -65: <span class="ruby-keyword kw">end</span> | |
| + <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report… | |
| +53: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword k… | |
| +54: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw… | |
| +55: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +56: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +57: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +58: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +59: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +60: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +61: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +62: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +63: <span class="ruby-keyword kw">end</span> | |
| +64: <span class="ruby-keyword kw">end</span> | |
| </pre> | |
| </div> | |
| </div> | |
| @@ -283,19 +283,19 @@ hour/day/month/year) | |
| onclick="toggleCode('M000009-source');return false;">[Source]</a><… | |
| <div class="method-source-code" id="M000009-source"> | |
| <pre> | |
| - <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report… | |
| -68: <span class="ruby-keyword kw">def</span> <span class="ruby-identifie… | |
| -69: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw… | |
| -70: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -71: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -72: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -73: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -74: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -75: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -76: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| -77: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| -78: <span class="ruby-keyword kw">end</span> | |
| -79: <span class="ruby-keyword kw">end</span> | |
| + <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report… | |
| +67: <span class="ruby-keyword kw">def</span> <span class="ruby-identifie… | |
| +68: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw… | |
| +69: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +70: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +71: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +72: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +73: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +74: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +75: <span class="ruby-keyword kw">when</span> <span class="ruby-iden… | |
| +76: <span class="ruby-keyword kw">self</span>.<span class="ruby-id… | |
| +77: <span class="ruby-keyword kw">end</span> | |
| +78: <span class="ruby-keyword kw">end</span> | |
| </pre> | |
| </div> | |
| </div> | |
| diff --git a/rdoc/created.rid b/rdoc/created.rid | |
| @@ -1 +1 @@ | |
| -Mon, 02 Feb 2009 14:31:59 +0100 | |
| +Mon, 23 Feb 2009 15:46:25 +0100 | |
| diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/report_rb.html b/rdoc/fil… | |
| @@ -56,7 +56,7 @@ | |
| </tr> | |
| <tr class="top-aligned-row"> | |
| <td><strong>Last Update:</strong></td> | |
| - <td>Mon Feb 02 11:52:42 +0100 2009</td> | |
| + <td>Mon Feb 23 15:45:50 +0100 2009</td> | |
| </tr> | |
| </table> | |
| </div> | |
| diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/reporting_period_rb.html … | |
| @@ -56,7 +56,7 @@ | |
| </tr> | |
| <tr class="top-aligned-row"> | |
| <td><strong>Last Update:</strong></td> | |
| - <td>Mon Feb 02 14:00:53 +0100 2009</td> | |
| + <td>Mon Feb 02 14:32:12 +0100 2009</td> | |
| </tr> | |
| </table> | |
| </div> | |
| diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline_rb.html b/rdoc/files/lib/… | |
| @@ -56,7 +56,7 @@ | |
| </tr> | |
| <tr class="top-aligned-row"> | |
| <td><strong>Last Update:</strong></td> | |
| - <td>Mon Feb 02 11:52:04 +0100 2009</td> | |
| + <td>Mon Feb 23 15:46:05 +0100 2009</td> | |
| </tr> | |
| </table> | |
| </div> | |
| diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb | |
| @@ -142,6 +142,57 @@ describe Kvlr::ReportsAsSparkline::Report do | |
| result[6][1].should == 0.0 | |
| end | |
| + it 'should return correct data for aggregation :maximum' do | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :maximum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run().to_a | |
| + | |
| + result[10][1].should == 2.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 3.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :minimum' do | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :minimum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run().to_a | |
| + | |
| + result[10][1].should == 2.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 2.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :average' do | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :average, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run().to_a | |
| + | |
| + result[10][1].should == 2.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 2.5 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| it 'should return correct data for aggregation :count when custom … | |
| @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| :aggregation => :count, | |
| @@ -278,47 +329,55 @@ describe Kvlr::ReportsAsSparkline::Report do | |
| describe '#setup_conditions' do | |
| - it 'should return conditions for date_column >= begin_at only when no cust… | |
| - begin_at = Time.now | |
| + before do | |
| + @begin_at = Time.now | |
| + end | |
| - @report.send(:setup_conditions, begin_at).should == ['created_at >= ?', … | |
| + it 'should return conditions for date_column >= begin_at only when no cust… | |
| + @report.send(:setup_conditions, @begin_at).should == ['created_at >= ?',… | |
| end | |
| it 'should return conditions for date_column >= begin_at only when an empt… | |
| - begin_at = Time.now | |
| - | |
| - @report.send(:setup_conditions, begin_at, {}).should == ['created_at >= … | |
| + @report.send(:setup_conditions, @begin_at, {}).should == ['created_at >=… | |
| end | |
| it 'should return conditions for date_column >= begin_at only when an empt… | |
| - begin_at = Time.now | |
| - | |
| - @report.send(:setup_conditions, begin_at, []).should == ['created_at >= … | |
| + @report.send(:setup_conditions, @begin_at, []).should == ['created_at >=… | |
| end | |
| 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, custom_conditions) | |
| + conditions = @report.send(:setup_conditions, @begin_at, custom_condition… | |
| #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) | |
| + conditions.should include(@begin_at) | |
| + end | |
| + | |
| + it 'should correctly translate { :column => nil }' do | |
| + @report.send(:setup_conditions, @begin_at, { :column => nil }).should ==… | |
| + end | |
| + | |
| + it 'should correctly translate { :column => [1, 2] }' do | |
| + @report.send(:setup_conditions, @begin_at, { :column => [1, 2] }).should… | |
| + end | |
| + | |
| + it 'should correctly translate { :column => (1..3) }' do | |
| + @report.send(:setup_conditions, @begin_at, { :column => (1..3) }).should… | |
| end | |
| 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, custom_conditions).should == [ | |
| + @report.send(:setup_conditions, @begin_at, custom_conditions).should == [ | |
| 'first_name = ? AND last_name = ? AND created_at >= ?', | |
| 'first name', | |
| 'last name', | |
| - begin_at | |
| + @begin_at | |
| ] | |
| end | |