groupdate 4.1.2 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +67 -34
- data/CONTRIBUTING.md +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +35 -37
- data/lib/groupdate.rb +7 -4
- data/lib/groupdate/enumerable.rb +9 -15
- data/lib/groupdate/magic.rb +54 -10
- data/lib/groupdate/query_methods.rb +3 -10
- data/lib/groupdate/relation_builder.rb +90 -49
- data/lib/groupdate/series_builder.rb +137 -73
- data/lib/groupdate/version.rb +1 -1
- metadata +18 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d18870af57fc18006c5f176fa3138dfe36a715b16e5a6113f447aee6d3b7468
|
4
|
+
data.tar.gz: 889870a1a2db8784e28413c6071a784dff66d00b4aaf05a323e70e5d16f09fd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ae29a1e8d0eb14c4ef904fbe366cb5f7029d9e94429a74f6f70073a880f2910f62d5161d39618808c09dc937185cc79ba233ac3fc7e34f983a8e32d60e0be70
|
7
|
+
data.tar.gz: 6348f6853cf2015f6777bcc63e1e416a31456a0e779678164992bd6424daca73272bb5fcb12016d1f3c085b4e14e991afa067f4f93354982e7229699f3ea2eab
|
data/CHANGELOG.md
CHANGED
@@ -1,31 +1,64 @@
|
|
1
|
-
##
|
1
|
+
## 5.2.0 (2020-09-07)
|
2
|
+
|
3
|
+
- Added warning for non-attribute argument
|
4
|
+
- Added support for beginless and endless ranges in `range` option
|
5
|
+
|
6
|
+
## 5.1.0 (2020-07-30)
|
7
|
+
|
8
|
+
- Added `n` option to minute and second for custom durations
|
9
|
+
|
10
|
+
## 5.0.0 (2020-02-18)
|
11
|
+
|
12
|
+
- Added support for `week_start` for SQLite
|
13
|
+
- Added support for full weekday names
|
14
|
+
- Made `day_start` behavior consistent between Active Record and enumerable
|
15
|
+
- Made `last` option extend to end of current period
|
16
|
+
- Raise error when `day_start` and `week_start` passed to unsupported methods
|
17
|
+
- The `day_start` option no longer applies to shorter periods
|
18
|
+
- Fixed `inconsistent time zone info` errors around DST with MySQL and PostgreSQL
|
19
|
+
- Improved performance of `format` option
|
20
|
+
- Removed deprecated positional arguments for time zone and range
|
21
|
+
- Dropped support for `mysql` gem (last release was 2013)
|
22
|
+
|
23
|
+
## 4.3.0 (2019-12-26)
|
24
|
+
|
25
|
+
- Fixed error with empty results in Ruby 2.7
|
26
|
+
- Fixed deprecation warnings in Ruby 2.7
|
27
|
+
- Deprecated positional arguments for time zone and range
|
28
|
+
|
29
|
+
## 4.2.0 (2019-10-28)
|
30
|
+
|
31
|
+
- Added `day_of_year`
|
32
|
+
- Dropped support for Rails 4.2
|
33
|
+
|
34
|
+
## 4.1.2 (2019-05-26)
|
2
35
|
|
3
36
|
- Fixed error with empty data and `current: false`
|
4
37
|
- Fixed error in time zone check for Rails < 5.2
|
5
38
|
- Prevent infinite loop with endless ranges
|
6
39
|
|
7
|
-
## 4.1.1
|
40
|
+
## 4.1.1 (2018-12-11)
|
8
41
|
|
9
42
|
- Made column resolution consistent with `group`
|
10
43
|
- Added support for `alias_attribute`
|
11
44
|
|
12
|
-
## 4.1.0
|
45
|
+
## 4.1.0 (2018-11-04)
|
13
46
|
|
14
47
|
- Many performance improvements
|
15
48
|
- Added check for consistent time zone info
|
16
49
|
- Fixed error message for invalid queries with MySQL and SQLite
|
17
50
|
- Fixed issue with enumerable methods ignoring nils
|
18
51
|
|
19
|
-
## 4.0.2
|
52
|
+
## 4.0.2 (2018-10-15)
|
20
53
|
|
21
54
|
- Make `current` option work without `last`
|
22
55
|
- 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)
|
23
56
|
|
24
|
-
## 4.0.1
|
57
|
+
## 4.0.1 (2018-05-03)
|
25
58
|
|
26
59
|
- Fixed incorrect range with `last` option near time change
|
27
60
|
|
28
|
-
## 4.0.0
|
61
|
+
## 4.0.0 (2018-02-21)
|
29
62
|
|
30
63
|
- Custom calculation methods are supported by default - `groupdate_calculation_methods` is no longer needed
|
31
64
|
|
@@ -37,37 +70,37 @@ Breaking changes
|
|
37
70
|
- `week_start` now affects `day_of_week`
|
38
71
|
- Removed support for `reverse_order` (was never supported in Rails 5)
|
39
72
|
|
40
|
-
## 3.2.1
|
73
|
+
## 3.2.1 (2018-02-21)
|
41
74
|
|
42
75
|
- Added `minute_of_hour`
|
43
76
|
- Added support for `unscoped`
|
44
77
|
|
45
|
-
## 3.2.0
|
78
|
+
## 3.2.0 (2017-01-30)
|
46
79
|
|
47
80
|
- Added limited support for SQLite
|
48
81
|
|
49
|
-
## 3.1.1
|
82
|
+
## 3.1.1 (2016-10-25)
|
50
83
|
|
51
84
|
- Fixed `current: false`
|
52
85
|
- Fixed `last` with `group_by_quarter`
|
53
86
|
- Raise `ArgumentError` when `last` option is not supported
|
54
87
|
|
55
|
-
## 3.1.0
|
88
|
+
## 3.1.0 (2016-10-22)
|
56
89
|
|
57
90
|
- Better support for date columns with `time_zone: false`
|
58
91
|
- Better date range handling for `range` option
|
59
92
|
|
60
|
-
## 3.0.2
|
93
|
+
## 3.0.2 (2016-08-09)
|
61
94
|
|
62
95
|
- Fixed `group_by_period` with associations
|
63
96
|
- Fixed `week_start` option for enumerables
|
64
97
|
|
65
|
-
## 3.0.1
|
98
|
+
## 3.0.1 (2016-07-13)
|
66
99
|
|
67
100
|
- Added support for Redshift
|
68
101
|
- Fix for infinite loop in certain cases for Rails 5
|
69
102
|
|
70
|
-
## 3.0.0
|
103
|
+
## 3.0.0 (2016-05-30)
|
71
104
|
|
72
105
|
Breaking changes
|
73
106
|
|
@@ -75,16 +108,16 @@ Breaking changes
|
|
75
108
|
- Array and hash methods no longer return the entire series by default. Use `series: true` for the previous behavior.
|
76
109
|
- The `series: false` option now returns the correct types and order, and plays nicely with other options.
|
77
110
|
|
78
|
-
## 2.5.3
|
111
|
+
## 2.5.3 (2016-04-28)
|
79
112
|
|
80
113
|
- All tests green with `mysql` gem
|
81
114
|
- Added support for decimal day start
|
82
115
|
|
83
|
-
## 2.5.2
|
116
|
+
## 2.5.2 (2016-02-16)
|
84
117
|
|
85
118
|
- Added `dates` option to return dates for day, week, month, quarter, and year
|
86
119
|
|
87
|
-
## 2.5.1
|
120
|
+
## 2.5.1 (2016-02-03)
|
88
121
|
|
89
122
|
- Added `group_by_quarter`
|
90
123
|
- Added `default_value` option
|
@@ -92,13 +125,13 @@ Breaking changes
|
|
92
125
|
- Raise `ArgumentError` if no field specified
|
93
126
|
- Added support for ActiveRecord 5 beta
|
94
127
|
|
95
|
-
## 2.5.0
|
128
|
+
## 2.5.0 (2015-09-29)
|
96
129
|
|
97
130
|
- Added `group_by_period` method
|
98
131
|
- Added `current` option
|
99
132
|
- Raise `ArgumentError` if no block given to enumerable
|
100
133
|
|
101
|
-
## 2.4.0
|
134
|
+
## 2.4.0 (2014-12-28)
|
102
135
|
|
103
136
|
- Added localization
|
104
137
|
- Added `carry_forward` option
|
@@ -106,77 +139,77 @@ Breaking changes
|
|
106
139
|
- Fixed issue w/ Brasilia Summer Time
|
107
140
|
- Fixed issues w/ ActiveRecord 4.2
|
108
141
|
|
109
|
-
## 2.3.0
|
142
|
+
## 2.3.0 (2014-08-31)
|
110
143
|
|
111
144
|
- Raise error when ActiveRecord::Base.default_timezone is not `:utc`
|
112
145
|
- Added `day_of_month`
|
113
146
|
- Added `month_of_year`
|
114
147
|
- Do not quote column name
|
115
148
|
|
116
|
-
## 2.2.1
|
149
|
+
## 2.2.1 (2014-06-23)
|
117
150
|
|
118
151
|
- Fixed ActiveRecord 3 associations
|
119
152
|
|
120
|
-
## 2.2.0
|
153
|
+
## 2.2.0 (2014-06-22)
|
121
154
|
|
122
155
|
- Added support for arrays and hashes
|
123
156
|
|
124
|
-
## 2.1.1
|
157
|
+
## 2.1.1 (2014-05-17)
|
125
158
|
|
126
159
|
- Fixed format option with multiple groups
|
127
160
|
- Better error message if time zone support is missing for MySQL
|
128
161
|
|
129
|
-
## 2.1.0
|
162
|
+
## 2.1.0 (2014-03-16)
|
130
163
|
|
131
164
|
- Added last option
|
132
165
|
- Added format option
|
133
166
|
|
134
|
-
## 2.0.4
|
167
|
+
## 2.0.4 (2014-03-12)
|
135
168
|
|
136
169
|
- Added multiple groups
|
137
170
|
- Added order
|
138
171
|
- Subsequent methods no longer modify relation
|
139
172
|
|
140
|
-
## 2.0.3
|
173
|
+
## 2.0.3 (2014-03-11)
|
141
174
|
|
142
175
|
- Implemented respond_to?
|
143
176
|
|
144
|
-
## 2.0.2
|
177
|
+
## 2.0.2 (2014-03-11)
|
145
178
|
|
146
179
|
- where, joins, and includes no longer need to be before the group_by method
|
147
180
|
|
148
|
-
## 2.0.1
|
181
|
+
## 2.0.1 (2014-03-07)
|
149
182
|
|
150
183
|
- Use time zone instead of UTC for results
|
151
184
|
|
152
|
-
## 2.0.0
|
185
|
+
## 2.0.0 (2014-03-07)
|
153
186
|
|
154
187
|
- Returns entire series by default
|
155
188
|
- Added day_start option
|
156
189
|
- Better interface
|
157
190
|
|
158
|
-
## 1.0.5
|
191
|
+
## 1.0.5 (2014-03-06)
|
159
192
|
|
160
193
|
- Added global time_zone option
|
161
194
|
|
162
|
-
## 1.0.4
|
195
|
+
## 1.0.4 (2013-07-20)
|
163
196
|
|
164
197
|
- Added global week_start option
|
165
198
|
- Fixed bug with NULL values and series
|
166
199
|
|
167
|
-
## 1.0.3
|
200
|
+
## 1.0.3 (2013-07-05)
|
168
201
|
|
169
202
|
- Fixed deprecation warning when used with will_paginate
|
170
203
|
- Fixed bug with DateTime series
|
171
204
|
|
172
|
-
## 1.0.2
|
205
|
+
## 1.0.2 (2013-06-10)
|
173
206
|
|
174
207
|
- Added :start option for custom week start for group_by_week
|
175
208
|
|
176
|
-
## 1.0.1
|
209
|
+
## 1.0.1 (2013-06-03)
|
177
210
|
|
178
211
|
- Fixed series for Rails < 3.2 and MySQL
|
179
212
|
|
180
|
-
## 1.0.0
|
213
|
+
## 1.0.0 (2013-05-15)
|
181
214
|
|
182
215
|
- First major release
|
data/CONTRIBUTING.md
CHANGED
@@ -57,7 +57,7 @@ brew services start mysql
|
|
57
57
|
|
58
58
|
# create databases
|
59
59
|
createdb groupdate_test
|
60
|
-
|
60
|
+
mysqladmin create groupdate_test
|
61
61
|
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
62
62
|
|
63
63
|
# clone the repo and run the tests
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -32,9 +32,9 @@ For MySQL and SQLite, also follow [these instructions](#additional-instructions)
|
|
32
32
|
```ruby
|
33
33
|
User.group_by_day(:created_at).count
|
34
34
|
# {
|
35
|
-
# Sat,
|
36
|
-
# Sun,
|
37
|
-
# Mon,
|
35
|
+
# Sat, 24 May 2020 => 50,
|
36
|
+
# Sun, 25 May 2020 => 100,
|
37
|
+
# Mon, 26 May 2020 => 34
|
38
38
|
# }
|
39
39
|
```
|
40
40
|
|
@@ -53,12 +53,14 @@ You can group by:
|
|
53
53
|
|
54
54
|
and
|
55
55
|
|
56
|
+
- minute_of_hour
|
56
57
|
- hour_of_day
|
57
58
|
- day_of_week (Sunday = 0, Monday = 1, etc)
|
58
59
|
- day_of_month
|
60
|
+
- day_of_year
|
59
61
|
- month_of_year
|
60
62
|
|
61
|
-
Use it anywhere you can use `group`. Works with `count`, `sum`, `minimum`, `maximum`, and `average`. For `median`, check out [ActiveMedian](https://github.com/ankane/active_median).
|
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).
|
62
64
|
|
63
65
|
### Time Zones
|
64
66
|
|
@@ -73,9 +75,9 @@ or
|
|
73
75
|
```ruby
|
74
76
|
User.group_by_week(:created_at, time_zone: "Pacific Time (US & Canada)").count
|
75
77
|
# {
|
76
|
-
# Sun,
|
77
|
-
# Sun,
|
78
|
-
# Sun,
|
78
|
+
# Sun, 08 Mar 2020 => 70,
|
79
|
+
# Sun, 15 Mar 2020 => 54,
|
80
|
+
# Sun, 22 Mar 2020 => 80
|
79
81
|
# }
|
80
82
|
```
|
81
83
|
|
@@ -86,13 +88,13 @@ Time zone objects also work. To see a list of available time zones in Rails, run
|
|
86
88
|
Weeks start on Sunday by default. Change this with:
|
87
89
|
|
88
90
|
```ruby
|
89
|
-
Groupdate.week_start = :
|
91
|
+
Groupdate.week_start = :monday
|
90
92
|
```
|
91
93
|
|
92
94
|
or
|
93
95
|
|
94
96
|
```ruby
|
95
|
-
User.group_by_week(:created_at, week_start: :
|
97
|
+
User.group_by_week(:created_at, week_start: :monday).count
|
96
98
|
```
|
97
99
|
|
98
100
|
### Day Start
|
@@ -146,8 +148,8 @@ To get keys in a different format, use:
|
|
146
148
|
```ruby
|
147
149
|
User.group_by_month(:created_at, format: "%b %Y").count
|
148
150
|
# {
|
149
|
-
# "Jan
|
150
|
-
# "Feb
|
151
|
+
# "Jan 2020" => 10
|
152
|
+
# "Feb 2020" => 12
|
151
153
|
# }
|
152
154
|
```
|
153
155
|
|
@@ -192,6 +194,14 @@ User.group_by_period(params[:period], :created_at, permit: ["day", "week"]).coun
|
|
192
194
|
|
193
195
|
Raises an `ArgumentError` for unpermitted periods.
|
194
196
|
|
197
|
+
### Custom Duration
|
198
|
+
|
199
|
+
To group by a specific number of minutes or seconds, use:
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
User.group_by_minute(:created_at, n: 10).count # 10 minutes
|
203
|
+
```
|
204
|
+
|
195
205
|
### Date Columns
|
196
206
|
|
197
207
|
If grouping on date columns which don’t need time zone conversion, use:
|
@@ -225,17 +235,23 @@ Supports the same options as above
|
|
225
235
|
users.group_by_day(time_zone: time_zone) { |u| u.created_at }
|
226
236
|
```
|
227
237
|
|
238
|
+
Get the entire series with:
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
users.group_by_day(series: true) { |u| u.created_at }
|
242
|
+
```
|
243
|
+
|
228
244
|
Count
|
229
245
|
|
230
246
|
```ruby
|
231
|
-
|
247
|
+
users.group_by_day { |u| u.created_at }.map { |k, v| [k, v.count] }.to_h
|
232
248
|
```
|
233
249
|
|
234
250
|
## Additional Instructions
|
235
251
|
|
236
252
|
### For MySQL
|
237
253
|
|
238
|
-
[Time zone support](https://dev.mysql.com/doc/refman/
|
254
|
+
[Time zone support](https://dev.mysql.com/doc/refman/8.0/en/time-zone-support.html) must be installed on the server.
|
239
255
|
|
240
256
|
```sh
|
241
257
|
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
@@ -256,7 +272,7 @@ It should return the time instead of `NULL`.
|
|
256
272
|
Groupdate has limited support for SQLite.
|
257
273
|
|
258
274
|
- No time zone support
|
259
|
-
- No `day_start`
|
275
|
+
- No `day_start` option
|
260
276
|
- No `group_by_quarter` method
|
261
277
|
|
262
278
|
If your application’s time zone is set to something other than `Etc/UTC` (the default), create an initializer with:
|
@@ -267,36 +283,18 @@ Groupdate.time_zone = false
|
|
267
283
|
|
268
284
|
## Upgrading
|
269
285
|
|
270
|
-
###
|
271
|
-
|
272
|
-
Groupdate 4.0 brings a number of improvements. Here are a few to be aware of:
|
273
|
-
|
274
|
-
- `group_by` methods return an `ActiveRecord::Relation` instead of a `Groupdate::Series`
|
275
|
-
- Invalid options now throw an `ArgumentError`
|
276
|
-
- `week_start` now affects `day_of_week`
|
277
|
-
- Custom calculation methods are supported by default
|
278
|
-
|
279
|
-
### 3.0
|
286
|
+
### 5.0
|
280
287
|
|
281
|
-
Groupdate
|
288
|
+
Groupdate 5.0 brings a number of improvements. Here are a few to be aware of:
|
282
289
|
|
283
|
-
- `
|
284
|
-
-
|
285
|
-
-
|
286
|
-
|
287
|
-
### 2.0
|
288
|
-
|
289
|
-
Groupdate 2.0 brings a number of improvements. Here are two things to be aware of:
|
290
|
-
|
291
|
-
- the entire series is returned by default
|
292
|
-
- `ActiveSupport::TimeWithZone` keys are now returned for every database adapter - adapters previously returned `Time` or `String` keys
|
290
|
+
- The `week_start` option is now supported for SQLite
|
291
|
+
- The `day_start` option is now consistent between Active Record and enumerable
|
292
|
+
- Deprecated positional arguments for time zone and range have been removed
|
293
293
|
|
294
294
|
## History
|
295
295
|
|
296
296
|
View the [changelog](https://github.com/ankane/groupdate/blob/master/CHANGELOG.md)
|
297
297
|
|
298
|
-
Groupdate follows [Semantic Versioning](https://semver.org/)
|
299
|
-
|
300
298
|
## Contributing
|
301
299
|
|
302
300
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
data/lib/groupdate.rb
CHANGED
@@ -1,18 +1,21 @@
|
|
1
|
+
# dependencies
|
1
2
|
require "active_support/core_ext/module/attribute_accessors"
|
2
3
|
require "active_support/time"
|
3
|
-
|
4
|
+
|
5
|
+
# modules
|
6
|
+
require "groupdate/magic"
|
4
7
|
require "groupdate/relation_builder"
|
5
8
|
require "groupdate/series_builder"
|
6
|
-
require "groupdate/
|
9
|
+
require "groupdate/version"
|
7
10
|
|
8
11
|
module Groupdate
|
9
12
|
class Error < RuntimeError; end
|
10
13
|
|
11
|
-
PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :minute_of_hour, :day_of_month, :month_of_year]
|
14
|
+
PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :minute_of_hour, :day_of_month, :day_of_year, :month_of_year]
|
12
15
|
METHODS = PERIODS.map { |v| :"group_by_#{v}" } + [:group_by_period]
|
13
16
|
|
14
17
|
mattr_accessor :week_start, :day_start, :time_zone, :dates
|
15
|
-
self.week_start = :
|
18
|
+
self.week_start = :sunday
|
16
19
|
self.day_start = 0
|
17
20
|
self.dates = true
|
18
21
|
|