groupdate 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daf83ef79d462a5e64a5718a32473030971ac38c
4
- data.tar.gz: f7223697deffaeca83fff949081837397da1aaea
3
+ metadata.gz: 16e0f474a6785462e83ab5811c44e54bc71689ce
4
+ data.tar.gz: 7b66609e83996472c4e501f1502d4b76f73f4908
5
5
  SHA512:
6
- metadata.gz: 20e08fd6c4737c5624fbee1bb3651af4b50b30d863ce50d3a3ef6e574bfdc8b3b072149cff799bd62eb7d6a38a4d474b376f0f9caaff5e35fb226abcd0f72b4b
7
- data.tar.gz: 7ef016743a24bab4dcc51b69f4a48cdd7670eb86165f4158f0dd3c3f56c8c720e92bbcfce1952cfbb0b72add483ec4492f973953c8acd5942a178808e05cc23a
6
+ metadata.gz: 260e9bf32e334238baa95e59b085418aedc824046312699d69827f6b54a736287d68a70a87bb22a62f936b5336efe0f60012c59e1d1d9babc0c9b5434702767c
7
+ data.tar.gz: 5fe2dab3320cb3b0b119191440a490d967a12174290afa872d499858173c9a0c099867fff28e018369a3a8d8bc0e1afd2bc532ebcfa70c649e6dbbc4ae7a79f3
data/.gitignore CHANGED
@@ -3,8 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
- *.gemfile.lock
6
+ *.lock
8
7
  InstalledFiles
9
8
  _yardoc
10
9
  coverage
data/.travis.yml CHANGED
@@ -1,13 +1,14 @@
1
1
  language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
- - 2.0.0
4
+ - 2.1
5
5
  - jruby
6
6
  gemfile:
7
7
  - Gemfile
8
8
  - gemfiles/activerecord31.gemfile
9
9
  - gemfiles/activerecord32.gemfile
10
10
  - gemfiles/activerecord40.gemfile
11
+ - gemfiles/activerecord41.gemfile
11
12
  script: bundle exec rake test
12
13
  before_script:
13
14
  - mysql -e 'create database groupdate_test;'
@@ -17,3 +18,7 @@ notifications:
17
18
  email:
18
19
  on_success: never
19
20
  on_failure: change
21
+ matrix:
22
+ allow_failures:
23
+ - rvm: jruby
24
+ gemfile: Gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 2.4.0
2
+
3
+ - Added localization
4
+ - Added `carry_forward` option
5
+ - Added `series: false` option for arrays and hashes
6
+ - Fixed issue w/ Brasilia Summer Time
7
+ - Fixed issues w/ ActiveRecord 4.2
8
+
1
9
  ## 2.3.0
2
10
 
3
11
  - Raise error when ActiveRecord::Base.default_timezone is not `:utc`
@@ -11,7 +19,7 @@
11
19
 
12
20
  ## 2.2.0
13
21
 
14
- - Added support for Arrays and Hashes
22
+ - Added support for arrays and hashes
15
23
 
16
24
  ## 2.1.1
17
25
 
data/Gemfile CHANGED
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in groupdate.gemspec
4
4
  gemspec
5
5
 
6
- gem "activerecord", "~> 4.1"
6
+ gem "activerecord", "~> 4.2.0"
data/README.md CHANGED
@@ -47,8 +47,8 @@ and
47
47
 
48
48
  - hour_of_day
49
49
  - day_of_week (Sunday = 0, Monday = 1, etc)
50
- - day_of_month [master]
51
- - month_of_year [master]
50
+ - day_of_month
51
+ - month_of_year
52
52
 
53
53
  Use it anywhere you can use `group`.
54
54
 
@@ -137,7 +137,7 @@ To get keys in a different format, use:
137
137
  User.group_by_hour_of_day(:created_at, format: "%l %P").count.keys.first # 12 am
