Introduction
Introduction Statistics Contact Development Disclaimer Help
Merge branch 'rails3x' - reportable - Fork of reportable required by WarVox, fr…
Log
Files
Refs
README
---
commit 660e1ab0067ae945b96d8e3503f55f97a5eeb73d
parent ae86ca08fc9ae2a92ca14d17c97bc3415f1f9554
Author: Martin Kavalar <[email protected]>
Date: Tue, 14 Feb 2012 10:52:48 +0100
Merge branch 'rails3x'
Conflicts:
Rakefile
Diffstat:
M Gemfile | 10 +++++-----
M generators/reportable_jquery_flot_… | 49 ++++++++++-----------------…
M generators/reportable_migration/re… | 44 +++++++++------------------…
M generators/reportable_raphael_asse… | 58 ++++++++++-----------------…
M lib/saulabs/reportable.rb | 6 ++----
M lib/saulabs/reportable/cumulated_r… | 2 +-
M lib/saulabs/reportable/railtie.rb | 13 ++++++++++++-
M lib/saulabs/reportable/report_cach… | 7 ++++---
M lib/saulabs/reportable/report_tag_… | 8 ++++----
M lib/saulabs/reportable/result_set.… | 10 ++++++++--
D rails/init.rb | 11 -----------
D spec/boot.rb | 22 ----------------------
M spec/classes/report_cache_spec.rb | 29 +++++++++++++++++------------
M spec/classes/report_spec.rb | 8 ++++----
M spec/other/report_method_spec.rb | 17 ++++++++++-------
M spec/other/report_tag_helper_spec.… | 2 ++
M spec/spec_helper.rb | 43 ++++++++++++++++++++++-------…
17 files changed, 146 insertions(+), 193 deletions(-)
---
diff --git a/Gemfile b/Gemfile
@@ -1,9 +1,9 @@
source "http://rubygems.org"
-gem 'rails', '~> 2.3.0'
-gem 'activerecord', '~> 2.3.0', :require => 'active_record'
-gem 'activesupport', '~> 2.3.0', :require => 'active_support'
-gem 'actionpack', '~> 2.3.0', :require => 'action_pack'
+gem 'rails', '~> 3.2.0'
+gem 'activerecord', '~> 3.2.0', :require => 'active_record'
+gem 'activesupport', '~> 3.2.0', :require => 'active_support'
+gem 'actionpack', '~> 3.2.0', :require => 'action_pack'
gem 'sqlite3-ruby', '>= 1.2.0'
gem 'mysql', '>= 2.8.0'
@@ -11,7 +11,7 @@ gem 'pg', '>= 0.9.0'
gem 'tzinfo', '>= 0.3.0'
gem 'rake', '>= 0.8.7'
-gem 'rspec', '~> 1.3.0'
+gem 'rspec', '~> 2.8.0'
gem 'simplecov'
gem 'excellent', '>= 1.5.4'
gem 'yard', '>= 0.4.0'
diff --git a/generators/reportable_jquery_flot_assets/reportable_jquery_flot_as…
@@ -1,37 +1,18 @@
-if Saulabs::Reportable::RailsAdapter::IS_RAILS3
-
- class ReportableJqueryFlotAssetsGenerator < Rails::Generators::Base
-
- include Rails::Generators::Actions
-
- def create_jquery_flot_file
- empty_directory('public/javascripts')
- copy_file(
- File.join(File.dirname(__FILE__), 'templates', 'jquery.flot.min.js'),
- 'public/javascripts/jquery.flot.min.js'
- )
- copy_file(
- File.join(File.dirname(__FILE__), 'templates', 'excanvas.min.js'),
- 'public/javascripts/excanvas.min.js'
- )
- readme(File.join(File.dirname(__FILE__), 'templates', 'NOTES'))
- end
-
- end
-
-else
-
- class ReportableJqueryFlotAssetsGenerator < Rails::Generator::Base
-
- def manifest
- record do |m|
- m.directory('public/javascripts')
- m.file('jquery.flot.min.js', 'public/javascripts/jquery.flot.min.js')
- m.file('excanvas.min.js', 'public/javascripts/excanvas.min.js')
- m.readme('NOTES')
- end
- end
-
+class ReportableJqueryFlotAssetsGenerator < Rails::Generators::Base
+
+ include Rails::Generators::Actions
+
+ def create_jquery_flot_file
+ empty_directory('public/javascripts')
+ copy_file(
+ File.join(File.dirname(__FILE__), 'templates', 'jquery.flot.min.js'),
+ 'public/javascripts/jquery.flot.min.js'
+ )
+ copy_file(
+ File.join(File.dirname(__FILE__), 'templates', 'excanvas.min.js'),
+ 'public/javascripts/excanvas.min.js'
+ )
+ readme(File.join(File.dirname(__FILE__), 'templates', 'NOTES'))
end
end
diff --git a/generators/reportable_migration/reportable_migration_generator.rb …
@@ -1,40 +1,20 @@
-if Saulabs::Reportable::RailsAdapter::IS_RAILS3
+class ReportableMigrationGenerator < Rails::Generators::Base
- class ReportableMigrationGenerator < Rails::Generators::Base
-
- include Rails::Generators::Migration
-
- def create_migration
- migration_template(
- File.join(File.dirname(__FILE__), 'templates', 'migration.rb'),
- 'db/migrate/create_reportable_cache.rb'
- )
- end
-
- def self.next_migration_number(dirname)
- if ActiveRecord::Base.timestamped_migrations
- Time.now.utc.strftime("%Y%m%d%H%M%S")
- else
- "%.3d" % (current_migration_number(dirname) + 1)
- end
- end
+ include Rails::Generators::Migration
+ def create_migration
+ migration_template(
+ File.join(File.dirname(__FILE__), 'templates', 'migration.rb'),
+ 'db/migrate/create_reportable_cache.rb'
+ )
end
-else
-
- class ReportableMigrationGenerator < Rails::Generator::Base
-
- def manifest
- record do |m|
- m.migration_template('migration.rb', 'db/migrate')
- end
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
end
-
- def file_name
- 'create_reportable_cache'
- end
-
end
end
diff --git a/generators/reportable_raphael_assets/reportable_raphael_assets_gen…
@@ -1,42 +1,22 @@
-if Saulabs::Reportable::RailsAdapter::IS_RAILS3
-
- class ReportableRaphaelAssetsGenerator < Rails::Generators::Base
-
- include Rails::Generators::Actions
-
- def create_raphael_file
- empty_directory('public/javascripts')
- copy_file(
- File.join(File.dirname(__FILE__), 'templates', 'raphael.min.js'),
- 'public/javascripts/raphael.min.js'
- )
- copy_file(
- File.join(File.dirname(__FILE__), 'templates', 'g.raphael.min.js'),
- 'public/javascripts/g.raphael.min.js'
- )
- copy_file(
- File.join(File.dirname(__FILE__), 'templates', 'g.line.min.js'),
- 'public/javascripts/g.line.min.js'
- )
- readme(File.join(File.dirname(__FILE__), 'templates', 'NOTES'))
- end
-
- end
-
-else
-
- class ReportableRaphaelAssetsGenerator < Rails::Generator::Base
-
- def manifest
- record do |m|
- m.directory('public/javascripts')
- m.file('raphael.min.js', 'public/javascripts/raphael.min.js')
- m.file('g.raphael.min.js', 'public/javascripts/g.raphael.min.js')
- m.file('g.line.min.js', 'public/javascripts/g.line.min.js')
- m.readme('NOTES')
- end
- end
-
+class ReportableRaphaelAssetsGenerator < Rails::Generators::Base
+
+ include Rails::Generators::Actions
+
+ def create_raphael_file
+ empty_directory('public/javascripts')
+ copy_file(
+ File.join(File.dirname(__FILE__), 'templates', 'raphael.min.js'),
+ 'public/javascripts/raphael.min.js'
+ )
+ copy_file(
+ File.join(File.dirname(__FILE__), 'templates', 'g.raphael.min.js'),
+ 'public/javascripts/g.raphael.min.js'
+ )
+ copy_file(
+ File.join(File.dirname(__FILE__), 'templates', 'g.line.min.js'),
+ 'public/javascripts/g.line.min.js'
+ )
+ readme(File.join(File.dirname(__FILE__), 'templates', 'NOTES'))
end
end
diff --git a/lib/saulabs/reportable.rb b/lib/saulabs/reportable.rb
@@ -1,5 +1,7 @@
+
require 'saulabs/reportable/report'
require 'saulabs/reportable/cumulated_report'
+require 'saulabs/reportable/railtie'
module Saulabs
@@ -9,10 +11,6 @@ module Saulabs
#
module RailsAdapter
- IS_RAILS3 = defined?(Rails) && Rails::VERSION::MAJOR >= 3
-
- require 'saulabs/reportable/railtie' if IS_RAILS3
-
# Extends the {Saulabs::Reportable::ClassMethods#reportable} method into…
#
def self.included(base)
diff --git a/lib/saulabs/reportable/cumulated_report.rb b/lib/saulabs/reportabl…
@@ -25,7 +25,7 @@ module Saulabs
first_reporting_period = ReportingPeriod.first(options[:grouping], o…
acc = initial_cumulative_value(first_reporting_period.date_time, opt…
result = []
- data.each do |element|
+ data.to_a.each do |element|
acc += element[1].to_f
result << [element[0], acc]
end
diff --git a/lib/saulabs/reportable/railtie.rb b/lib/saulabs/reportable/railtie…
@@ -1,4 +1,5 @@
require 'saulabs/reportable'
+require 'saulabs/reportable/report_tag_helper'
require 'rails'
module Saulabs
@@ -10,7 +11,17 @@ module Saulabs
GEM_ROOT = File.join(File.dirname(__FILE__), '..', '..', '..')
initializer 'saulabs.reportable.initialization' do
- require File.join(GEM_ROOT, 'rails', 'init')
+ ActiveSupport.on_load :active_record do
+ ActiveRecord::Base.class_eval do
+ include Saulabs::Reportable::RailsAdapter
+ end
+ end
+ ActiveSupport.on_load :action_view do
+ ActionView::Base.class_eval do
+ include Saulabs::Reportable::ReportTagHelper
+ end
+ end
+
end
generators do
diff --git a/lib/saulabs/reportable/report_cache.rb b/lib/saulabs/reportable/re…
@@ -1,5 +1,6 @@
require 'saulabs/reportable/reporting_period'
require 'saulabs/reportable/result_set'
+require 'active_record'
module Saulabs
@@ -10,7 +11,7 @@ module Saulabs
#
class ReportCache < ActiveRecord::Base
- set_table_name :reportable_cache
+ self.table_name = :reportable_cache
validates_presence_of :model_name
validates_presence_of :report_name
@@ -63,7 +64,7 @@ module Saulabs
# @option options [DateTime, Boolean] :end_date (false)
# when specified, the report will only include data for the +:limit+ r…
#
- # @return [Array<Array<DateTime, Float>>]
+ # @return [ResultSet<Array<DateTime, Float>>]
# the result of the report as pairs of {DateTime}s and {Float}s
#
def self.process(report, options, &block)
@@ -122,7 +123,7 @@ module Saulabs
elsif conditions.is_a?(Hash) && conditions.any?
conditions.map.sort{|x,y|x.to_s<=>y.to_s}.flatten.join
else
- conditions.to_s
+ conditions.empty? ? '' : conditions.to_s
end
end
diff --git a/lib/saulabs/reportable/report_tag_helper.rb b/lib/saulabs/reportab…
@@ -37,7 +37,7 @@ module Saulabs
#
def google_report_tag(data, options = {})
options.reverse_merge!(Config.google_options)
- data = data.collect { |d| d[1] }
+ data = data.to_a.collect { |d| d[1] }
labels = ''
unless options[:labels].empty?
chxr = {}
@@ -93,8 +93,8 @@ module Saulabs
var graph = Raphael('#{options[:dom_id]}');
graph.g.linechart(
-10, 4, #{options[:width]}, #{options[:height]},
- #{(0..data.size).to_a.to_json},
- #{data.map { |d| d[1].send(:eval, options[:format]) }.to_json},
+ #{(0..data.to_a.size).to_a.to_json},
+ #{data.to_a.map { |d| d[1].send(:eval, options[:format]) }.to_json…
#{raphael_options.to_json}
).hover(function() {
this.disc = graph.g.disc(this.x, this.y, 3).attr({fill: "#{options…
@@ -146,7 +146,7 @@ module Saulabs
%Q{<div id="#{options[:dom_id]}" style="width:#{options[:width]}px;hei…
<script type="text\/javascript" charset="utf-8">
$(function() {
- var set = #{data.map{|d| d[1] }.to_json},
+ var set = #{data.to_a.map{|d| d[1] }.to_json},
data = [];
for (var i = 0; i < set.length; i++) {
data.push([i, set[i]]);
diff --git a/lib/saulabs/reportable/result_set.rb b/lib/saulabs/reportable/resu…
@@ -8,7 +8,7 @@ module Saulabs
# the name of the model and the report the result set
# was generated from.
#
- class ResultSet < ::Array
+ class ResultSet
# the name of the model the result set is based on
#
@@ -18,6 +18,12 @@ module Saulabs
#
attr_reader :report_name
+ # array representation of the result
+ #
+ def to_a
+ @results
+ end
+
# Initializes a new result set.
#
# @param [Array] array
@@ -28,7 +34,7 @@ module Saulabs
# the name of the report the result is based on
#
def initialize(array, model_name, report_name)
- super(array)
+ @results = array
@model_name = model_name
@report_name = report_name.to_s
end
diff --git a/rails/init.rb b/rails/init.rb
@@ -1,11 +0,0 @@
-require 'action_view'
-require 'saulabs/reportable'
-require 'saulabs/reportable/report_tag_helper'
-
-ActiveRecord::Base.class_eval do
- include Saulabs::Reportable::RailsAdapter
-end
-
-ActionView::Base.class_eval do
- include Saulabs::Reportable::ReportTagHelper
-end
diff --git a/spec/boot.rb b/spec/boot.rb
@@ -1,22 +0,0 @@
-plugin_root = File.join(File.dirname(__FILE__), '..')
-
-$:.unshift "#{plugin_root}/lib"
-
-Bundler.require
-require 'initializer'
-
-RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + '/../') unless defined?…
-Rails::Initializer.run(:set_load_path)
-Rails::Initializer.run(:set_autoload_paths)
-Rails::Initializer.run(:initialize_time_zone) do |config|
- config.time_zone = 'Pacific Time (US & Canada)'
-end
-
-require File.join(File.dirname(__FILE__), '..', 'rails', 'init.rb')
-
-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…
-ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'sqlite3'])
-load(File.join(File.dirname(__FILE__), 'db', 'schema.rb'))
diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec…
@@ -214,7 +214,7 @@ describe Saulabs::Reportable::ReportCache do
@report.name.to_s,
@report.options[:grouping].identifier.to_s,
@report.aggregation.to_s,
- @report.options[:conditions].to_s,
+ '',
Saulabs::Reportable::ReportingPeriod.first(@report.options[:grouping…
],
:limit => 10,
@@ -235,7 +235,7 @@ describe Saulabs::Reportable::ReportCache do
@report.name.to_s,
@report.options[:grouping].identifier.to_s,
@report.aggregation.to_s,
- @report.options[:conditions].to_s,
+ '',
Saulabs::Reportable::ReportingPeriod.first(@report.options[:grouping…
Saulabs::Reportable::ReportingPeriod.new(@report.options[:grouping],…
],
@@ -248,8 +248,7 @@ describe Saulabs::Reportable::ReportCache do
it "should read existing data from the cache for the correct grouping if o…
grouping = Saulabs::Reportable::Grouping.new(:month)
- Saulabs::Reportable::ReportCache.should_receive(:find).once.with(
- :all,
+ Saulabs::Reportable::ReportCache.should_receive(:all).once.with(
:conditions => [
%w(model_name report_name grouping aggregation conditions).map do |c…
"#{Saulabs::Reportable::ReportCache.connection.quote_column_name(c…
@@ -258,7 +257,7 @@ describe Saulabs::Reportable::ReportCache do
@report.name.to_s,
grouping.identifier.to_s,
@report.aggregation.to_s,
- @report.options[:conditions].to_s,
+ '',
Saulabs::Reportable::ReportingPeriod.first(grouping, 10).date_time
],
:limit => 10,
@@ -279,6 +278,11 @@ describe Saulabs::Reportable::ReportCache do
describe '.serialize_conditions' do
+ it 'should serialize empty conditions correctly' do
+ result = Saulabs::Reportable::ReportCache.send(:serialize_conditions, [])
+ result.should eql('')
+ end
+
it 'should serialize a conditions array correctly' do
result = Saulabs::Reportable::ReportCache.send(:serialize_conditions, ['…
result.should eql('active = ? AND gender = ?truemale')
@@ -334,7 +338,7 @@ describe Saulabs::Reportable::ReportCache do
end
it 'should save the created Saulabs::Reportable::ReportCache' do
- @cached.should_receive(:save!).once
+ @cached.should_receive(:save!)
Saulabs::Reportable::ReportCache.send(:prepare_result, @new_data, [], @r…
end
@@ -342,16 +346,17 @@ describe Saulabs::Reportable::ReportCache do
it 'should return an array of arrays of Dates and Floats' do
result = Saulabs::Reportable::ReportCache.send(:prepare_result, @new_dat…
- result.should be_kind_of(Array)
- result[0].should be_kind_of(Array)
- result[0][0].should be_kind_of(Date)
- result[0][1].should be_kind_of(Float)
+ result.should be_kind_of(Saulabs::Reportable::ResultSet)
+ result.to_a.should be_kind_of(Array)
+ result.to_a[0].should be_kind_of(Array)
+ result.to_a[0][0].should be_kind_of(Date)
+ result.to_a[0][1].should be_kind_of(Float)
end
describe 'with :live_data = false' do
before do
- @result = Saulabs::Reportable::ReportCache.send(:prepare_result, @new_…
+ @result = Saulabs::Reportable::ReportCache.send(:prepare_result, @new_…
end
it 'should return an array of length :limit' do
@@ -368,7 +373,7 @@ describe Saulabs::Reportable::ReportCache do
before do
options = @report.options.merge(:live_data => true)
- @result = Saulabs::Reportable::ReportCache.send(:prepare_result, @new_…
+ @result = Saulabs::Reportable::ReportCache.send(:prepare_result, @new_…
end
it 'should return an array of length (:limit + 1)' do
diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -56,13 +56,13 @@ describe Saulabs::Reportable::Report do
it 'should return an array of the same length as the specified limit when …
@report = Saulabs::Reportable::Report.new(User, :cumulated_registrations…
- @report.run.length.should == 10
+ @report.run.to_a.length.should == 10
end
it 'should return an array of the same length as the specified limit + 1 w…
@report = Saulabs::Reportable::Report.new(User, :cumulated_registrations…
- @report.run.length.should == 11
+ @report.run.to_a.length.should == 11
end
for grouping in [:hour, :day, :week, :month] do
@@ -98,7 +98,7 @@ describe Saulabs::Reportable::Report do
:limit => 10,
:end_date => @end_date
)
- @result = @report.run
+ @result = @report.run.to_a
end
it "should start with the reporting period (end_date - limit.#{gro…
@@ -127,7 +127,7 @@ describe Saulabs::Reportable::Report do
:limit => 10,
:live_data => live_data
)
- @result = @report.run
+ @result = @report.run.to_a
end
it "should be an array starting reporting period (Time.now - lim…
diff --git a/spec/other/report_method_spec.rb b/spec/other/report_method_spec.rb
@@ -1,7 +1,17 @@
require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))),'spec…
describe Saulabs::Reportable do
+
+ ActiveRecord::Base.class_eval do
+ include Saulabs::Reportable::RailsAdapter
+ end
+
+ class User < ActiveRecord::Base
+ reportable :registrations, :limit => 10
+ end
+ class SpecialUser < User; end
+
before(:all) do
User.create!(:login => 'test 1', :created_at => Time.now - 1.days, :profi…
User.create!(:login => 'test 2', :created_at => Time.now - 2.days, :profil…
@@ -59,10 +69,3 @@ describe Saulabs::Reportable do
end
-class User < ActiveRecord::Base
-
- reportable :registrations, :limit => 10
-
-end
-
-class SpecialUser < User; end
diff --git a/spec/other/report_tag_helper_spec.rb b/spec/other/report_tag_helpe…
@@ -1,5 +1,7 @@
require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))),'spec…
+require 'reportable/report_tag_helper'
+
describe Saulabs::Reportable::ReportTagHelper do
before do
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
@@ -1,19 +1,39 @@
ENV['RAILS_ENV'] = 'test'
require 'rubygems'
-require 'bundler'
-Bundler.setup
-
-require File.join(File.dirname(__FILE__), 'boot')
-
-class User < ActiveRecord::Base; end
-
-class YieldMatchException < Exception; end
+require 'bundler/setup'
+require 'active_record'
+require 'active_record/version'
+require 'active_support'
begin
require 'ruby-debug'
- Debugger.start
- Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
+ # Debugger.start
+ # Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
rescue LoadError
# ruby-debug wasn't available so neither can the debugging be
-end
-\ No newline at end of file
+end
+
+ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
+
+$LOAD_PATH << File.join(ROOT, 'lib')
+$LOAD_PATH << File.join(ROOT, 'lib/saulabs')
+
+require File.join(ROOT, 'lib', 'saulabs', 'reportable.rb')
+
+# Rails::Initializer.run(:set_load_path)
+# Rails::Initializer.run(:set_autoload_paths)
+# Rails::Initializer.run(:initialize_time_zone) do |config|
+# config.time_zone = 'Pacific Time (US & Canada)'
+# end
+
+FileUtils.mkdir_p File.join(File.dirname(__FILE__), 'log')
+ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__F…
+
+databases = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'databa…
+ActiveRecord::Base.establish_connection(databases[ENV['DB'] || 'sqlite3'])
+load(File.join(File.dirname(__FILE__), 'db', 'schema.rb'))
+
+class User < ActiveRecord::Base; end
+
+class YieldMatchException < Exception; end
You are viewing proxied material from jay.scot. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.