Commit 9d92cbf32beb2da0a0fcefa815294c6af7c4f72a

Authored by Andrew Kane
1 parent 3cd62d0b
Exists in cumulative_sum

Only limit query time range if carry_forward: false

lib/groupdate/magic.rb
... ... @@ -72,18 +72,11 @@ module Groupdate
72 72 if options[:series] == false
73 73 group
74 74 else
75   - relation =
76   - if time_range.is_a?(Range)
77   - # doesn't matter whether we include the end of a ... range - it will be excluded later
78   - group.where("#{column} >= ? AND #{column} <= ?", time_range.first, time_range.last)
79   - else
80   - group.where("#{column} IS NOT NULL")
81   - end
82   -
83 75 # TODO do not change object state
84 76 @group_index = group.group_values.size - 1
  77 + @column = column
85 78  
86   - Groupdate::Series.new(self, relation)
  79 + Groupdate::Series.new(self, group)
87 80 end
88 81 end
89 82  
... ... @@ -111,6 +104,16 @@ module Groupdate
111 104 lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) }
112 105 end
113 106  
  107 + if !options[:carry_forward]
  108 + relation =
  109 + if time_range.is_a?(Range)
  110 + # doesn't matter whether we include the end of a ... range - it will be excluded later
  111 + relation.where("#{@column} >= ? AND #{@column} <= ?", time_range.first, time_range.last)
  112 + else
  113 + relation.where("#{@column} IS NOT NULL")
  114 + end
  115 + end
  116 +
114 117 count =
115 118 begin
116 119 if method == :custom
... ...
lib/groupdate/series.rb
... ... @@ -10,7 +10,7 @@ module Groupdate
10 10 def cumulative_sum
11 11 magic.options[:carry_forward] = true
12 12 sql =
13   - if magic.send(:postgresql?, connection.adapter_name)
  13 + if magic.send(:postgresql?, relation.connection.adapter_name)
14 14 "SUM(COUNT(id)) OVER (ORDER BY #{relation.group_values[0]})::integer"
15 15 else
16 16 raise "`cumulative_sum` not supported with MySQL"
... ...
test/test_helper.rb
... ... @@ -641,11 +641,10 @@ module TestGroupdate
641 641 create_user "2013-05-01 00:00:00 UTC"
642 642 create_user "2013-05-01 00:00:00 UTC"
643 643 expected = {
644   - utc.parse("2012-01-01 00:00:00 UTC") => 1,
645 644 utc.parse("2013-01-01 00:00:00 UTC") => 3,
646 645 utc.parse("2014-01-01 00:00:00 UTC") => 3
647 646 }
648   - assert_equal expected, User.group_by_year(:created_at, last: 3).cumulative_sum
  647 + assert_equal expected, User.group_by_year(:created_at, last: 2).cumulative_sum
649 648 end
650 649  
651 650 # helpers
... ...