micro-lite-rb 0.0.1
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 +7 -0
- data/groupdate-6.8.0/CHANGELOG.md +329 -0
- data/groupdate-6.8.0/CONTRIBUTING.md +75 -0
- data/groupdate-6.8.0/LICENSE.txt +22 -0
- data/groupdate-6.8.0/README.md +284 -0
- data/groupdate-6.8.0/lib/groupdate/active_record.rb +6 -0
- data/groupdate-6.8.0/lib/groupdate/adapters/base_adapter.rb +46 -0
- data/groupdate-6.8.0/lib/groupdate/adapters/mysql_adapter.rb +63 -0
- data/groupdate-6.8.0/lib/groupdate/adapters/postgresql_adapter.rb +46 -0
- data/groupdate-6.8.0/lib/groupdate/adapters/sqlite_adapter.rb +86 -0
- data/groupdate-6.8.0/lib/groupdate/enumerable.rb +25 -0
- data/groupdate-6.8.0/lib/groupdate/magic.rb +269 -0
- data/groupdate-6.8.0/lib/groupdate/query_methods.rb +18 -0
- data/groupdate-6.8.0/lib/groupdate/relation.rb +19 -0
- data/groupdate-6.8.0/lib/groupdate/series_builder.rb +309 -0
- data/groupdate-6.8.0/lib/groupdate/version.rb +3 -0
- data/groupdate-6.8.0/lib/groupdate.rb +54 -0
- data/micro-lite-rb.gemspec +12 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7cf5a8c4794e2d8c3c22daab33664cd044e0cb89bbe941547fd798dbbcd0851c
|
|
4
|
+
data.tar.gz: ed1f789c5eab989882ec83c74a78c19c44c1112d1037850aef6fa92a3fa1c6bf
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 00fed851bf13aab78e210ba59ca32c5e3ba86a723ab5ed105094b2c60b13b6e56419e6979a069f97e2e530609c4bf84b8c5ebc3f9060ea27a321fc8c424f22a6
|
|
7
|
+
data.tar.gz: 48be2388d21865b6493f6014b2b4ea0932e2c7f3161d3c4fce7f954bb9089e3bbe453e38b884e2b93b83bae21fa76e215aab9466a992fe82aa47ac8696e856fb
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
## 6.8.0 (2026-04-04)
|
|
2
|
+
|
|
3
|
+
- Dropped support for Ruby < 3.3 and Active Record < 7.2
|
|
4
|
+
|
|
5
|
+
## 6.7.0 (2025-06-02)
|
|
6
|
+
|
|
7
|
+
- Added time zone support for SQLite
|
|
8
|
+
- Added `day_start` support for SQLite
|
|
9
|
+
- Added `group_by_quarter` support for SQLite
|
|
10
|
+
|
|
11
|
+
## 6.6.0 (2025-05-04)
|
|
12
|
+
|
|
13
|
+
- Dropped support for Ruby < 3.2 and Active Record < 7.1
|
|
14
|
+
|
|
15
|
+
## 6.5.1 (2024-10-07)
|
|
16
|
+
|
|
17
|
+
- Fixed connection leasing for Active Record 7.2+
|
|
18
|
+
|
|
19
|
+
## 6.5.0 (2024-10-01)
|
|
20
|
+
|
|
21
|
+
- Added support for Active Record 8
|
|
22
|
+
- Dropped support for Ruby < 3.1 and Active Record < 7
|
|
23
|
+
|
|
24
|
+
## 6.4.0 (2023-09-13)
|
|
25
|
+
|
|
26
|
+
- Added support for Trilogy
|
|
27
|
+
|
|
28
|
+
## 6.3.0 (2023-07-02)
|
|
29
|
+
|
|
30
|
+
- Dropped support for Ruby < 3 and Rails < 6.1
|
|
31
|
+
|
|
32
|
+
## 6.2.1 (2023-04-18)
|
|
33
|
+
|
|
34
|
+
- Fixed extra day with `DateTime` ranges
|
|
35
|
+
|
|
36
|
+
## 6.2.0 (2023-01-29)
|
|
37
|
+
|
|
38
|
+
- Added support for async methods with Active Record 7.1
|
|
39
|
+
|
|
40
|
+
## 6.1.0 (2022-04-05)
|
|
41
|
+
|
|
42
|
+
- Added `expand_range` option
|
|
43
|
+
|
|
44
|
+
## 6.0.1 (2022-01-16)
|
|
45
|
+
|
|
46
|
+
- Fixed incorrect results (error before 6.0) with `includes` with Active Record 6.1+
|
|
47
|
+
|
|
48
|
+
## 6.0.0 (2022-01-15)
|
|
49
|
+
|
|
50
|
+
- Raise `ActiveRecord::UnknownAttributeReference` for non-attribute arguments
|
|
51
|
+
- Raise `ArgumentError` for ranges with string bounds
|
|
52
|
+
- Added `n` option for Redshift
|
|
53
|
+
- Changed SQL to return dates instead of times for day, week, month, quarter, and year
|
|
54
|
+
- Removed `dates` option
|
|
55
|
+
- Dropped support for Ruby < 2.6 and Rails < 5.2
|
|
56
|
+
|
|
57
|
+
## 5.2.4 (2021-12-15)
|
|
58
|
+
|
|
59
|
+
- Simplified queries for Active Record 7 and MySQL
|
|
60
|
+
|
|
61
|
+
## 5.2.3 (2021-12-06)
|
|
62
|
+
|
|
63
|
+
- Fixed error and warnings with Active Record 7
|
|
64
|
+
|
|
65
|
+
## 5.2.2 (2021-02-08)
|
|
66
|
+
|
|
67
|
+
- Added support for `nil..nil` ranges in `range` option
|
|
68
|
+
|
|
69
|
+
## 5.2.1 (2020-09-09)
|
|
70
|
+
|
|
71
|
+
- Improved error message for invalid ranges
|
|
72
|
+
- Fixed bug with date string ranges
|
|
73
|
+
|
|
74
|
+
## 5.2.0 (2020-09-07)
|
|
75
|
+
|
|
76
|
+
- Added warning for non-attribute argument
|
|
77
|
+
- Added support for beginless and endless ranges in `range` option
|
|
78
|
+
|
|
79
|
+
## 5.1.0 (2020-07-30)
|
|
80
|
+
|
|
81
|
+
- Added `n` option to minute and second for custom durations
|
|
82
|
+
|
|
83
|
+
## 5.0.0 (2020-02-18)
|
|
84
|
+
|
|
85
|
+
- Added support for `week_start` for SQLite
|
|
86
|
+
- Added support for full weekday names
|
|
87
|
+
- Made `day_start` behavior consistent between Active Record and enumerable
|
|
88
|
+
- Made `last` option extend to end of current period
|
|
89
|
+
- Raise error when `day_start` and `week_start` passed to unsupported methods
|
|
90
|
+
- The `day_start` option no longer applies to shorter periods
|
|
91
|
+
- Fixed `inconsistent time zone info` errors around DST with MySQL and PostgreSQL
|
|
92
|
+
- Improved performance of `format` option
|
|
93
|
+
- Removed deprecated positional arguments for time zone and range
|
|
94
|
+
- Dropped support for `mysql` gem (last release was 2013)
|
|
95
|
+
|
|
96
|
+
## 4.3.0 (2019-12-26)
|
|
97
|
+
|
|
98
|
+
- Fixed error with empty results in Ruby 2.7
|
|
99
|
+
- Fixed deprecation warnings in Ruby 2.7
|
|
100
|
+
- Deprecated positional arguments for time zone and range
|
|
101
|
+
|
|
102
|
+
## 4.2.0 (2019-10-28)
|
|
103
|
+
|
|
104
|
+
- Added `day_of_year`
|
|
105
|
+
- Dropped support for Rails 4.2
|
|
106
|
+
|
|
107
|
+
## 4.1.2 (2019-05-26)
|
|
108
|
+
|
|
109
|
+
- Fixed error with empty data and `current: false`
|
|
110
|
+
- Fixed error in time zone check for Rails < 5.2
|
|
111
|
+
- Prevent infinite loop with endless ranges
|
|
112
|
+
|
|
113
|
+
## 4.1.1 (2018-12-11)
|
|
114
|
+
|
|
115
|
+
- Made column resolution consistent with `group`
|
|
116
|
+
- Added support for `alias_attribute`
|
|
117
|
+
|
|
118
|
+
## 4.1.0 (2018-11-04)
|
|
119
|
+
|
|
120
|
+
- Many performance improvements
|
|
121
|
+
- Added check for consistent time zone info
|
|
122
|
+
- Fixed error message for invalid queries with MySQL and SQLite
|
|
123
|
+
- Fixed issue with enumerable methods ignoring nils
|
|
124
|
+
|
|
125
|
+
## 4.0.2 (2018-10-15)
|
|
126
|
+
|
|
127
|
+
- Make `current` option work without `last`
|
|
128
|
+
- 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)
|
|
129
|
+
|
|
130
|
+
## 4.0.1 (2018-05-03)
|
|
131
|
+
|
|
132
|
+
- Fixed incorrect range with `last` option near time change
|
|
133
|
+
|
|
134
|
+
## 4.0.0 (2018-02-21)
|
|
135
|
+
|
|
136
|
+
- Custom calculation methods are supported by default - `groupdate_calculation_methods` is no longer needed
|
|
137
|
+
|
|
138
|
+
Breaking changes
|
|
139
|
+
|
|
140
|
+
- Dropped support for Rails < 4.2
|
|
141
|
+
- Invalid options now throw an `ArgumentError`
|
|
142
|
+
- `group_by` methods return an `ActiveRecord::Relation` instead of a `Groupdate::Series`
|
|
143
|
+
- `week_start` now affects `day_of_week`
|
|
144
|
+
- Removed support for `reverse_order` (was never supported in Rails 5)
|
|
145
|
+
|
|
146
|
+
## 3.2.1 (2018-02-21)
|
|
147
|
+
|
|
148
|
+
- Added `minute_of_hour`
|
|
149
|
+
- Added support for `unscoped`
|
|
150
|
+
|
|
151
|
+
## 3.2.0 (2017-01-30)
|
|
152
|
+
|
|
153
|
+
- Added limited support for SQLite
|
|
154
|
+
|
|
155
|
+
## 3.1.1 (2016-10-25)
|
|
156
|
+
|
|
157
|
+
- Fixed `current: false`
|
|
158
|
+
- Fixed `last` with `group_by_quarter`
|
|
159
|
+
- Raise `ArgumentError` when `last` option is not supported
|
|
160
|
+
|
|
161
|
+
## 3.1.0 (2016-10-22)
|
|
162
|
+
|
|
163
|
+
- Better support for date columns with `time_zone: false`
|
|
164
|
+
- Better date range handling for `range` option
|
|
165
|
+
|
|
166
|
+
## 3.0.2 (2016-08-09)
|
|
167
|
+
|
|
168
|
+
- Fixed `group_by_period` with associations
|
|
169
|
+
- Fixed `week_start` option for enumerables
|
|
170
|
+
|
|
171
|
+
## 3.0.1 (2016-07-13)
|
|
172
|
+
|
|
173
|
+
- Added support for Redshift
|
|
174
|
+
- Fix for infinite loop in certain cases for Rails 5
|
|
175
|
+
|
|
176
|
+
## 3.0.0 (2016-05-30)
|
|
177
|
+
|
|
178
|
+
Breaking changes
|
|
179
|
+
|
|
180
|
+
- `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`.
|
|
181
|
+
- Array and hash methods no longer return the entire series by default. Use `series: true` for the previous behavior.
|
|
182
|
+
- The `series: false` option now returns the correct types and order, and plays nicely with other options.
|
|
183
|
+
|
|
184
|
+
## 2.5.3 (2016-04-28)
|
|
185
|
+
|
|
186
|
+
- All tests green with `mysql` gem
|
|
187
|
+
- Added support for decimal day start
|
|
188
|
+
|
|
189
|
+
## 2.5.2 (2016-02-16)
|
|
190
|
+
|
|
191
|
+
- Added `dates` option to return dates for day, week, month, quarter, and year
|
|
192
|
+
|
|
193
|
+
## 2.5.1 (2016-02-03)
|
|
194
|
+
|
|
195
|
+
- Added `group_by_quarter`
|
|
196
|
+
- Added `default_value` option
|
|
197
|
+
- Accept symbol for `format` option
|
|
198
|
+
- Raise `ArgumentError` if no field specified
|
|
199
|
+
- Added support for Active Record 5 beta
|
|
200
|
+
|
|
201
|
+
## 2.5.0 (2015-09-29)
|
|
202
|
+
|
|
203
|
+
- Added `group_by_period` method
|
|
204
|
+
- Added `current` option
|
|
205
|
+
- Raise `ArgumentError` if no block given to enumerable
|
|
206
|
+
|
|
207
|
+
## 2.4.0 (2014-12-28)
|
|
208
|
+
|
|
209
|
+
- Added localization
|
|
210
|
+
- Added `carry_forward` option
|
|
211
|
+
- Added `series: false` option for arrays and hashes
|
|
212
|
+
- Fixed issue w/ Brasilia Summer Time
|
|
213
|
+
- Fixed issues w/ Active Record 4.2
|
|
214
|
+
|
|
215
|
+
## 2.3.0 (2014-08-31)
|
|
216
|
+
|
|
217
|
+
- Raise error when `ActiveRecord::Base.default_timezone` is not `:utc`
|
|
218
|
+
- Added `day_of_month`
|
|
219
|
+
- Added `month_of_year`
|
|
220
|
+
- Do not quote column name
|
|
221
|
+
|
|
222
|
+
## 2.2.1 (2014-06-23)
|
|
223
|
+
|
|
224
|
+
- Fixed Active Record 3 associations
|
|
225
|
+
|
|
226
|
+
## 2.2.0 (2014-06-22)
|
|
227
|
+
|
|
228
|
+
- Added support for arrays and hashes
|
|
229
|
+
|
|
230
|
+
## 2.1.1 (2014-05-17)
|
|
231
|
+
|
|
232
|
+
- Fixed format option with multiple groups
|
|
233
|
+
- Better error message if time zone support is missing for MySQL
|
|
234
|
+
|
|
235
|
+
## 2.1.0 (2014-03-16)
|
|
236
|
+
|
|
237
|
+
- Added last option
|
|
238
|
+
- Added format option
|
|
239
|
+
|
|
240
|
+
## 2.0.4 (2014-03-12)
|
|
241
|
+
|
|
242
|
+
- Added multiple groups
|
|
243
|
+
- Added order
|
|
244
|
+
- Subsequent methods no longer modify relation
|
|
245
|
+
|
|
246
|
+
## 2.0.3 (2014-03-11)
|
|
247
|
+
|
|
248
|
+
- Implemented respond_to?
|
|
249
|
+
|
|
250
|
+
## 2.0.2 (2014-03-11)
|
|
251
|
+
|
|
252
|
+
- where, joins, and includes no longer need to be before the group_by method
|
|
253
|
+
|
|
254
|
+
## 2.0.1 (2014-03-07)
|
|
255
|
+
|
|
256
|
+
- Use time zone instead of UTC for results
|
|
257
|
+
|
|
258
|
+
## 2.0.0 (2014-03-07)
|
|
259
|
+
|
|
260
|
+
- Returns entire series by default
|
|
261
|
+
- Added day_start option
|
|
262
|
+
- Better interface
|
|
263
|
+
|
|
264
|
+
## 1.0.5 (2014-03-06)
|
|
265
|
+
|
|
266
|
+
- Added global time_zone option
|
|
267
|
+
|
|
268
|
+
## 1.0.4 (2013-07-20)
|
|
269
|
+
|
|
270
|
+
- Added global week_start option
|
|
271
|
+
- Fixed bug with NULL values and series
|
|
272
|
+
|
|
273
|
+
## 1.0.3 (2013-07-05)
|
|
274
|
+
|
|
275
|
+
- Fixed deprecation warning when used with will_paginate
|
|
276
|
+
- Fixed bug with DateTime series
|
|
277
|
+
|
|
278
|
+
## 1.0.2 (2013-06-10)
|
|
279
|
+
|
|
280
|
+
- Added :start option for custom week start for group_by_week
|
|
281
|
+
|
|
282
|
+
## 1.0.1 (2013-06-03)
|
|
283
|
+
|
|
284
|
+
- Fixed series for Rails < 3.2 and MySQL
|
|
285
|
+
|
|
286
|
+
## 1.0.0 (2013-05-15)
|
|
287
|
+
|
|
288
|
+
- First major release
|
|
289
|
+
|
|
290
|
+
## 0.1.6 (2013-05-07)
|
|
291
|
+
|
|
292
|
+
- Fixed error with Ruby 1.8
|
|
293
|
+
|
|
294
|
+
## 0.1.5 (2013-05-01)
|
|
295
|
+
|
|
296
|
+
- Added support for getting the entire series
|
|
297
|
+
|
|
298
|
+
## 0.1.4 (2013-05-01)
|
|
299
|
+
|
|
300
|
+
- Added support for Ruby 1.8
|
|
301
|
+
|
|
302
|
+
## 0.1.3 (2013-04-25)
|
|
303
|
+
|
|
304
|
+
- Improved field names for ordering
|
|
305
|
+
|
|
306
|
+
## 0.1.2 (2013-04-24)
|
|
307
|
+
|
|
308
|
+
- Added ability to order easily
|
|
309
|
+
|
|
310
|
+
## 0.1.1 (2013-04-21)
|
|
311
|
+
|
|
312
|
+
- Improved return types for MySQL
|
|
313
|
+
|
|
314
|
+
## 0.1.0 (2013-04-21)
|
|
315
|
+
|
|
316
|
+
- Added `day_of_week`
|
|
317
|
+
- Added `hour_of_day`
|
|
318
|
+
|
|
319
|
+
## 0.0.3 (2013-04-18)
|
|
320
|
+
|
|
321
|
+
- Added support for Rails time zone strings
|
|
322
|
+
|
|
323
|
+
## 0.0.2 (2013-04-18)
|
|
324
|
+
|
|
325
|
+
- Added tests
|
|
326
|
+
|
|
327
|
+
## 0.0.1 (2013-04-17)
|
|
328
|
+
|
|
329
|
+
- First release
|
|
@@ -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.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013-2025 Andrew Kane
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# Groupdate
|
|
2
|
+
|
|
3
|
+
The simplest way to group by:
|
|
4
|
+
|
|
5
|
+
- day
|
|
6
|
+
- week
|
|
7
|
+
- hour of the day
|
|
8
|
+
- and more (complete list below)
|
|
9
|
+
|
|
10
|
+
:tada: Time zones - including daylight saving time - supported!! **the best part**
|
|
11
|
+
|
|
12
|
+
:cake: Get the entire series - **the other best part**
|
|
13
|
+
|
|
14
|
+
Supports PostgreSQL, MySQL, MariaDB, SQLite, and Redshift, plus arrays and hashes
|
|
15
|
+
|
|
16
|
+
:cupid: Goes hand in hand with [Chartkick](https://www.chartkick.com)
|
|
17
|
+
|
|
18
|
+
[](https://github.com/ankane/groupdate/actions)
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Add this line to your application’s Gemfile:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem "groupdate"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
For MySQL and MariaDB, also follow [these instructions](#additional-instructions).
|
|
29
|
+
|
|
30
|
+
## Getting Started
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
User.group_by_day(:created_at).count
|
|
34
|
+
# {
|
|
35
|
+
# Wed, 01 Jan 2025 => 50,
|
|
36
|
+
# Thu, 02 Jan 2025 => 100,
|
|
37
|
+
# Fri, 03 Jan 2025 => 34
|
|
38
|
+
# }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Results are returned in ascending order by default, so no need to sort.
|
|
42
|
+
|
|
43
|
+
You can group by:
|
|
44
|
+
|
|
45
|
+
- second
|
|
46
|
+
- minute
|
|
47
|
+
- hour
|
|
48
|
+
- day
|
|
49
|
+
- week
|
|
50
|
+
- month
|
|
51
|
+
- quarter
|
|
52
|
+
- year
|
|
53
|
+
|
|
54
|
+
and
|
|
55
|
+
|
|
56
|
+
- minute_of_hour
|
|
57
|
+
- hour_of_day
|
|
58
|
+
- day_of_week (Sunday = 0, Monday = 1, etc)
|
|
59
|
+
- day_of_month
|
|
60
|
+
- day_of_year
|
|
61
|
+
- month_of_year
|
|
62
|
+
|
|
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).
|
|
64
|
+
|
|
65
|
+
### Time Zones
|
|
66
|
+
|
|
67
|
+
The default time zone is `Time.zone`. Change this with:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
Groupdate.time_zone = "Pacific Time (US & Canada)"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
or
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
User.group_by_week(:created_at, time_zone: "Pacific Time (US & Canada)").count
|
|
77
|
+
# {
|
|
78
|
+
# Sun, 05 Jan 2025 => 70,
|
|
79
|
+
# Sun, 12 Jan 2025 => 54,
|
|
80
|
+
# Sun, 19 Jan 2025 => 80
|
|
81
|
+
# }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Time zone objects also work. To see a list of available time zones in Rails, run `rake time:zones:all`.
|
|
85
|
+
|
|
86
|
+
### Week Start
|
|
87
|
+
|
|
88
|
+
Weeks start on Sunday by default. Change this with:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
Groupdate.week_start = :monday
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
or
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
User.group_by_week(:created_at, week_start: :monday).count
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Day Start
|
|
101
|
+
|
|
102
|
+
You can change the hour days start with:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
Groupdate.day_start = 2 # 2 am - 2 am
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
or
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
User.group_by_day(:created_at, day_start: 2).count
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Time Range
|
|
115
|
+
|
|
116
|
+
To get a specific time range, use:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
User.group_by_day(:created_at, range: 2.weeks.ago.midnight..Time.now).count
|
|
120
|
+
```
|
|
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
|
+
|
|
128
|
+
To get the most recent time periods, use:
|
|
129
|
+
|
|
130
|
+
```ruby
|
|
131
|
+
User.group_by_week(:created_at, last: 8).count # last 8 weeks
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
To exclude the current period, use:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
User.group_by_week(:created_at, last: 8, current: false).count
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Order
|
|
141
|
+
|
|
142
|
+
You can order in descending order with:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
User.group_by_day(:created_at, reverse: true).count
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Keys
|
|
149
|
+
|
|
150
|
+
Keys are returned as date or time objects for the start of the period.
|
|
151
|
+
|
|
152
|
+
To get keys in a different format, use:
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
User.group_by_month(:created_at, format: "%b %Y").count
|
|
156
|
+
# {
|
|
157
|
+
# "Jan 2025" => 10
|
|
158
|
+
# "Feb 2025" => 12
|
|
159
|
+
# }
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
or
|
|
163
|
+
|
|
164
|
+
```ruby
|
|
165
|
+
User.group_by_hour_of_day(:created_at, format: "%-l %P").count
|
|
166
|
+
# {
|
|
167
|
+
# "12 am" => 15,
|
|
168
|
+
# "1 am" => 11
|
|
169
|
+
# ...
|
|
170
|
+
# }
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Takes a `String`, which is passed to [strftime](https://www.strfti.me/), or a `Symbol`, which is looked up by `I18n.localize` in `i18n` scope 'time.formats', or a `Proc`. You can pass a locale with the `locale` option.
|
|
174
|
+
|
|
175
|
+
### Series
|
|
176
|
+
|
|
177
|
+
The entire series is returned by default. To exclude points without data, use:
|
|
178
|
+
|
|
179
|
+
```ruby
|
|
180
|
+
User.group_by_day(:created_at, series: false).count
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Or change the default value with:
|
|
184
|
+
|
|
185
|
+
```ruby
|
|
186
|
+
User.group_by_day(:created_at, default_value: "missing").count
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Dynamic Grouping
|
|
190
|
+
|
|
191
|
+
```ruby
|
|
192
|
+
User.group_by_period(:day, :created_at).count
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Limit groupings with the `permit` option.
|
|
196
|
+
|
|
197
|
+
```ruby
|
|
198
|
+
User.group_by_period(params[:period], :created_at, permit: ["day", "week"]).count
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Raises an `ArgumentError` for unpermitted periods.
|
|
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
|
+
|
|
211
|
+
### Date Columns
|
|
212
|
+
|
|
213
|
+
If grouping on date columns which don’t need time zone conversion, use:
|
|
214
|
+
|
|
215
|
+
```ruby
|
|
216
|
+
User.group_by_week(:created_on, time_zone: false).count
|
|
217
|
+
```
|
|
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
|
+
|
|
227
|
+
## Arrays and Hashes
|
|
228
|
+
|
|
229
|
+
```ruby
|
|
230
|
+
users.group_by_day { |u| u.created_at } # or group_by_day(&:created_at)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
Supports the same options as above
|
|
234
|
+
|
|
235
|
+
```ruby
|
|
236
|
+
users.group_by_day(time_zone: time_zone) { |u| u.created_at }
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Get the entire series with:
|
|
240
|
+
|
|
241
|
+
```ruby
|
|
242
|
+
users.group_by_day(series: true) { |u| u.created_at }
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Count
|
|
246
|
+
|
|
247
|
+
```ruby
|
|
248
|
+
users.group_by_day { |u| u.created_at }.to_h { |k, v| [k, v.count] }
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Additional Instructions
|
|
252
|
+
|
|
253
|
+
<a name="for-mysql"></a>
|
|
254
|
+
|
|
255
|
+
### For MySQL and MariaDB
|
|
256
|
+
|
|
257
|
+
[Time zone support](https://dev.mysql.com/doc/refman/8.0/en/time-zone-support.html) must be installed on the server.
|
|
258
|
+
|
|
259
|
+
```sh
|
|
260
|
+
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
You can confirm it worked with:
|
|
264
|
+
|
|
265
|
+
```sql
|
|
266
|
+
SELECT CONVERT_TZ(NOW(), '+00:00', 'Pacific/Honolulu');
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
It should return the time instead of `NULL`.
|
|
270
|
+
|
|
271
|
+
## History
|
|
272
|
+
|
|
273
|
+
View the [changelog](https://github.com/ankane/groupdate/blob/master/CHANGELOG.md)
|
|
274
|
+
|
|
275
|
+
## Contributing
|
|
276
|
+
|
|
277
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
|
278
|
+
|
|
279
|
+
- [Report bugs](https://github.com/ankane/groupdate/issues)
|
|
280
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/groupdate/pulls)
|
|
281
|
+
- Write, clarify, or fix documentation
|
|
282
|
+
- Suggest or add new features
|
|
283
|
+
|
|
284
|
+
To get started with development and testing, check out the [Contributing Guide](CONTRIBUTING.md).
|