138
138
  ```
139
139
 
140
- Takes a `String`, which is passed to [strftime](http://strfti.me/), or a `Proc`
140
+ Takes a `String`, which is passed to [strftime](http://strfti.me/), or a `Proc`. You can pass a locale with the `locale` option.
141
141
 
142
142
  ## Arrays and Hashes
143
143
 
@@ -175,18 +175,6 @@ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
175
175
 
176
176
  or copy and paste [these statements](https://gist.githubusercontent.com/ankane/1d6b0022173186accbf0/raw/time_zone_support.sql) into a SQL console.
177
177
 
178
- #### For JRuby
179
-
180
- Use the master version of your JDBC adapter. You will get incorrect results for versions before [this commit](https://github.com/jruby/activerecord-jdbc-adapter/commit/c1cdb7cec8d3f06fc54995e8d872d830bd0a4d91).
181
-
182
- ```ruby
183
- # postgresql
184
- gem "activerecord-jdbcpostgresql-adapter", :github => "jruby/activerecord-jdbc-adapter"
185
-
186
- # mysql
187
- gem "activerecord-jdbcmysql-adapter", :github => "jruby/activerecord-jdbc-adapter"
188
- ```
189
-
190
178
  ## Upgrading to 2.0
191
179
 
192
180
  Groupdate 2.0 brings a number a great improvements. Here are two things to be aware of:
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in searchkick.gemspec
4
4
  gemspec path: "../"
5
5
 
6
- gem "activerecord", "~> 3.1"
6
+ gem "activerecord", "~> 3.1.0"
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in searchkick.gemspec
4
4
  gemspec path: "../"
5
5
 
6
- gem "activerecord", "~> 3.2"
6
+ gem "activerecord", "~> 3.2.0"
@@ -3,4 +3,4 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in searchkick.gemspec
4
4
  gemspec path: "../"
5
5
 
6
- gem "activerecord", "~> 4.0"
6
+ gem "activerecord", "~> 4.0.0"
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in searchkick.gemspec
4
+ gemspec path: "../"
5
+
6
+ gem "activerecord", "~> 4.1.0"
data/groupdate.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
- spec.add_development_dependency "minitest", ">= 5"
25
+ spec.add_development_dependency "minitest"
26
26
  spec.add_development_dependency "activerecord"
27
27
 
28
28
  if RUBY_PLATFORM == "java"
@@ -1,3 +1,5 @@
1
+ require "i18n"
2
+
1
3
  module Groupdate
2
4
  class Magic
3
5
  attr_accessor :field, :options
@@ -16,7 +18,12 @@ module Groupdate
16
18
  end
17
19
 
18
20
  def group_by(enum, &block)
19
- series(enum.group_by{|v| v = yield(v); v ? round_time(v) : nil }, [])
21
+ group = enum.group_by{|v| v = yield(v); v ? round_time(v) : nil }
22
+ if options[:series] == false
23
+ group
24
+ else
25
+ series(group, [])
26
+ end
20
27
  end
21
28
 
22
29
  def relation(column, relation)
@@ -103,13 +110,16 @@ module Groupdate
103
110
  # undo reverse since we do not want this to appear in the query
104
111
  reverse = relation.reverse_order_value
105
112
  if reverse
106
- relation = relation.reverse_order
113
+ relation = relation.except(:reverse_order)
107
114
  end
108
115
  order = relation.order_values.first
109
116
  if order.is_a?(String)
110
117
  parts = order.split(" ")
111
- reverse_order = (parts.size == 2 && parts[0].to_sym == field && parts[1].to_s.downcase == "desc")
112
- reverse = !reverse if reverse_order
118
+ reverse_order = (parts.size == 2 && (parts[0].to_sym == field || (activerecord42? && parts[0] == "#{relation.quoted_table_name}.#{relation.quoted_primary_key}")) && parts[1].to_s.downcase == "desc")
119
+ if reverse_order
120
+ reverse = !reverse
121
+ relation = relation.reorder(relation.order_values[1..-1])
122
+ end
113
123
  end
114
124
 
115
125
  multiple_groups = relation.group_values.size > 1
@@ -198,8 +208,8 @@ module Groupdate
198
208
 
199
209
  step = 1.send(field)
200
210
 
201
- while time_range.cover?(series.last + step)
202
- series << series.last + step
211
+ while (next_step = round_time(series.last + step)) and time_range.cover?(next_step)
212
+ series << next_step
203
213
  end
204
214
 
205
215
  if multiple_groups
@@ -221,6 +231,7 @@ module Groupdate
221
231
  series = series.to_a.reverse
222
232
  end
223
233
 
234
+ locale = options[:locale] || I18n.locale
224
235
  key_format =
225
236
  if options[:format]
226
237
  if options[:format].respond_to?(:call)
@@ -238,15 +249,17 @@ module Groupdate
238
249
  when :month_of_year
239
250
  key = Date.new(2014, key, 1).to_time
240
251
  end
241
- key.strftime(options[:format].to_s)
252
+ I18n.localize(key, format: options[:format].to_s, locale: locale)
242
253
  end
243
254
  end
244
255
  else
245
256
  lambda{|k| k }
246
257
  end
247
258
 
259
+ value = 0
248
260
  Hash[series.map do |k|
249
- [multiple_groups ? k[0...@group_index] + [key_format.call(k[@group_index])] + k[(@group_index + 1)..-1] : key_format.call(k), count[k] || default_value]
261
+ value = count[k] || (@options[:carry_forward] && value) || default_value
262
+ [multiple_groups ? k[0...@group_index] + [key_format.call(k[@group_index])] + k[(@group_index + 1)..-1] : key_format.call(k), value]
250
263
  end]
251
264
  end
252
265
 
@@ -286,5 +299,9 @@ module Groupdate
286
299
  time.is_a?(Time) ? time + day_start.hours : time
287
300
  end
288
301
 
302
+ def activerecord42?
303
+ ActiveRecord::VERSION::STRING.starts_with?("4.2.")
304
+ end
305
+
289
306
  end
290
307
  end
@@ -1,3 +1,3 @@
1
1
  module Groupdate
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.0"
3
3
  end
data/test/test_helper.rb CHANGED
@@ -3,12 +3,10 @@ Bundler.require(:default)
3
3
  require "minitest/autorun"
4
4
  require "minitest/pride"
5
5
  require "logger"
6
- require "active_record"
7
6
 
8
- # TODO determine why this is necessary
9
- if RUBY_PLATFORM == "java"
10
- ENV["TZ"] = "UTC"
11
- end
7
+ Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)
8
+
9
+ ENV["TZ"] = "UTC"
12
10
 
13
11
  # for debugging
14
12
  # ActiveRecord::Base.logger = Logger.new(STDOUT)
@@ -24,6 +22,14 @@ end
24
22
  class Post < ActiveRecord::Base
25
23
  end
26
24
 
25
+ # i18n
26
+ I18n.enforce_available_locales = true
27
+ I18n.backend.store_translations :de, {
28
+ :date => {
29
+ :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
30
+ }
31
+ }
32
+
27
33
  # migrations
28
34
  %w(postgresql mysql2).each do |adapter|
29
35
  ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil
@@ -49,7 +55,11 @@ module TestGroupdate
49
55
  # second
50
56
 
51
57
  def test_second_end_of_second
52
- assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
58
+ if ActiveRecord::Base.connection.adapter_name == "Mysql2" and ActiveRecord::VERSION::STRING.starts_with?("4.2.")
59
+ skip # no millisecond precision
60
+ else
61
+ assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
62
+ end
53
63
  end
54
64
 
55
65
  def test_second_start_of_second
@@ -713,6 +723,19 @@ module TestGroupdate
713
723
  assert_format :month_of_year, "Jan", "%b"
714
724
  end
715
725
 
726
+ def test_format_locale
727
+ create_user "2014-10-01 00:00:00 UTC"
728
+ assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
729
+ end
730
+
731
+ def test_format_locale_global
732
+ create_user "2014-10-01 00:00:00 UTC"
733
+ I18n.locale = :de
734
+ assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count
735
+ ensure
736
+ I18n.locale = :en
737
+ end
738
+
716
739
  def test_format_multiple_groups
717
740
  create_user "2014-03-01 00:00:00 UTC"
718
741
  assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count
@@ -737,6 +760,28 @@ module TestGroupdate
737
760
  end
738
761
  end
739
762
 
763
+ # Brasilia Summer Time
764
+
765
+ def test_brasilia_summer_time
766
+ # must parse and convert to UTC for ActiveRecord 3.1
767
+ create_user(brasilia.parse("2014-10-19 02:00:00").utc.to_s)
768
+ create_user(brasilia.parse("2014-10-20 02:00:00").utc.to_s)
769
+ expected = {
770
+ brasilia.parse("2014-10-19 01:00:00") => 1,
771
+ brasilia.parse("2014-10-20 00:00:00") => 1
772
+ }
773
+ assert_equal expected, User.group_by_day(:created_at, time_zone: "Brasilia").count
774
+ end
775
+
776
+ # carry_forward option
777
+
778
+ def test_carry_forward
779
+ create_user "2014-05-01 00:00:00 UTC"
780
+ create_user "2014-05-01 00:00:00 UTC"
781
+ create_user "2014-05-03 00:00:00 UTC"
782
+ assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[utc.parse("2014-05-02 00:00:00 UTC")]
783
+ end
784
+
740
785
  # helpers
741
786
 
742
787
  def assert_format(method, expected, format, options = {})
@@ -778,6 +823,10 @@ module TestGroupdate
778
823
  ActiveSupport::TimeZone["UTC"]
779
824
  end
780
825
 
826
+ def brasilia
827
+ ActiveSupport::TimeZone["Brasilia"]
828
+ end
829
+
781
830
  def teardown
782
831
  User.delete_all
783
832
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groupdate
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-31 00:00:00.000000000 Z
11
+ date: 2014-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '5'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '5'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activerecord
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -125,6 +125,7 @@ files:
125
125
  - gemfiles/activerecord31.gemfile
126
126
  - gemfiles/activerecord32.gemfile
127
127
  - gemfiles/activerecord40.gemfile
128
+ - gemfiles/activerecord41.gemfile
128
129
  - groupdate.gemspec
129
130
  - lib/groupdate.rb
130
131
  - lib/groupdate/active_record.rb
@@ -167,4 +168,3 @@ test_files:
167
168
  - test/mysql_test.rb
168
169
  - test/postgresql_test.rb
169
170
  - test/test_helper.rb
170
- has_rdoc: