| now supporting inherited models - reportable - Fork of reportable required by W… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 627ddcc78052dbd58ea36f1d405deff5c6f8801d | |
| parent ce341dcb125a1b76ce526cd545a84ba52fed0e41 | |
| Author: marcoow <[email protected]> | |
| Date: Mon, 8 Dec 2008 20:45:09 +0800 | |
| now supporting inherited models | |
| Signed-off-by: Marco Otte-Witte <[email protected]> | |
| 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(-) | |
| --- | |
| 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 | |
| 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') | |
| 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 | |
| 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, o… | |
| - 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, nam… | |
| + else | |
| + report = Kvlr::ReportsAsSparkline::Report.new(self, name, option… | |
| + end | |
| raise ArgumentError.new unless args.length == 0 || (args.length ==… | |
| report.run(args.length == 0 ? {} : args[0]) | |
| end | |
| diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sp… | |
| @@ -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 | |
| diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar… | |
| @@ -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_condition… | |
| @@ -66,9 +66,7 @@ module Kvlr #:nodoc: | |
| raise ArgumentError.new("Invalid option #{k}") unless [:limit,… | |
| end | |
| end | |
| - if options[:conditions] && !options[:conditions].is_a?(Array) && !op… | |
| - raise ArgumentError.new("Invalid conditions: conditions must be sp… | |
| - end | |
| + raise ArgumentError.new("Invalid conditions: #{options[:conditions].… | |
| end | |
| end | |
| 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 | |
| diff --git a/spec/models/report_method_spec.rb b/spec/models/report_method_spec… | |
| @@ -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_no… | |
| + lambda { User.registrations_report({ :limit => 1 }) }.should_not raise_e… | |
| 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, :pro… | |
| + User.create!(:login => 'test 2', :created_at => Time.now - 2.weeks, :pro… | |
| + SpecialUser.create!(:login => 'test 3', :created_at => Time.now - 2.week… | |
| + 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 | |
| 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 specifie… | |
| - Kvlr::ReportsAsSparkline::Report.should_receive(:new).once.with(User, :r… | |
| - | |
| - User.send(:report_as_sparkline, :registrations) | |
| - end | |
| - | |
| - end | |
| - | |
| - describe "#report_as_sparkline :cumulated_registrations, { :cumulate => true… | |
| - | |
| - it 'should create a new Kvlr::ReportsAsSparkline::CumulateReport with the … | |
| - Kvlr::ReportsAsSparkline::CumulatedReport.should_receive(:new).once.with… | |
| - | |
| - User.send(:report_as_sparkline, :cumulated_registrations, :cumulate => t… | |
| - end | |
| - | |
| - end | |
| - | |
| -end | |
| 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 fo… | |
| - grouping = Kvlr::ReportsAsSparkline::Grouping.new(:month) | |
| - period = grouping.to_reporting_period(Time.now) | |
| - | |
| - grouping.next_reporting_period(period).should == Date.new((period + 1.mo… | |
| - end | |
| - | |
| - it 'should return the date 1 week after the specified period for grouping … | |
| - grouping = Kvlr::ReportsAsSparkline::Grouping.new(:week) | |
| - period = grouping.to_reporting_period(Time.now) | |
| - | |
| - grouping.next_reporting_period(period).should == Date.new((period + 1.we… | |
| - end | |
| - | |
| - it 'should return the date 1 day after the specified period for grouping :… | |
| - grouping = Kvlr::ReportsAsSparkline::Grouping.new(:day) | |
| - period = grouping.to_reporting_period(Time.now) | |
| - | |
| - grouping.next_reporting_period(period).should == Date.new((period + 1.da… | |
| - end | |
| - | |
| - it 'should return the date and time 1 hour after the specified period for … | |
| - grouping = Kvlr::ReportsAsSparkline::Grouping.new(:hour) | |
| - period = grouping.to_reporting_period(Time.now) | |
| - | |
| - grouping.next_reporting_period(period).should == DateTime.new((period + … | |
| - end | |
| - | |
| - end | |
| - | |
| end | |
| 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, :p… | |
| 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 … | |
| 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, :conditi… | |
| }.should_not raise_error(ArgumentError) | |
| end | |