groupdate 3.0.1 → 3.0.2

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: dd126e51d0c9b683eae721f9c2bf6a133409039b
4
- data.tar.gz: 0f5f4487b87fbe82f22eea5edebe9558cc883593
3
+ metadata.gz: 8a15c2188ffbf51536333cfad342a88325d5866c
4
+ data.tar.gz: 962f3ddeffa87168994308c6740ad126393db9e3
5
5
  SHA512:
6
- metadata.gz: 7b9c7878256b705354bb3ccef1c87946fa94815f34144cf1332775ec448f2db35bcd485399dc91bddef7089d56d92e94ed9be18acf4332fe415074b927a1d32e
7
- data.tar.gz: 8653c47e5f08f1001974aad183fbd7c182673a0da9436d8e1d79b288cac728621a472d3c9abc92ee5b5303ffc0d4a5d2090e96a69df3b33786096e92d845d35f
6
+ metadata.gz: caf2a8b62b7d05e4934f3035b1d7e16c739bef41d316ced765ceb48b0b5c36cb29bad12579ca90b3cfdacdb870d4ab721bc0d925aa5d1c791b15a393a067d4ba
7
+ data.tar.gz: dabd8ea049f07f730c219348dc247996838831221252eecb3a58a40fd650aa6a16b552a707db4aba1e6b522e984300888e4a7615fb8737bb0360ae2f2ff8e73b
@@ -10,7 +10,7 @@ gemfile:
10
10
  - test/gemfiles/activerecord41.gemfile
11
11
  - test/gemfiles/activerecord42.gemfile
12
12
  sudo: false
13
- script: bundle exec rake test
13
+ script: RUBYOPT=W0 bundle exec rake test
14
14
  before_install:
15
15
  - gem install bundler
16
16
  - mysql -e 'create database groupdate_test;'
@@ -1,3 +1,8 @@
1
+ ## 3.0.2
2
+
3
+ - Fixed `group_by_period` with associations
4
+ - Fixed `week_start` option for enumerables
5
+
1
6
  ## 3.0.1
2
7
 
3
8
  - Added support for Redshift
data/README.md CHANGED
@@ -11,8 +11,6 @@ The simplest way to group by:
11
11
 
12
12
  :cake: Get the entire series - **the other best part**
13
13
 
14
- Works with Rails 3.1+
15
-
16
14
  Supports PostgreSQL, MySQL, and Redshift, plus arrays and hashes
17
15
 
18
16
  [![Build Status](https://travis-ci.org/ankane/groupdate.svg?branch=master)](https://travis-ci.org/ankane/groupdate)
@@ -7,7 +7,7 @@ module Groupdate
7
7
  PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year]
8
8
  # backwards compatibility for anyone who happened to use it
9
9
  FIELDS = PERIODS
10
- METHODS = PERIODS.map { |v| :"group_by_#{v}" }
10
+ METHODS = PERIODS.map { |v| :"group_by_#{v}" } + [:group_by_period]
11
11
 
12
12
  mattr_accessor :week_start, :day_start, :time_zone, :dates
13
13
  self.week_start = :sun
@@ -11,13 +11,20 @@ module Enumerable
11
11
  end
12
12
  end
13
13
 
14
- def group_by_period(period, options = {}, &block)
15
- # to_sym is unsafe on user input, so convert to strings
16
- permitted_periods = ((options[:permit] || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s)
17
- if permitted_periods.include?(period.to_s)
18
- send("group_by_#{period}", options, &block)
14
+ def group_by_period(*args, &block)
15
+ if block || !respond_to?(:scoping)
16
+ period = args[0]
17
+ options = args[1] || {}
18
+
19
+ # to_sym is unsafe on user input, so convert to strings
20
+ permitted_periods = ((options[:permit] || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s)
21
+ if permitted_periods.include?(period.to_s)
22
+ send("group_by_#{period}", options, &block)
23
+ else
24
+ raise ArgumentError, "Unpermitted period"
25
+ end
19
26
  else
20
- raise ArgumentError, "Unpermitted period"
27
+ scoping { @klass.send(:group_by_period, *args, &block) }
21
28
  end
22
29
  end
23
30
  end
@@ -320,7 +320,7 @@ module Groupdate
320
320
  when :hour_of_day
321
321
  time.hour
322
322
  when :day_of_week
323
- (7 - week_start + ((time.wday - 1) % 7) % 7)
323
+ time.wday
324
324
  when :day_of_month
325
325
  time.day
326
326
  when :month_of_year
@@ -1,3 +1,3 @@
1
1
  module Groupdate
2
- VERSION = "3.0.1"
2
+ VERSION = "3.0.2"
3
3
  end
@@ -293,6 +293,15 @@ module TestDatabase
293
293
  assert_equal expected, user.posts.group_by_day(:created_at).count
294
294
  end
295
295
 
296
+ def test_associations_period
297
+ user = create_user("2014-03-01")
298
+ user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
299
+ expected = {
300
+ Date.parse("2014-04-01") => 1
301
+ }
302
+ assert_equal expected, user.posts.group_by_period(:day, :created_at).count
303
+ end
304
+
296
305
  # activerecord default_timezone option
297
306
 
298
307
  def test_default_timezone_local
@@ -378,7 +387,7 @@ module TestGroupdate
378
387
  # second
379
388
 
380
389
  def test_second_end_of_second
381
- if enumerable_test? || (ActiveRecord::Base.connection.adapter_name == "Mysql2" && ActiveRecord::VERSION::STRING.starts_with?("4.2."))
390
+ if enumerable_test? || ActiveRecord::Base.connection.adapter_name == "Mysql2"
382
391
  skip # no millisecond precision
383
392
  else
384
393
  assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
@@ -962,17 +971,22 @@ module TestGroupdate
962
971
 
963
972
  def test_format_hour_of_day_day_start
964
973
  create_user "2014-03-01"
965
- assert_format :hour_of_day, "2 am", "%-l %P", day_start: 2
974
+ assert_format :hour_of_day, "12 am", "%-l %P", day_start: 2
966
975
  end
967
976
 
968
977
  def test_format_day_of_week
969
978
  create_user "2014-03-01"
970
- assert_format :day_of_week, "Sun", "%a"
979
+ assert_format :day_of_week, "Sat", "%a"
980
+ end
981
+
982
+ def test_format_day_of_week_day_start
983
+ create_user "2014-03-01"
984
+ assert_format :day_of_week, "Fri", "%a", day_start: 2
971
985
  end
972
986
 
973
987
  def test_format_day_of_week_week_start
974
988
  create_user "2014-03-01"
975
- assert_format :day_of_week, "Sun", "%a", week_start: :sat
989
+ assert_format :day_of_week, "Sat", "%a", week_start: :mon
976
990
  end
977
991
 
978
992
  def test_format_day_of_month
@@ -1016,7 +1030,7 @@ module TestGroupdate
1016
1030
  # helpers
1017
1031
 
1018
1032
  def assert_format(method, expected, format, options = {})
1019
- assert_equal expected, call_method(method, :created_at, options.merge(format: format, series: true)).keys.first
1033
+ assert_equal({expected => 1}, call_method(method, :created_at, options.merge(format: format, series: false)))
1020
1034
  end
1021
1035
 
1022
1036
  def assert_result_time(method, expected, time_str, time_zone = false, options = {})
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: 3.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-13 00:00:00.000000000 Z
11
+ date: 2016-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport