Commit 05223f08f879cfd596b060aa33e28f8f0e1b4b1d

Authored by Andrew Kane
1 parent dc8fc08d

Fixed enumerable for ActiveRecord 3.1

lib/groupdate.rb
1 1 require "active_support/time"
2 2 require "groupdate/version"
  3 +
  4 +module Groupdate
  5 + FIELDS = [:second, :minute, :hour, :day, :week, :month, :year, :day_of_week, :hour_of_day]
  6 + METHODS = FIELDS.map{|v| :"group_by_#{v}" }
  7 +
  8 + mattr_accessor :week_start, :day_start, :time_zone
  9 + self.week_start = :sun
  10 + self.day_start = 0
  11 +end
  12 +
3 13 require "groupdate/magic"
4 14 begin
5 15 require "active_record"
... ... @@ -8,9 +18,3 @@ rescue LoadError
8 18 end
9 19 require "groupdate/active_record" if defined?(ActiveRecord)
10 20 require "groupdate/enumerable"
11   -
12   -module Groupdate
13   - mattr_accessor :week_start, :day_start, :time_zone
14   - self.week_start = :sun
15   - self.day_start = 0
16   -end
... ...
lib/groupdate/active_record.rb
... ... @@ -3,6 +3,25 @@ require "groupdate/scopes"
3 3  
4 4 ActiveRecord::Base.send(:extend, Groupdate::Scopes)
5 5  
  6 +module ActiveRecord
  7 + class Relation
  8 +
  9 + if ActiveRecord::VERSION::MAJOR == 3 and ActiveRecord::VERSION::MINOR < 2
  10 +
  11 + def method_missing_with_hack(method, *args, &block)
  12 + if Groupdate::METHODS.include?(method)
  13 + scoping { @klass.send(method, *args, &block) }
  14 + else
  15 + method_missing_without_hack(method, *args, &block)
  16 + end
  17 + end
  18 + alias_method_chain :method_missing, :hack
  19 +
  20 + end
  21 +
  22 + end
  23 +end
  24 +
6 25 # hack for **unfixed** rails issue
7 26 # https://github.com/rails/rails/issues/7121
8 27 module ActiveRecord
... ...
lib/groupdate/enumerable.rb
1 1 module Enumerable
2 2  
3   - # skip due to ActiveRecord bug
4   - unless defined?(ActiveRecord) and ActiveRecord::VERSION::MAJOR == 3 and ActiveRecord::VERSION::MINOR < 2
5   - [:second, :minute, :hour, :day, :week, :month, :year, :day_of_week, :hour_of_day].each do |field|
6   - define_method :"group_by_#{field}" do |options = {}, &block|
7   - Groupdate::Magic.new(field, options).group_by(self, &block)
8   - end
  3 + Groupdate::FIELDS.each do |field|
  4 + define_method :"group_by_#{field}" do |options = {}, &block|
  5 + Groupdate::Magic.new(field, options).group_by(self, &block)
9 6 end
10 7 end
11 8  
... ...
lib/groupdate/scopes.rb
... ... @@ -5,7 +5,7 @@ require &quot;active_record&quot;
5 5 module Groupdate
6 6 module Scopes
7 7  
8   - [:second, :minute, :hour, :day, :week, :month, :year, :day_of_week, :hour_of_day].each do |field|
  8 + Groupdate::FIELDS.each do |field|
9 9 define_method :"group_by_#{field}" do |*args|
10 10 args = args.dup
11 11 options = args[-1].is_a?(Hash) ? args.pop : {}
... ...
test/enumerable_test.rb
1 1 require_relative "test_helper"
2 2  
3   -unless ActiveRecord::VERSION::MAJOR == 3 and ActiveRecord::VERSION::MINOR < 2
4   - class TestEnumerable < Minitest::Unit::TestCase
5   - include TestGroupdate
  3 +class TestEnumerable < Minitest::Unit::TestCase
  4 + include TestGroupdate
6 5  
7   - def test_enumerable
8   - user_a = User.new(created_at: utc.parse("2014-01-21"))
9   - user_b = User.new(created_at: utc.parse("2014-03-14"))
10   - expected = {
11   - utc.parse("2014-01-01") => [user_a],
12   - utc.parse("2014-02-01") => [],
13   - utc.parse("2014-03-01") => [user_b]
14   - }
15   - assert_equal expected, [user_a, user_b].group_by_month(&:created_at)
16   - end
17   -
18   - def call_method(method, field, options)
19   - Hash[ User.all.to_a.send(:"group_by_#{method}", options){|u| u.send(field) }.map{|k, v| [k, v.size] } ]
20   - end
  6 + def test_enumerable
  7 + user_a = User.new(created_at: utc.parse("2014-01-21"))
  8 + user_b = User.new(created_at: utc.parse("2014-03-14"))
  9 + expected = {
  10 + utc.parse("2014-01-01") => [user_a],
  11 + utc.parse("2014-02-01") => [],
  12 + utc.parse("2014-03-01") => [user_b]
  13 + }
  14 + assert_equal expected, [user_a, user_b].group_by_month(&:created_at)
  15 + end
21 16  
  17 + def call_method(method, field, options)
  18 + Hash[ User.all.to_a.send(:"group_by_#{method}", options){|u| u.send(field) }.map{|k, v| [k, v.size] } ]
22 19 end
  20 +
23 21 end
... ...