Commit 6f298a9e45191982caef2458aa000027161b024a
Exists in
master
and in
12 other branches
Merge branch 'master' into dynamic_grouping
Showing
6 changed files
with
40 additions
and
14 deletions
Show diff stats
.travis.yml
README.md
... | ... | @@ -15,7 +15,7 @@ Works with Rails 3.1+ |
15 | 15 | |
16 | 16 | Supports PostgreSQL and MySQL, plus arrays and hashes |
17 | 17 | |
18 | -[](https://travis-ci.org/ankane/groupdate) | |
18 | +[](https://travis-ci.org/ankane/groupdate) | |
19 | 19 | |
20 | 20 | :cupid: Goes hand in hand with [Chartkick](http://ankane.github.io/chartkick/) |
21 | 21 | ... | ... |
groupdate.gemspec
... | ... | @@ -30,6 +30,6 @@ Gem::Specification.new do |spec| |
30 | 30 | spec.add_development_dependency "activerecord-jdbcmysql-adapter" |
31 | 31 | else |
32 | 32 | spec.add_development_dependency "pg" |
33 | - spec.add_development_dependency "mysql2" | |
33 | + spec.add_development_dependency "mysql2", "~> 0.3.20" | |
34 | 34 | end |
35 | 35 | end | ... | ... |
lib/groupdate/magic.rb
... | ... | @@ -108,7 +108,7 @@ module Groupdate |
108 | 108 | |
109 | 109 | def perform(relation, method, *args, &block) |
110 | 110 | # undo reverse since we do not want this to appear in the query |
111 | - reverse = relation.reverse_order_value | |
111 | + reverse = relation.send(:reverse_order_value) | |
112 | 112 | if reverse |
113 | 113 | relation = relation.except(:reverse_order) |
114 | 114 | end |
... | ... | @@ -212,20 +212,23 @@ module Groupdate |
212 | 212 | series << next_step |
213 | 213 | end |
214 | 214 | |
215 | - if multiple_groups | |
216 | - keys = count.keys.map { |k| k[0...@group_index] + k[(@group_index + 1)..-1] }.uniq | |
217 | - series = series.reverse if reverse | |
218 | - keys.flat_map do |k| | |
219 | - series.map { |s| k[0...@group_index] + [s] + k[@group_index..-1] } | |
220 | - end | |
221 | - else | |
222 | - series | |
223 | - end | |
215 | + series | |
224 | 216 | else |
225 | 217 | [] |
226 | 218 | end |
227 | 219 | end |
228 | 220 | |
221 | + series = | |
222 | + if multiple_groups | |
223 | + keys = count.keys.map { |k| k[0...@group_index] + k[(@group_index + 1)..-1] }.uniq | |
224 | + series = series.reverse if reverse | |
225 | + keys.flat_map do |k| | |
226 | + series.map { |s| k[0...@group_index] + [s] + k[@group_index..-1] } | |
227 | + end | |
228 | + else | |
229 | + series | |
230 | + end | |
231 | + | |
229 | 232 | # reversed above if multiple groups |
230 | 233 | if !multiple_groups && reverse |
231 | 234 | series = series.to_a.reverse | ... | ... |
lib/groupdate/series.rb
... | ... | @@ -12,7 +12,7 @@ module Groupdate |
12 | 12 | # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb |
13 | 13 | if ActiveRecord::Calculations.method_defined?(method) |
14 | 14 | magic.perform(relation, method, *args, &block) |
15 | - elsif @relation.respond_to?(method) | |
15 | + elsif @relation.respond_to?(method, true) | |
16 | 16 | Groupdate::Series.new(magic, relation.send(method, *args, &block)) |
17 | 17 | else |
18 | 18 | super | ... | ... |
test/test_helper.rb
... | ... | @@ -636,6 +636,29 @@ module TestGroupdate |
636 | 636 | assert_equal expected, User.group_by_day(:created_at).group(:score).order(:score).count |
637 | 637 | end |
638 | 638 | |
639 | + def test_group_day_of_week | |
640 | + create_user "2013-05-01 00:00:00 UTC", 1 | |
641 | + create_user "2013-05-02 00:00:00 UTC", 2 | |
642 | + create_user "2013-05-03 00:00:00 UTC", 2 | |
643 | + expected = { | |
644 | + [1, 0] => 0, | |
645 | + [1, 1] => 0, | |
646 | + [1, 2] => 0, | |
647 | + [1, 3] => 1, | |
648 | + [1, 4] => 0, | |
649 | + [1, 5] => 0, | |
650 | + [1, 6] => 0, | |
651 | + [2, 0] => 0, | |
652 | + [2, 1] => 0, | |
653 | + [2, 2] => 0, | |
654 | + [2, 3] => 0, | |
655 | + [2, 4] => 1, | |
656 | + [2, 5] => 1, | |
657 | + [2, 6] => 0 | |
658 | + } | |
659 | + assert_equal expected, User.group(:score).group_by_day_of_week(:created_at).count | |
660 | + end | |
661 | + | |
639 | 662 | def test_groupdate_multiple |
640 | 663 | create_user "2013-05-01 00:00:00 UTC", 1 |
641 | 664 | expected = { | ... | ... |