groupdate 3.2.0 → 6.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +146 -27
- data/CONTRIBUTING.md +75 -0
- data/LICENSE.txt +1 -1
- data/README.md +67 -44
- data/lib/groupdate/active_record.rb +4 -51
- data/lib/groupdate/adapters/base_adapter.rb +51 -0
- data/lib/groupdate/adapters/mysql_adapter.rb +63 -0
- data/lib/groupdate/adapters/postgresql_adapter.rb +46 -0
- data/lib/groupdate/adapters/sqlite_adapter.rb +51 -0
- data/lib/groupdate/enumerable.rb +9 -14
- data/lib/groupdate/magic.rb +202 -333
- data/lib/groupdate/query_methods.rb +18 -0
- data/lib/groupdate/relation.rb +19 -0
- data/lib/groupdate/series_builder.rb +304 -0
- data/lib/groupdate/version.rb +1 -1
- data/lib/groupdate.rb +37 -7
- metadata +20 -145
- data/.gitignore +0 -19
- data/.travis.yml +0 -25
- data/Gemfile +0 -6
- data/Rakefile +0 -24
- data/groupdate.gemspec +0 -37
- data/lib/groupdate/calculations.rb +0 -26
- data/lib/groupdate/order_hack.rb +0 -11
- data/lib/groupdate/scopes.rb +0 -24
- data/lib/groupdate/series.rb +0 -30
- data/test/enumerable_test.rb +0 -60
- data/test/gemfiles/activerecord31.gemfile +0 -6
- data/test/gemfiles/activerecord32.gemfile +0 -6
- data/test/gemfiles/activerecord40.gemfile +0 -6
- data/test/gemfiles/activerecord41.gemfile +0 -6
- data/test/gemfiles/activerecord42.gemfile +0 -6
- data/test/gemfiles/redshift.gemfile +0 -7
- data/test/mysql_test.rb +0 -15
- data/test/postgresql_test.rb +0 -15
- data/test/redshift_test.rb +0 -18
- data/test/sqlite_test.rb +0 -29
- data/test/test_helper.rb +0 -1207
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 372cf4471ee65729f41e684b45f5b3e05333379390fab3956df568a2fa89c8cd
|
4
|
+
data.tar.gz: 549f1a34474348dfc96c7a0c244218688e4c2d45cd9b935a3c5ff63b03873968
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17009c3b4061583998a5419edca9b62fb138136097d9ef1a719dc04a09bd5f3583be45bae5639498000318cffa2d5d2e0a31b0e2f564a1d3cc61016d1db7f4f2
|
7
|
+
data.tar.gz: 1fcb4a2a87baf147fa763508f0b17b01bbae8c28fed284055282ec196163e92026548f87ddcf381fba280c2d5a2e4a61adb54d8e8f4334252ec481e4f04f92d9
|
data/CHANGELOG.md
CHANGED
@@ -1,29 +1,148 @@
|
|
1
|
-
##
|
1
|
+
## 6.2.1 (2023-04-18)
|
2
|
+
|
3
|
+
- Fixed extra day with `DateTime` ranges
|
4
|
+
|
5
|
+
## 6.2.0 (2023-01-29)
|
6
|
+
|
7
|
+
- Added support for async methods with Active Record 7.1
|
8
|
+
|
9
|
+
## 6.1.0 (2022-04-05)
|
10
|
+
|
11
|
+
- Added `expand_range` option
|
12
|
+
|
13
|
+
## 6.0.1 (2022-01-16)
|
14
|
+
|
15
|
+
- Fixed incorrect results (error before 6.0) with `includes` with Active Record 6.1+
|
16
|
+
|
17
|
+
## 6.0.0 (2022-01-15)
|
18
|
+
|
19
|
+
- Raise `ActiveRecord::UnknownAttributeReference` for non-attribute arguments
|
20
|
+
- Raise `ArgumentError` for ranges with string bounds
|
21
|
+
- Added `n` option for Redshift
|
22
|
+
- Changed SQL to return dates instead of times for day, week, month, quarter, and year
|
23
|
+
- Removed `dates` option
|
24
|
+
- Dropped support for Ruby < 2.6 and Rails < 5.2
|
25
|
+
|
26
|
+
## 5.2.4 (2021-12-15)
|
27
|
+
|
28
|
+
- Simplified queries for Active Record 7 and MySQL
|
29
|
+
|
30
|
+
## 5.2.3 (2021-12-06)
|
31
|
+
|
32
|
+
- Fixed error and warnings with Active Record 7
|
33
|
+
|
34
|
+
## 5.2.2 (2021-02-08)
|
35
|
+
|
36
|
+
- Added support for `nil..nil` ranges in `range` option
|
37
|
+
|
38
|
+
## 5.2.1 (2020-09-09)
|
39
|
+
|
40
|
+
- Improved error message for invalid ranges
|
41
|
+
- Fixed bug with date string ranges
|
42
|
+
|
43
|
+
## 5.2.0 (2020-09-07)
|
44
|
+
|
45
|
+
- Added warning for non-attribute argument
|
46
|
+
- Added support for beginless and endless ranges in `range` option
|
47
|
+
|
48
|
+
## 5.1.0 (2020-07-30)
|
49
|
+
|
50
|
+
- Added `n` option to minute and second for custom durations
|
51
|
+
|
52
|
+
## 5.0.0 (2020-02-18)
|
53
|
+
|
54
|
+
- Added support for `week_start` for SQLite
|
55
|
+
- Added support for full weekday names
|
56
|
+
- Made `day_start` behavior consistent between Active Record and enumerable
|
57
|
+
- Made `last` option extend to end of current period
|
58
|
+
- Raise error when `day_start` and `week_start` passed to unsupported methods
|
59
|
+
- The `day_start` option no longer applies to shorter periods
|
60
|
+
- Fixed `inconsistent time zone info` errors around DST with MySQL and PostgreSQL
|
61
|
+
- Improved performance of `format` option
|
62
|
+
- Removed deprecated positional arguments for time zone and range
|
63
|
+
- Dropped support for `mysql` gem (last release was 2013)
|
64
|
+
|
65
|
+
## 4.3.0 (2019-12-26)
|
66
|
+
|
67
|
+
- Fixed error with empty results in Ruby 2.7
|
68
|
+
- Fixed deprecation warnings in Ruby 2.7
|
69
|
+
- Deprecated positional arguments for time zone and range
|
70
|
+
|
71
|
+
## 4.2.0 (2019-10-28)
|
72
|
+
|
73
|
+
- Added `day_of_year`
|
74
|
+
- Dropped support for Rails 4.2
|
75
|
+
|
76
|
+
## 4.1.2 (2019-05-26)
|
77
|
+
|
78
|
+
- Fixed error with empty data and `current: false`
|
79
|
+
- Fixed error in time zone check for Rails < 5.2
|
80
|
+
- Prevent infinite loop with endless ranges
|
81
|
+
|
82
|
+
## 4.1.1 (2018-12-11)
|
83
|
+
|
84
|
+
- Made column resolution consistent with `group`
|
85
|
+
- Added support for `alias_attribute`
|
86
|
+
|
87
|
+
## 4.1.0 (2018-11-04)
|
88
|
+
|
89
|
+
- Many performance improvements
|
90
|
+
- Added check for consistent time zone info
|
91
|
+
- Fixed error message for invalid queries with MySQL and SQLite
|
92
|
+
- Fixed issue with enumerable methods ignoring nils
|
93
|
+
|
94
|
+
## 4.0.2 (2018-10-15)
|
95
|
+
|
96
|
+
- Make `current` option work without `last`
|
97
|
+
- Fixed default value for `maximum`, `minimum`, and `average` (periods with no results now return `nil` instead of `0`, pass `default_value: 0` for previous behavior)
|
98
|
+
|
99
|
+
## 4.0.1 (2018-05-03)
|
100
|
+
|
101
|
+
- Fixed incorrect range with `last` option near time change
|
102
|
+
|
103
|
+
## 4.0.0 (2018-02-21)
|
104
|
+
|
105
|
+
- Custom calculation methods are supported by default - `groupdate_calculation_methods` is no longer needed
|
106
|
+
|
107
|
+
Breaking changes
|
108
|
+
|
109
|
+
- Dropped support for Rails < 4.2
|
110
|
+
- Invalid options now throw an `ArgumentError`
|
111
|
+
- `group_by` methods return an `ActiveRecord::Relation` instead of a `Groupdate::Series`
|
112
|
+
- `week_start` now affects `day_of_week`
|
113
|
+
- Removed support for `reverse_order` (was never supported in Rails 5)
|
114
|
+
|
115
|
+
## 3.2.1 (2018-02-21)
|
116
|
+
|
117
|
+
- Added `minute_of_hour`
|
118
|
+
- Added support for `unscoped`
|
119
|
+
|
120
|
+
## 3.2.0 (2017-01-30)
|
2
121
|
|
3
122
|
- Added limited support for SQLite
|
4
123
|
|
5
|
-
## 3.1.1
|
124
|
+
## 3.1.1 (2016-10-25)
|
6
125
|
|
7
126
|
- Fixed `current: false`
|
8
127
|
- Fixed `last` with `group_by_quarter`
|
9
128
|
- Raise `ArgumentError` when `last` option is not supported
|
10
129
|
|
11
|
-
## 3.1.0
|
130
|
+
## 3.1.0 (2016-10-22)
|
12
131
|
|
13
132
|
- Better support for date columns with `time_zone: false`
|
14
133
|
- Better date range handling for `range` option
|
15
134
|
|
16
|
-
## 3.0.2
|
135
|
+
## 3.0.2 (2016-08-09)
|
17
136
|
|
18
137
|
- Fixed `group_by_period` with associations
|
19
138
|
- Fixed `week_start` option for enumerables
|
20
139
|
|
21
|
-
## 3.0.1
|
140
|
+
## 3.0.1 (2016-07-13)
|
22
141
|
|
23
142
|
- Added support for Redshift
|
24
143
|
- Fix for infinite loop in certain cases for Rails 5
|
25
144
|
|
26
|
-
## 3.0.0
|
145
|
+
## 3.0.0 (2016-05-30)
|
27
146
|
|
28
147
|
Breaking changes
|
29
148
|
|
@@ -31,16 +150,16 @@ Breaking changes
|
|
31
150
|
- Array and hash methods no longer return the entire series by default. Use `series: true` for the previous behavior.
|
32
151
|
- The `series: false` option now returns the correct types and order, and plays nicely with other options.
|
33
152
|
|
34
|
-
## 2.5.3
|
153
|
+
## 2.5.3 (2016-04-28)
|
35
154
|
|
36
155
|
- All tests green with `mysql` gem
|
37
156
|
- Added support for decimal day start
|
38
157
|
|
39
|
-
## 2.5.2
|
158
|
+
## 2.5.2 (2016-02-16)
|
40
159
|
|
41
160
|
- Added `dates` option to return dates for day, week, month, quarter, and year
|
42
161
|
|
43
|
-
## 2.5.1
|
162
|
+
## 2.5.1 (2016-02-03)
|
44
163
|
|
45
164
|
- Added `group_by_quarter`
|
46
165
|
- Added `default_value` option
|
@@ -48,13 +167,13 @@ Breaking changes
|
|
48
167
|
- Raise `ArgumentError` if no field specified
|
49
168
|
- Added support for ActiveRecord 5 beta
|
50
169
|
|
51
|
-
## 2.5.0
|
170
|
+
## 2.5.0 (2015-09-29)
|
52
171
|
|
53
172
|
- Added `group_by_period` method
|
54
173
|
- Added `current` option
|
55
174
|
- Raise `ArgumentError` if no block given to enumerable
|
56
175
|
|
57
|
-
## 2.4.0
|
176
|
+
## 2.4.0 (2014-12-28)
|
58
177
|
|
59
178
|
- Added localization
|
60
179
|
- Added `carry_forward` option
|
@@ -62,77 +181,77 @@ Breaking changes
|
|
62
181
|
- Fixed issue w/ Brasilia Summer Time
|
63
182
|
- Fixed issues w/ ActiveRecord 4.2
|
64
183
|
|
65
|
-
## 2.3.0
|
184
|
+
## 2.3.0 (2014-08-31)
|
66
185
|
|
67
186
|
- Raise error when ActiveRecord::Base.default_timezone is not `:utc`
|
68
187
|
- Added `day_of_month`
|
69
188
|
- Added `month_of_year`
|
70
189
|
- Do not quote column name
|
71
190
|
|
72
|
-
## 2.2.1
|
191
|
+
## 2.2.1 (2014-06-23)
|
73
192
|
|
74
193
|
- Fixed ActiveRecord 3 associations
|
75
194
|
|
76
|
-
## 2.2.0
|
195
|
+
## 2.2.0 (2014-06-22)
|
77
196
|
|
78
197
|
- Added support for arrays and hashes
|
79
198
|
|
80
|
-
## 2.1.1
|
199
|
+
## 2.1.1 (2014-05-17)
|
81
200
|
|
82
201
|
- Fixed format option with multiple groups
|
83
202
|
- Better error message if time zone support is missing for MySQL
|
84
203
|
|
85
|
-
## 2.1.0
|
204
|
+
## 2.1.0 (2014-03-16)
|
86
205
|
|
87
206
|
- Added last option
|
88
207
|
- Added format option
|
89
208
|
|
90
|
-
## 2.0.4
|
209
|
+
## 2.0.4 (2014-03-12)
|
91
210
|
|
92
211
|
- Added multiple groups
|
93
212
|
- Added order
|
94
213
|
- Subsequent methods no longer modify relation
|
95
214
|
|
96
|
-
## 2.0.3
|
215
|
+
## 2.0.3 (2014-03-11)
|
97
216
|
|
98
217
|
- Implemented respond_to?
|
99
218
|
|
100
|
-
## 2.0.2
|
219
|
+
## 2.0.2 (2014-03-11)
|
101
220
|
|
102
221
|
- where, joins, and includes no longer need to be before the group_by method
|
103
222
|
|
104
|
-
## 2.0.1
|
223
|
+
## 2.0.1 (2014-03-07)
|
105
224
|
|
106
225
|
- Use time zone instead of UTC for results
|
107
226
|
|
108
|
-
## 2.0.0
|
227
|
+
## 2.0.0 (2014-03-07)
|
109
228
|
|
110
229
|
- Returns entire series by default
|
111
230
|
- Added day_start option
|
112
231
|
- Better interface
|
113
232
|
|
114
|
-
## 1.0.5
|
233
|
+
## 1.0.5 (2014-03-06)
|
115
234
|
|
116
235
|
- Added global time_zone option
|
117
236
|
|
118
|
-
## 1.0.4
|
237
|
+
## 1.0.4 (2013-07-20)
|
119
238
|
|
120
239
|
- Added global week_start option
|
121
240
|
- Fixed bug with NULL values and series
|
122
241
|
|
123
|
-
## 1.0.3
|
242
|
+
## 1.0.3 (2013-07-05)
|
124
243
|
|
125
244
|
- Fixed deprecation warning when used with will_paginate
|
126
245
|
- Fixed bug with DateTime series
|
127
246
|
|
128
|
-
## 1.0.2
|
247
|
+
## 1.0.2 (2013-06-10)
|
129
248
|
|
130
249
|
- Added :start option for custom week start for group_by_week
|
131
250
|
|
132
|
-
## 1.0.1
|
251
|
+
## 1.0.1 (2013-06-03)
|
133
252
|
|
134
253
|
- Fixed series for Rails < 3.2 and MySQL
|
135
254
|
|
136
|
-
## 1.0.0
|
255
|
+
## 1.0.0 (2013-05-15)
|
137
256
|
|
138
257
|
- First major release
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
First, thanks for wanting to contribute. You’re awesome! :heart:
|
4
|
+
|
5
|
+
## Help
|
6
|
+
|
7
|
+
We’re not able to provide support through GitHub Issues. If you’re looking for help with your code, try posting on [Stack Overflow](https://stackoverflow.com/).
|
8
|
+
|
9
|
+
All features should be documented. If you don’t see a feature in the docs, assume it doesn’t exist.
|
10
|
+
|
11
|
+
## Bugs
|
12
|
+
|
13
|
+
Think you’ve discovered a bug?
|
14
|
+
|
15
|
+
1. Search existing issues to see if it’s been reported.
|
16
|
+
2. Try the `master` branch to make sure it hasn’t been fixed.
|
17
|
+
|
18
|
+
```rb
|
19
|
+
gem "groupdate", github: "ankane/groupdate"
|
20
|
+
```
|
21
|
+
|
22
|
+
If the above steps don’t help, create an issue. Include:
|
23
|
+
|
24
|
+
- Detailed steps to reproduce
|
25
|
+
- Complete backtraces for exceptions
|
26
|
+
|
27
|
+
## New Features
|
28
|
+
|
29
|
+
If you’d like to discuss a new feature, create an issue and start the title with `[Idea]`.
|
30
|
+
|
31
|
+
## Pull Requests
|
32
|
+
|
33
|
+
Fork the project and create a pull request. A few tips:
|
34
|
+
|
35
|
+
- Keep changes to a minimum. If you have multiple features or fixes, submit multiple pull requests.
|
36
|
+
- Follow the existing style. The code should read like it’s written by a single person.
|
37
|
+
- Add one or more tests if possible. Make sure existing tests pass with:
|
38
|
+
|
39
|
+
```sh
|
40
|
+
bundle exec rake test
|
41
|
+
```
|
42
|
+
|
43
|
+
Feel free to open an issue to get feedback on your idea before spending too much time on it.
|
44
|
+
|
45
|
+
## Dev Setup
|
46
|
+
|
47
|
+
On Mac:
|
48
|
+
|
49
|
+
```sh
|
50
|
+
# install and run PostgreSQL
|
51
|
+
brew install postgresql
|
52
|
+
brew services start postgresql
|
53
|
+
|
54
|
+
# install and run MySQL
|
55
|
+
brew install mysql
|
56
|
+
brew services start mysql
|
57
|
+
|
58
|
+
# create databases
|
59
|
+
createdb groupdate_test
|
60
|
+
mysqladmin create groupdate_test
|
61
|
+
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
62
|
+
|
63
|
+
# clone the repo and run the tests
|
64
|
+
git clone https://github.com/ankane/groupdate.git
|
65
|
+
cd groupdate
|
66
|
+
bundle install
|
67
|
+
bundle exec rake test
|
68
|
+
|
69
|
+
# run a single test file
|
70
|
+
ruby test/postgresql_test.rb
|
71
|
+
```
|
72
|
+
|
73
|
+
---
|
74
|
+
|
75
|
+
This contributing guide is released under [CCO](https://creativecommons.org/publicdomain/zero/1.0/) (public domain). Use it for your own project without attribution.
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -11,24 +11,30 @@ The simplest way to group by:
|
|
11
11
|
|
12
12
|
:cake: Get the entire series - **the other best part**
|
13
13
|
|
14
|
-
Supports PostgreSQL, MySQL, and Redshift, plus arrays and hashes
|
14
|
+
Supports PostgreSQL, MySQL, and Redshift, plus arrays and hashes (and limited support for [SQLite](#for-sqlite))
|
15
15
|
|
16
|
-
|
16
|
+
:cupid: Goes hand in hand with [Chartkick](https://www.chartkick.com)
|
17
17
|
|
18
|
-
[![Build Status](https://
|
18
|
+
[![Build Status](https://github.com/ankane/groupdate/workflows/build/badge.svg?branch=master)](https://github.com/ankane/groupdate/actions)
|
19
19
|
|
20
|
-
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application’s Gemfile:
|
21
23
|
|
22
|
-
|
24
|
+
```ruby
|
25
|
+
gem "groupdate"
|
26
|
+
```
|
23
27
|
|
24
|
-
|
28
|
+
For MySQL and SQLite, also follow [these instructions](#additional-instructions).
|
29
|
+
|
30
|
+
## Getting Started
|
25
31
|
|
26
32
|
```ruby
|
27
33
|
User.group_by_day(:created_at).count
|
28
34
|
# {
|
29
|
-
# Sat,
|
30
|
-
# Sun,
|
31
|
-
# Mon,
|
35
|
+
# Sat, 24 May 2020 => 50,
|
36
|
+
# Sun, 25 May 2020 => 100,
|
37
|
+
# Mon, 26 May 2020 => 34
|
32
38
|
# }
|
33
39
|
```
|
34
40
|
|
@@ -47,12 +53,14 @@ You can group by:
|
|
47
53
|
|
48
54
|
and
|
49
55
|
|
56
|
+
- minute_of_hour
|
50
57
|
- hour_of_day
|
51
58
|
- day_of_week (Sunday = 0, Monday = 1, etc)
|
52
59
|
- day_of_month
|
60
|
+
- day_of_year
|
53
61
|
- month_of_year
|
54
62
|
|
55
|
-
Use it anywhere you can use `group`.
|
63
|
+
Use it anywhere you can use `group`. Works with `count`, `sum`, `minimum`, `maximum`, and `average`. For `median` and `percentile`, check out [ActiveMedian](https://github.com/ankane/active_median).
|
56
64
|
|
57
65
|
### Time Zones
|
58
66
|
|
@@ -67,9 +75,9 @@ or
|
|
67
75
|
```ruby
|
68
76
|
User.group_by_week(:created_at, time_zone: "Pacific Time (US & Canada)").count
|
69
77
|
# {
|
70
|
-
# Sun,
|
71
|
-
# Sun,
|
72
|
-
# Sun,
|
78
|
+
# Sun, 08 Mar 2020 => 70,
|
79
|
+
# Sun, 15 Mar 2020 => 54,
|
80
|
+
# Sun, 22 Mar 2020 => 80
|
73
81
|
# }
|
74
82
|
```
|
75
83
|
|
@@ -80,13 +88,13 @@ Time zone objects also work. To see a list of available time zones in Rails, run
|
|
80
88
|
Weeks start on Sunday by default. Change this with:
|
81
89
|
|
82
90
|
```ruby
|
83
|
-
Groupdate.week_start = :
|
91
|
+
Groupdate.week_start = :monday
|
84
92
|
```
|
85
93
|
|
86
94
|
or
|
87
95
|
|
88
96
|
```ruby
|
89
|
-
User.group_by_week(:created_at, week_start: :
|
97
|
+
User.group_by_week(:created_at, week_start: :monday).count
|
90
98
|
```
|
91
99
|
|
92
100
|
### Day Start
|
@@ -111,6 +119,12 @@ To get a specific time range, use:
|
|
111
119
|
User.group_by_day(:created_at, range: 2.weeks.ago.midnight..Time.now).count
|
112
120
|
```
|
113
121
|
|
122
|
+
To expand the range to the start and end of the time period, use:
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
User.group_by_day(:created_at, range: 2.weeks.ago..Time.now, expand_range: true).count
|
126
|
+
```
|
127
|
+
|
114
128
|
To get the most recent time periods, use:
|
115
129
|
|
116
130
|
```ruby
|
@@ -140,8 +154,8 @@ To get keys in a different format, use:
|
|
140
154
|
```ruby
|
141
155
|
User.group_by_month(:created_at, format: "%b %Y").count
|
142
156
|
# {
|
143
|
-
# "Jan
|
144
|
-
# "Feb
|
157
|
+
# "Jan 2020" => 10
|
158
|
+
# "Feb 2020" => 12
|
145
159
|
# }
|
146
160
|
```
|
147
161
|
|
@@ -181,11 +195,19 @@ User.group_by_period(:day, :created_at).count
|
|
181
195
|
Limit groupings with the `permit` option.
|
182
196
|
|
183
197
|
```ruby
|
184
|
-
User.group_by_period(params[:period], :created_at, permit:
|
198
|
+
User.group_by_period(params[:period], :created_at, permit: ["day", "week"]).count
|
185
199
|
```
|
186
200
|
|
187
201
|
Raises an `ArgumentError` for unpermitted periods.
|
188
202
|
|
203
|
+
### Custom Duration
|
204
|
+
|
205
|
+
To group by a specific number of minutes or seconds, use:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
User.group_by_minute(:created_at, n: 10).count # 10 minutes
|
209
|
+
```
|
210
|
+
|
189
211
|
### Date Columns
|
190
212
|
|
191
213
|
If grouping on date columns which don’t need time zone conversion, use:
|
@@ -194,6 +216,14 @@ If grouping on date columns which don’t need time zone conversion, use:
|
|
194
216
|
User.group_by_week(:created_on, time_zone: false).count
|
195
217
|
```
|
196
218
|
|
219
|
+
### Default Scopes
|
220
|
+
|
221
|
+
If you use Postgres and have a default scope that uses `order`, you may get a `column must appear in the GROUP BY clause` error (just like with Active Record’s `group` method). Remove the `order` scope with:
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
User.unscope(:order).group_by_day(:count).count
|
225
|
+
```
|
226
|
+
|
197
227
|
## Arrays and Hashes
|
198
228
|
|
199
229
|
```ruby
|
@@ -206,44 +236,42 @@ Supports the same options as above
|
|
206
236
|
users.group_by_day(time_zone: time_zone) { |u| u.created_at }
|
207
237
|
```
|
208
238
|
|
209
|
-
|
239
|
+
Get the entire series with:
|
210
240
|
|
211
241
|
```ruby
|
212
|
-
|
242
|
+
users.group_by_day(series: true) { |u| u.created_at }
|
213
243
|
```
|
214
244
|
|
215
|
-
|
216
|
-
|
217
|
-
Add this line to your application’s Gemfile:
|
245
|
+
Count
|
218
246
|
|
219
247
|
```ruby
|
220
|
-
|
248
|
+
users.group_by_day { |u| u.created_at }.to_h { |k, v| [k, v.count] }
|
221
249
|
```
|
222
250
|
|
223
|
-
|
251
|
+
## Additional Instructions
|
224
252
|
|
225
|
-
|
253
|
+
### For MySQL
|
254
|
+
|
255
|
+
[Time zone support](https://dev.mysql.com/doc/refman/8.0/en/time-zone-support.html) must be installed on the server.
|
226
256
|
|
227
257
|
```sh
|
228
258
|
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
229
259
|
```
|
230
260
|
|
231
|
-
or copy and paste [these statements](https://gist.githubusercontent.com/ankane/1d6b0022173186accbf0/raw/time_zone_support.sql) into a SQL console.
|
232
|
-
|
233
261
|
You can confirm it worked with:
|
234
262
|
|
235
263
|
```sql
|
236
|
-
SELECT CONVERT_TZ(NOW(), '+00:00', '
|
264
|
+
SELECT CONVERT_TZ(NOW(), '+00:00', 'Pacific/Honolulu');
|
237
265
|
```
|
238
266
|
|
239
267
|
It should return the time instead of `NULL`.
|
240
268
|
|
241
|
-
|
269
|
+
### For SQLite
|
242
270
|
|
243
271
|
Groupdate has limited support for SQLite.
|
244
272
|
|
245
273
|
- No time zone support
|
246
|
-
- No `day_start`
|
274
|
+
- No `day_start` option
|
247
275
|
- No `group_by_quarter` method
|
248
276
|
|
249
277
|
If your application’s time zone is set to something other than `Etc/UTC` (the default), create an initializer with:
|
@@ -254,27 +282,20 @@ Groupdate.time_zone = false
|
|
254
282
|
|
255
283
|
## Upgrading
|
256
284
|
|
257
|
-
###
|
258
|
-
|
259
|
-
Groupdate 3.0 brings a number of improvements. Here are a few to be aware of:
|
260
|
-
|
261
|
-
- `Date` objects are now returned for day, week, month, quarter, and year by default. Use `dates: false` for the previous behavior, or change this globally with `Groupdate.dates = false`.
|
262
|
-
- Array and hash methods no longer return the entire series by default. Use `series: true` for the previous behavior.
|
263
|
-
- The `series: false` option now returns the correct type and order, and plays nicely with other options.
|
285
|
+
### 6.0
|
264
286
|
|
265
|
-
|
287
|
+
Groupdate 6.0 protects against unsafe input by default. For non-attribute arguments, use:
|
266
288
|
|
267
|
-
|
289
|
+
```ruby
|
290
|
+
User.group_by_day(Arel.sql(known_safe_value)).count
|
291
|
+
```
|
268
292
|
|
269
|
-
|
270
|
-
- `ActiveSupport::TimeWithZone` keys are now returned for every database adapter - adapters previously returned `Time` or `String` keys
|
293
|
+
Also, the `dates` option has been removed.
|
271
294
|
|
272
295
|
## History
|
273
296
|
|
274
297
|
View the [changelog](https://github.com/ankane/groupdate/blob/master/CHANGELOG.md)
|
275
298
|
|
276
|
-
Groupdate follows [Semantic Versioning](http://semver.org/)
|
277
|
-
|
278
299
|
## Contributing
|
279
300
|
|
280
301
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
@@ -283,3 +304,5 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
|
|
283
304
|
- Fix bugs and [submit pull requests](https://github.com/ankane/groupdate/pulls)
|
284
305
|
- Write, clarify, or fix documentation
|
285
306
|
- Suggest or add new features
|
307
|
+
|
308
|
+
To get started with development and testing, check out the [Contributing Guide](CONTRIBUTING.md).
|
@@ -1,53 +1,6 @@
|
|
1
1
|
require "active_record"
|
2
|
-
require "groupdate/
|
3
|
-
require "groupdate/
|
4
|
-
require "groupdate/calculations"
|
5
|
-
require "groupdate/series"
|
2
|
+
require "groupdate/query_methods"
|
3
|
+
require "groupdate/relation"
|
6
4
|
|
7
|
-
ActiveRecord::Base.
|
8
|
-
|
9
|
-
module ActiveRecord
|
10
|
-
class Relation
|
11
|
-
if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR < 2
|
12
|
-
|
13
|
-
def method_missing_with_hack(method, *args, &block)
|
14
|
-
if Groupdate::METHODS.include?(method)
|
15
|
-
scoping { @klass.send(method, *args, &block) }
|
16
|
-
else
|
17
|
-
method_missing_without_hack(method, *args, &block)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
alias_method_chain :method_missing, :hack
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
module ActiveRecord
|
27
|
-
module Associations
|
28
|
-
class CollectionProxy
|
29
|
-
if ActiveRecord::VERSION::MAJOR == 3
|
30
|
-
delegate(*Groupdate::METHODS, to: :scoped)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# hack for issue before Rails 5
|
37
|
-
# https://github.com/rails/rails/issues/7121
|
38
|
-
module ActiveRecord
|
39
|
-
module Calculations
|
40
|
-
private
|
41
|
-
|
42
|
-
if ActiveRecord::VERSION::MAJOR < 5
|
43
|
-
def column_alias_for_with_hack(*keys)
|
44
|
-
if keys.first.is_a?(Groupdate::OrderHack)
|
45
|
-
keys.first.field
|
46
|
-
else
|
47
|
-
column_alias_for_without_hack(*keys)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
alias_method_chain :column_alias_for, :hack
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
5
|
+
ActiveRecord::Base.extend(Groupdate::QueryMethods)
|
6
|
+
ActiveRecord::Relation.include(Groupdate::Relation)
|