now supporting inherited models - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 627ddcc78052dbd58ea36f1d405deff5c6f8801d
(DIR) parent ce341dcb125a1b76ce526cd545a84ba52fed0e41
(HTM) Author: marcoow <marco.otte-witte@simplabs.com>
Date: Mon, 8 Dec 2008 20:45:09 +0800
now supporting inherited models
Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Diffstat:
M MIT-LICENSE | 2 +-
M Rakefile | 4 ++--
M init.rb | 1 +
M lib/kvlr/reports_as_sparkline.rb | 10 +++++-----
M lib/kvlr/reports_as_sparkline/grou… | 14 --------------
M lib/kvlr/reports_as_sparkline/repo… | 6 ++----
M spec/db/schema.rb | 1 +
M spec/models/report_method_spec.rb | 37 +++++++++++++++++++++++++++++--
D spec/other/class_methods_spec.rb | 31 -------------------------------
M spec/other/grouping_spec.rb | 32 -------------------------------
M spec/other/report_spec.rb | 10 +++-------
11 files changed, 50 insertions(+), 98 deletions(-)
---
(DIR) diff --git a/MIT-LICENSE b/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2008 Marco Otte-Witte
+Copyright (c) 2008 Marco Otte-Witte, Martin Kavalar
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
(DIR) diff --git a/Rakefile b/Rakefile
@@ -11,10 +11,10 @@ Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
end
-desc 'Generate documentation for the reports_as_sparkline plugin.'
+desc 'Generate documentation for the ReportsAsSparkline plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
- rdoc.title = 'reports_as_sparkline'
+ rdoc.title = 'ReportsAsSparkline'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
(DIR) diff --git a/init.rb b/init.rb
@@ -3,6 +3,7 @@ require 'kvlr/reports_as_sparkline'
ActiveRecord::Base.class_eval do
include Kvlr::ReportsAsSparkline
end
+
ActionView::Base.class_eval do
include Kvlr::ReportsAsSparkline::AssetTagHelper
end
(DIR) diff --git a/lib/kvlr/reports_as_sparkline.rb b/lib/kvlr/reports_as_sparkline.rb
@@ -30,13 +30,13 @@ module Kvlr #:nodoc:
# report_as_sparkline :rake, :operation => :sum
# end
def report_as_sparkline(name, options = {})
- if options.delete(:cumulate)
- report = Kvlr::ReportsAsSparkline::CumulatedReport.new(self, name, options)
- else
- report = Kvlr::ReportsAsSparkline::Report.new(self, name, options)
- end
(class << self; self; end).instance_eval do
define_method "#{name.to_s}_report".to_sym do |*args|
+ if options.delete(:cumulate)
+ report = Kvlr::ReportsAsSparkline::CumulatedReport.new(self, name, options)
+ else
+ report = Kvlr::ReportsAsSparkline::Report.new(self, name, options)
+ end
raise ArgumentError.new unless args.length == 0 || (args.length == 1 && args[0].is_a?(Hash))
report.run(args.length == 0 ? {} : args[0])
end
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sparkline/grouping.rb
@@ -29,20 +29,6 @@ module Kvlr #:nodoc:
end
end
- def next_reporting_period(period)
- return case @identifier
- when :day
- period + 1.day
- when :week
- period + 1.week
- when :month
- period += 1.month
- Date.new(period.year, period.month, 1)
- when :hour
- period + 1.hour
- end
- end
-
def first_reporting_period(limit)
return case @identifier
when :day
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_sparkline/report.rb
@@ -22,7 +22,7 @@ module Kvlr #:nodoc:
end
def run(options = {})
- ensure_valid_options(options)
+ ensure_valid_options(options, :run)
custom_conditions = options.key?(:conditions)
options.reverse_merge!(@options)
ReportCache.cached_transaction(self, options[:limit], custom_conditions) do |begin_at|
@@ -66,9 +66,7 @@ module Kvlr #:nodoc:
raise ArgumentError.new("Invalid option #{k}") unless [:limit, :conditions].include?(k)
end
end
- if options[:conditions] && !options[:conditions].is_a?(Array) && !options[:conditions].is_a?(Hash)
- raise ArgumentError.new("Invalid conditions: conditions must be specified as an Array or a Hash")
- end
+ raise ArgumentError.new("Invalid conditions: #{options[:conditions].inspect}") if options[:conditions] && !options[:conditions].is_a?(Array) && !options[:conditions].is_a?(Hash)
end
end
(DIR) diff --git a/spec/db/schema.rb b/spec/db/schema.rb
@@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version => 1) do
create_table :users, :force => true do |t|
t.string :login, :null => false
t.integer :profile_visits, :null => false, :default => 0
+ t.string :type, :null => false, :default => 'User'
t.timestamps
end
(DIR) diff --git a/spec/models/report_method_spec.rb b/spec/models/report_method_spec.rb
@@ -15,7 +15,7 @@ describe Kvlr::ReportsAsSparkline do
end
it 'should not raise an error when called with a hash' do
- lambda { User.registrations_report({ :aggregation => :sum }) }.should_not raise_error(ArgumentError)
+ lambda { User.registrations_report({ :limit => 1 }) }.should_not raise_error(ArgumentError)
end
it 'should not raise an error when called without a parameter' do
@@ -24,8 +24,41 @@ describe Kvlr::ReportsAsSparkline do
end
+ describe 'for inherited models' do
+
+ before(:all) do
+ User.create!(:login => 'test 1', :created_at => Time.now - 1.week, :profile_visits => 1)
+ User.create!(:login => 'test 2', :created_at => Time.now - 2.weeks, :profile_visits => 2)
+ SpecialUser.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :profile_visits => 3)
+ end
+
+ it 'should include all data when invoked on the base model class' do
+ result = User.registrations_report.to_a
+
+ result.length.should == 2
+ result[0][1].should == 1
+ result[1][1].should == 2
+ end
+
+ it 'should include all data when invoked on the base model class' do
+ result = SpecialUser.registrations_report.to_a
+
+ result.length.should == 1
+ result[0][1].should == 1
+ end
+
+ after(:all) do
+ User.destroy_all
+ SpecialUser.destroy_all
+ Kvlr::ReportsAsSparkline::ReportCache.destroy_all
+ end
+
+ end
+
end
class User < ActiveRecord::Base
- report_as_sparkline :registrations
+ report_as_sparkline :registrations, :cumulate => true
end
+
+class SpecialUser < User; end
(DIR) diff --git a/spec/other/class_methods_spec.rb b/spec/other/class_methods_spec.rb
@@ -1,31 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe Kvlr::ReportsAsSparkline::ClassMethods do
-
- describe "#report_as_sparkline :registrations" do
-
- it 'should add the method registrations_report to the model' do
- User.send(:report_as_sparkline, :registrations)
-
- User.methods.should include('registrations_report')
- end
-
- it 'should create a new Kvlr::ReportsAsSparkline::Report with the specified name to operate on in the created method' do
- Kvlr::ReportsAsSparkline::Report.should_receive(:new).once.with(User, :registrations, {})
-
- User.send(:report_as_sparkline, :registrations)
- end
-
- end
-
- describe "#report_as_sparkline :cumulated_registrations, { :cumulate => true }" do
-
- it 'should create a new Kvlr::ReportsAsSparkline::CumulateReport with the specified cumulate option to operate on in the created method' do
- Kvlr::ReportsAsSparkline::CumulatedReport.should_receive(:new).once.with(User, :cumulated_registrations, {})
-
- User.send(:report_as_sparkline, :cumulated_registrations, :cumulate => true)
- end
-
- end
-
-end
(DIR) diff --git a/spec/other/grouping_spec.rb b/spec/other/grouping_spec.rb
@@ -51,36 +51,4 @@ describe Kvlr::ReportsAsSparkline::Grouping do
end
- describe '.next_reporting_period' do
-
- it 'should return the first day of the month after the specified period for grouping :month' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:month)
- period = grouping.to_reporting_period(Time.now)
-
- grouping.next_reporting_period(period).should == Date.new((period + 1.month).year, (period + 1.month).month, 1)
- end
-
- it 'should return the date 1 week after the specified period for grouping :week' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:week)
- period = grouping.to_reporting_period(Time.now)
-
- grouping.next_reporting_period(period).should == Date.new((period + 1.week).year, (period + 1.week).month, (period + 1.week).day)
- end
-
- it 'should return the date 1 day after the specified period for grouping :day' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:day)
- period = grouping.to_reporting_period(Time.now)
-
- grouping.next_reporting_period(period).should == Date.new((period + 1.day).year, (period + 1.day).month, (period + 1.day).day)
- end
-
- it 'should return the date and time 1 hour after the specified period for grouping :hour' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:hour)
- period = grouping.to_reporting_period(Time.now)
-
- grouping.next_reporting_period(period).should == DateTime.new((period + 1.hour).year, (period + 1.hour).month, (period + 1.hour).day, (period + 1.hour).hour)
- end
-
- end
-
end
(DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
@@ -28,8 +28,8 @@ describe Kvlr::ReportsAsSparkline::Report do
User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :profile_visits => 3)
end
- it 'should validate the specified options' do
- @report.should_receive(:ensure_valid_options).once.with(:limit => 3)
+ it 'should validate the specified options for the :run context' do
+ @report.should_receive(:ensure_valid_options).once.with({ :limit => 3 }, :run)
result = @report.run(:limit => 3)
end
@@ -168,11 +168,7 @@ describe Kvlr::ReportsAsSparkline::Report do
describe 'for context :run' do
it 'should not raise an error if valid options are specified' do
- lambda { @report.send(:ensure_valid_options, {
- :limit => 100,
- :conditions => []
- },
- :run)
+ lambda { @report.send(:ensure_valid_options, { :limit => 100, :conditions => [] }, :run)
}.should_not raise_error(ArgumentError)
end