updated documentation, fixed bug in processing of data for end/beginning of year - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit d1aa1be511b8c85872d238b84faa356ea3231baf
(DIR) parent 8aa9bac80b0716c60fd58a5f78cba96077608b12
(HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Date: Mon, 2 Feb 2009 14:32:36 +0100
updated documentation, fixed bug in processing of data for end/beginning of year
Diffstat:
M README.rdoc | 2 +-
M lib/kvlr/reports_as_sparkline.rb | 2 +-
M lib/kvlr/reports_as_sparkline/grou… | 67 +++++++++++++++++++------------
M lib/kvlr/reports_as_sparkline/repo… | 4 ++--
M rdoc/classes/Kvlr/ReportsAsSparkli… | 4 +++-
M rdoc/classes/Kvlr/ReportsAsSparkli… | 6 ++++--
M rdoc/classes/Kvlr/ReportsAsSparkli… | 52 ++++++++++++++++----------------
M rdoc/created.rid | 2 +-
M rdoc/files/README_rdoc.html | 5 +++--
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 rdoc/files/lib/kvlr/reports_as_spa… | 2 +-
M spec/boot.rb | 2 +-
M spec/classes/grouping_spec.rb | 22 +++++++++++-----------
M spec/classes/report_spec.rb | 88 ++++++++++++++++++++++---------
16 files changed, 161 insertions(+), 103 deletions(-)
---
(DIR) diff --git a/README.rdoc b/README.rdoc
@@ -10,7 +10,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 aggregation :sum
* :aggregation - The aggregation to use (either :count or :sum); when using :sum, :value_column must also be specified
-* :grouping - The period records are grouped on (:hour, :day, :week, :month)
+* :grouping - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
* :limit - The number of periods to get (see :grouping)
* :conditions - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on
* :cumulate - Sets whether to cumulate the numbers (instead of [1, 2, 3] returns [1, 3, 6])
(DIR) diff --git a/lib/kvlr/reports_as_sparkline.rb b/lib/kvlr/reports_as_sparkline.rb
@@ -19,7 +19,7 @@ module Kvlr #:nodoc:
# * <tt>:date_column</tt> - The name of the date column on that the records are aggregated
# * <tt>:value_column</tt> - The name of the column that holds the value to sum for aggregation :sum
# * <tt>:aggregation</tt> - The aggregation to use (either :count or :sum); when using :sum, :value_column must also be specified
- # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month)
+ # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
# * <tt>:limit</tt> - The number of periods to get (see :grouping)
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on
# * <tt>:live_data</tt> - Specified whether data for the current reporting period is read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache only (defaults to false)
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sparkline/grouping.rb
@@ -18,30 +18,13 @@ module Kvlr #:nodoc:
end
def date_parts_from_db_string(db_string) #:nodoc:
- if ActiveRecord::Base.connection.adapter_name =~ /postgres/i
- case @identifier
- when :hour
- return (db_string[0..9].split('-') + [db_string[11..12]]).map(&:to_i)
- when :day
- return db_string[0..9].split('-').map(&:to_i)
- when :week
- parts = db_string[0..9].split('-').map(&:to_i)
- date = Date.new(parts[0], parts[1], parts[2])
- return [date.year, date.cweek]
- when :month
- return db_string[0..6].split('-')[0..1].map(&:to_i)
- end
- else
- parts = db_string.split('/').map(&:to_i)
- if ActiveRecord::Base.connection.adapter_name =~ /mysql/i
- if @identifier == :week && parts[1] > 52
- parts[0] += 1
- parts[1] = 1
- end
- return parts
- end
- parts[1] += 1 if @identifier == :week
- parts
+ return case ActiveRecord::Base.connection.adapter_name
+ when /mysql/i
+ from_mysql_db_string(db_string)
+ when /sqlite/i
+ from_sqlite_db_string(db_string)
+ when /postgres/i
+ from_postgresql_db_string(db_string)
end
end
@@ -58,6 +41,38 @@ module Kvlr #:nodoc:
private
+ def from_mysql_db_string(db_string)
+ if @identifier == :week
+ parts = [db_string[0..3], db_string[4..5]].map(&:to_i)
+ else
+ db_string.split('/').map(&:to_i)
+ end
+ end
+
+ def from_sqlite_db_string(db_string)
+ if @identifier == :week
+ parts = db_string.split('-').map(&:to_i)
+ date = Date.new(parts[0], parts[1], parts[2])
+ return [date.cwyear, date.cweek]
+ end
+ db_string.split('/').map(&:to_i)
+ end
+
+ def from_postgresql_db_string(db_string)
+ case @identifier
+ when :hour
+ return (db_string[0..9].split('-') + [db_string[11..12]]).map(&:to_i)
+ when :day
+ return db_string[0..9].split('-').map(&:to_i)
+ when :week
+ parts = db_string[0..9].split('-').map(&:to_i)
+ date = Date.new(parts[0], parts[1], parts[2])
+ return [date.cwyear, date.cweek]
+ when :month
+ return db_string[0..6].split('-')[0..1].map(&:to_i)
+ end
+ end
+
def mysql_format(date_column)
return case @identifier
when :hour
@@ -65,7 +80,7 @@ module Kvlr #:nodoc:
when :day
"DATE_FORMAT(#{date_column}, '%Y/%m/%d')"
when :week
- "DATE_FORMAT(#{date_column}, '%Y/%u')"
+ "YEARWEEK(#{date_column}, 3)"
when :month
"DATE_FORMAT(#{date_column}, '%Y/%m')"
end
@@ -78,7 +93,7 @@ module Kvlr #:nodoc:
when :day
"strftime('%Y/%m/%d', #{date_column})"
when :week
- "strftime('%Y/%W', #{date_column})"
+ "date(#{date_column}, 'weekday 0')"
when :month
"strftime('%Y/%m', #{date_column})"
end
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_sparkline/report.rb
@@ -16,7 +16,7 @@ module Kvlr #:nodoc:
# * <tt>:date_column</tt> - The name of the date column on that the records are aggregated
# * <tt>:value_column</tt> - The name of the column that holds the value to sum for aggregation :sum
# * <tt>:aggregation</tt> - The aggregation to use (either :count or :sum); when using :sum, :value_column must also be specified
- # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month)
+ # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
# * <tt>:limit</tt> - The number of periods to get (see :grouping)
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on
# * <tt>:live_data</tt> - Specified whether data for the current reporting period is read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache only (defaults to false)
@@ -42,7 +42,7 @@ module Kvlr #:nodoc:
# ==== Options
# * <tt>:limit</tt> - The number of periods to get
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on (<b>Beware that when you specify conditions here, caching will be disabled</b>)
- # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month)
+ # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
# * <tt>:live_data</tt> - Specified whether data for the current reporting period is read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache only (defaults to false)
def run(options = {})
ensure_valid_options(options, :run)
(DIR) diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/ClassMethods.html b/rdoc/classes/Kvlr/ReportsAsSparkline/ClassMethods.html
@@ -139,7 +139,9 @@ when using :sum, :value_column must also be specified
</li>
<li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week,
-:month)
+:month); <b>Beware that <a
+href="ClassMethods.html#M000002">reports_as_sparkline</a> treats weeks as
+starting on monday!</b>
</li>
<li><tt>:limit</tt> - The number of periods to get (see :grouping)
(DIR) diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/Report.html b/rdoc/classes/Kvlr/ReportsAsSparkline/Report.html
@@ -192,7 +192,8 @@ when using :sum, :value_column must also be specified
</li>
<li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week,
-:month)
+:month); <b>Beware that reports_as_sparkline treats weeks as starting on
+monday!</b>
</li>
<li><tt>:limit</tt> - The number of periods to get (see :grouping)
@@ -261,7 +262,8 @@ you specify conditions here, caching will be disabled</b>)
</li>
<li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week,
-:month)
+:month); <b>Beware that reports_as_sparkline treats weeks as starting on
+monday!</b>
</li>
<li><tt>:live_data</tt> - Specified whether data for the current reporting
(DIR) diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/ReportingPeriod.html b/rdoc/classes/Kvlr/ReportsAsSparkline/ReportingPeriod.html
@@ -246,19 +246,19 @@ hour/day/month/year)
onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000008-source">
<pre>
- <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/reporting_period.rb, line 53</span>
-53: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">next</span>
-54: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
-55: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
-56: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
-57: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
-58: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
-59: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
-60: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
-61: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
-62: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
-63: <span class="ruby-keyword kw">end</span>
-64: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/reporting_period.rb, line 54</span>
+54: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">next</span>
+55: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
+56: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
+57: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
+58: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
+59: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
+60: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
+61: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
+62: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
+63: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
+64: <span class="ruby-keyword kw">end</span>
+65: <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></p>
<div class="method-source-code" id="M000009-source">
<pre>
- <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/reporting_period.rb, line 67</span>
-67: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">previous</span>
-68: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
-69: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
-70: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
-71: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
-72: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
-73: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
-74: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
-75: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
-76: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
-77: <span class="ruby-keyword kw">end</span>
-78: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/reporting_period.rb, line 68</span>
+68: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">previous</span>
+69: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
+70: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
+71: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
+72: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
+73: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
+74: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
+75: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
+76: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
+77: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
+78: <span class="ruby-keyword kw">end</span>
+79: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
(DIR) diff --git a/rdoc/created.rid b/rdoc/created.rid
@@ -1 +1 @@
-Wed, 21 Jan 2009 13:09:39 +0100
+Mon, 02 Feb 2009 14:31:59 +0100
(DIR) diff --git a/rdoc/files/README_rdoc.html b/rdoc/files/README_rdoc.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Tue Jan 20 19:59:51 +0100 2009</td>
+ <td>Mon Feb 02 11:51:38 +0100 2009</td>
</tr>
</table>
</div>
@@ -93,7 +93,8 @@ aggregation :sum
:sum, :value_column must also be specified
</li>
-<li>:grouping - The period records are grouped on (:hour, :day, :week, :month)
+<li>:grouping - The period records are grouped on (:hour, :day, :week, :month);
+<b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
</li>
<li>:limit - The number of periods to get (see :grouping)
(DIR) diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/grouping_rb.html b/rdoc/files/lib/kvlr/reports_as_sparkline/grouping_rb.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Thu Jan 15 15:32:06 +0100 2009</td>
+ <td>Mon Feb 02 14:24:37 +0100 2009</td>
</tr>
</table>
</div>
(DIR) diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/report_rb.html b/rdoc/files/lib/kvlr/reports_as_sparkline/report_rb.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Tue Jan 20 12:21:17 +0100 2009</td>
+ <td>Mon Feb 02 11:52:42 +0100 2009</td>
</tr>
</table>
</div>
(DIR) diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/reporting_period_rb.html b/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>Tue Jan 20 18:50:13 +0100 2009</td>
+ <td>Mon Feb 02 14:00:53 +0100 2009</td>
</tr>
</table>
</div>
(DIR) diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline_rb.html b/rdoc/files/lib/kvlr/reports_as_sparkline_rb.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Wed Jan 21 13:08:56 +0100 2009</td>
+ <td>Mon Feb 02 11:52:04 +0100 2009</td>
</tr>
</table>
</div>
(DIR) diff --git a/spec/boot.rb b/spec/boot.rb
@@ -19,5 +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')))
-ActiveRecord::Base.establish_connection(databases['sqlite3'])
+ActiveRecord::Base.establish_connection(databases['sqlite'])
load(File.join(File.dirname(__FILE__), 'db', 'schema.rb'))
(DIR) diff --git a/spec/classes/grouping_spec.rb b/spec/classes/grouping_spec.rb
@@ -26,8 +26,8 @@ describe Kvlr::ReportsAsSparkline::Grouping do
Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%m/%d')"
end
- it 'should use DATE_FORMAT with format string "%Y/%u" for grouping :week' do
- Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%u')"
+ it 'should use YEARWEEK with mode 3 for grouping :week' do
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "YEARWEEK(created_at, 3)"
end
it 'should use DATE_FORMAT with format string "%Y/%m" for grouping :month' do
@@ -66,8 +66,8 @@ describe Kvlr::ReportsAsSparkline::Grouping do
Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at').should == "strftime('%Y/%m/%d', created_at)"
end
- it 'should use strftime with format string "%Y/%W" for grouping :week' do
- Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "strftime('%Y/%W', created_at)"
+ it 'should use date with mode "weekday 0" for grouping :week' do
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "date(created_at, 'weekday 0')"
end
it 'should use strftime with format string "%Y/%m" for grouping :month' do
@@ -94,9 +94,9 @@ describe Kvlr::ReportsAsSparkline::Grouping do
end
- it 'should split the string with "/" and increment the week by 1 for grouping :week' do
- db_string = '2008/2'
- expected = [2008, 3]
+ it 'should split the string with "-" and return teh calendar year and week for grouping :week' do
+ db_string = '2008-2-1'
+ expected = [2008, 5]
Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
end
@@ -133,7 +133,7 @@ describe Kvlr::ReportsAsSparkline::Grouping do
ActiveRecord::Base.connection.stub!(:adapter_name).and_return('MySQL')
end
- for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:week, '2008/40'], [:month, '2008/12']] do
+ for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month, '2008/12']] do
it "should split the string with '/' for grouping :#{grouping[0].to_s}" do
Kvlr::ReportsAsSparkline::Grouping.new(grouping[0]).date_parts_from_db_string(grouping[1]).should == grouping[1].split('/').map(&:to_i)
@@ -141,9 +141,9 @@ describe Kvlr::ReportsAsSparkline::Grouping do
end
- it 'should split the string with "/", set the week to 1 and increment the year by 1 if the week is greater than 52 for grouping :week' do
- db_string = '2008/53'
- expected = [2009, 1]
+ it 'should use the first 4 numbers for the year and the last 2 numbers for the week for grouping :week' do
+ db_string = '200852'
+ expected = [2008, 52]
Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
end
(DIR) diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -70,30 +70,6 @@ describe Kvlr::ReportsAsSparkline::Report do
describe "for grouping #{grouping.to_s}" do
- before(:all) do
-
- end
-
-
-
-
-
- after(:all) do
- User.destroy_all
- end
-
- after(:each) do
- Kvlr::ReportsAsSparkline::ReportCache.destroy_all
- end
-
- end
-
- end
-
- for grouping in [:hour, :day, :week, :month] do
-
- describe "for grouping #{grouping.to_s}" do
-
[true, false].each do |live_data|
describe "with :live_data = #{live_data}" do
@@ -211,7 +187,69 @@ describe Kvlr::ReportsAsSparkline::Report do
end
- after(:each) do
+ describe 'for grouping week with data ranging over two years' do
+
+ describe 'with the first week of the second year belonging to the first year' do
+
+ before(:all) do
+ User.create!(:login => 'test 1', :created_at => DateTime.new(2008, 12, 22))
+ User.create!(:login => 'test 2', :created_at => DateTime.new(2008, 12, 29))
+ User.create!(:login => 'test 3', :created_at => DateTime.new(2009, 1, 4))
+ User.create!(:login => 'test 4', :created_at => DateTime.new(2009, 1, 5))
+ User.create!(:login => 'test 5', :created_at => DateTime.new(2009, 1, 12))
+
+ Time.stub!(:now).and_return(DateTime.new(2009, 1, 25))
+ end
+
+ it 'should return correct data for aggregation :count' do
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,
+ :aggregation => :count,
+ :grouping => :week,
+ :limit => 10
+ )
+ result = @report.run.to_a
+
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 1.0
+ result[6][1].should == 2.0
+ result[5][1].should == 1.0
+ end
+
+ end
+
+ describe 'with the first week of the second year belonging to the second year' do
+
+ before(:all) do
+ User.create!(:login => 'test 1', :created_at => DateTime.new(2009, 12, 21))
+ User.create!(:login => 'test 2', :created_at => DateTime.new(2009, 12, 28))
+ User.create!(:login => 'test 3', :created_at => DateTime.new(2010, 1, 3))
+ User.create!(:login => 'test 4', :created_at => DateTime.new(2010, 1, 4))
+ User.create!(:login => 'test 5', :created_at => DateTime.new(2010, 1, 11))
+
+ Time.stub!(:now).and_return(DateTime.new(2010, 1, 25))
+ end
+
+ it 'should return correct data for aggregation :count' do
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,
+ :aggregation => :count,
+ :grouping => :week,
+ :limit => 10
+ )
+ result = @report.run.to_a
+
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 1.0
+ result[6][1].should == 2.0
+ result[5][1].should == 1.0
+ end
+
+ end
+
+ end
+
+ after do
Kvlr::ReportsAsSparkline::ReportCache.destroy_all
end