diff --git a/lib/groupdate/magic.rb b/lib/groupdate/magic.rb index 0fe6253..6f2bb31 100644 --- a/lib/groupdate/magic.rb +++ b/lib/groupdate/magic.rb @@ -72,18 +72,11 @@ module Groupdate if options[:series] == false group else - relation = - if time_range.is_a?(Range) - # doesn't matter whether we include the end of a ... range - it will be excluded later - group.where("#{column} >= ? AND #{column} <= ?", time_range.first, time_range.last) - else - group.where("#{column} IS NOT NULL") - end - # TODO do not change object state @group_index = group.group_values.size - 1 + @column = column - Groupdate::Series.new(self, relation) + Groupdate::Series.new(self, group) end end @@ -111,6 +104,16 @@ module Groupdate lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) } end + if !options[:carry_forward] + relation = + if time_range.is_a?(Range) + # doesn't matter whether we include the end of a ... range - it will be excluded later + relation.where("#{@column} >= ? AND #{@column} <= ?", time_range.first, time_range.last) + else + relation.where("#{@column} IS NOT NULL") + end + end + count = begin if method == :custom diff --git a/lib/groupdate/series.rb b/lib/groupdate/series.rb index 97a1493..d81d2c4 100644 --- a/lib/groupdate/series.rb +++ b/lib/groupdate/series.rb @@ -10,7 +10,7 @@ module Groupdate def cumulative_sum magic.options[:carry_forward] = true sql = - if magic.send(:postgresql?, connection.adapter_name) + if magic.send(:postgresql?, relation.connection.adapter_name) "SUM(COUNT(id)) OVER (ORDER BY #{relation.group_values[0]})::integer" else raise "`cumulative_sum` not supported with MySQL" diff --git a/test/test_helper.rb b/test/test_helper.rb index 46d8b14..bd9ca00 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -641,11 +641,10 @@ module TestGroupdate create_user "2013-05-01 00:00:00 UTC" create_user "2013-05-01 00:00:00 UTC" expected = { - utc.parse("2012-01-01 00:00:00 UTC") => 1, utc.parse("2013-01-01 00:00:00 UTC") => 3, utc.parse("2014-01-01 00:00:00 UTC") => 3 } - assert_equal expected, User.group_by_year(:created_at, last: 3).cumulative_sum + assert_equal expected, User.group_by_year(:created_at, last: 2).cumulative_sum end # helpers -- libgit2 0.21.0