groupdate 0.1.1 → 0.1.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: db67453333111a42475d9ca697eb81181c6f291d
4
- data.tar.gz: bf1a4d74a65edc1aa710ac417800fe5d5d2c43bb
3
+ metadata.gz: ecaeb21da82e1d6200205f6c8f0de7675bd9a047
4
+ data.tar.gz: 0f5c4ca2715331c187ba9fd343f68b9a5a5143b4
5
5
  SHA512:
6
- metadata.gz: cc7436598003f15a4219dc3fae9964b99bba54cb610b2a553b84b5365e5434f2ccca3c3c0ed2fb344a00e64c03e61a334700a32d837c687460e24b334dabab1f
7
- data.tar.gz: dd4de87e97a685898f3d61f465e768cf6886df28497b5b9b1f7015b9c1ca9ab48870e3acc1dc8bc59279824f14ba252cd4db9f85bcae0cc2b0722a37487cab2b
6
+ metadata.gz: 5a21d57c6b249f6de6e27bf27ce6acd5886c3395d5e98edfadb3c5dcebe77a52ec0fb4640a4ca731b59a15a54b398fcca8ecc72e09bbb07386f0c579d0cc3101
7
+ data.tar.gz: dc3063e2885001609d9ee8cbccd38558b6e33cc2e7ae4af49b0b8b922fa68180c38b92e62b9416536fb399c7f20ad4d10686781f7f9452d6f86f548641b90b7d
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", github: "rails/rails"
6
+ # gem "activerecord", github: "rails/rails"
data/README.md CHANGED
@@ -7,11 +7,13 @@ The simplest way to group by:
7
7
  - month
8
8
  - day of the week
9
9
  - hour of the day
10
- - *and more* (complete list at bottom)
10
+ - and more (complete list at bottom)
11
11
 
12
- :tada: Time zones supported!!
12
+ :tada: Time zones supported!! **The best part**
13
13
 
14
- PostgreSQL and MySQL only at the moment - support for other datastores coming soon
14
+ Works with Rails 3.0+
15
+
16
+ Supports PostgreSQL and MySQL
15
17
 
16
18
  ## Usage
17
19
 
@@ -77,6 +79,12 @@ User.group_by_hour_of_day(:created_at, "Pacific Time (US & Canada)").count
77
79
  # }
78
80
  ```
79
81
 
82
+ You can order results with `group_field`.
83
+
84
+ ```ruby
85
+ User.group_by_day(:created_at).order("group_field desc").limit(20).count
86
+ ```
87
+
80
88
  Use it with anything you can use `group` with:
81
89
 
82
90
  ```ruby
@@ -89,7 +97,37 @@ Go nuts!
89
97
  Request.where(page: "/home").group_by_minute(:started_at).maximum(:request_time)
90
98
  ```
91
99
 
92
- ### Note
100
+ ## Installation
101
+
102
+ Add this line to your application's Gemfile:
103
+
104
+ ```ruby
105
+ gem 'groupdate'
106
+ ```
107
+
108
+ #### For MySQL
109
+
110
+ [Time zone support](http://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html) must be installed on the server.
111
+
112
+ ```
113
+ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
114
+ ```
115
+
116
+ ## Complete list
117
+
118
+ group_by_?
119
+
120
+ - second
121
+ - minute
122
+ - hour
123
+ - day
124
+ - week
125
+ - month
126
+ - year
127
+ - hour_of_day
128
+ - day_of_week
129
+
130
+ ## Note
93
131
 
94
132
  activerecord <= 4.0.0.beta1 and the pg gem returns String objects instead of Time objects.
95
133
  [This is fixed on activerecord master](https://github.com/rails/rails/commit/2cc09441c2de57b024b11ba666ba1e72c2b20cfe)
@@ -122,36 +160,6 @@ User.group_by_day_of_week(:created_at).count
122
160
 
123
161
  These are *not* a result of groupdate (and unfortunately cannot be fixed by groupdate)
124
162
 
125
- ## Installation
126
-
127
- Add this line to your application's Gemfile:
128
-
129
- ```ruby
130
- gem 'groupdate'
131
- ```
132
-
133
- ### MySQL only
134
-
135
- [Time zone support](http://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html) must be installed on the server.
136
-
137
- ```
138
- mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
139
- ```
140
-
141
- ## Complete list
142
-
143
- group_by_?
144
-
145
- - second
146
- - minute
147
- - hour
148
- - day
149
- - week
150
- - month
151
- - year
152
- - hour_of_day
153
- - day_of_week
154
-
155
163
  ## Contributing
156
164
 
157
165
  1. Fork it
data/lib/groupdate.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "groupdate/version"
2
+ require "active_record"
2
3
 
3
4
  module Groupdate
4
5
  extend ActiveSupport::Concern
@@ -81,12 +82,31 @@ module Groupdate
81
82
  else
82
83
  raise "Connection adapter not supported"
83
84
  end
84
- group(sanitize_sql_array(query))
85
+
86
+ group(Groupdate::OrderHack.new(sanitize_sql_array(query)))
85
87
  }
86
88
  end
87
89
  end
88
90
  end
89
91
 
92
+ class OrderHack < String; end
90
93
  end
91
94
 
92
95
  ActiveRecord::Base.send :include, Groupdate
96
+
97
+ # hack for **unfixed** rails issue
98
+ # https://github.com/rails/rails/issues/7121
99
+ module ActiveRecord
100
+ module Calculations
101
+
102
+ def column_alias_for_with_hack(*keys)
103
+ if keys.first.is_a?(Groupdate::OrderHack)
104
+ "group_field"
105
+ else
106
+ column_alias_for_without_hack(*keys)
107
+ end
108
+ end
109
+ alias_method_chain :column_alias_for, :hack
110
+
111
+ end
112
+ end
@@ -1,3 +1,3 @@
1
1
  module Groupdate
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,7 +1,7 @@
1
+ require "bundler/setup"
2
+ Bundler.require(:default)
1
3
  require "minitest/autorun"
2
4
  require "minitest/pride"
3
- require "active_record"
4
- require "groupdate"
5
5
  require "logger"
6
6
 
7
7
  # for debugging
@@ -31,102 +31,116 @@ describe Groupdate do
31
31
 
32
32
  it "works!" do
33
33
  [
34
- {name: "Andrew", score: 1, created_at: Time.parse("2013-04-01 00:00:10.200 UTC")},
35
- {name: "Jordan", score: 2, created_at: Time.parse("2013-04-01 00:00:10.200 UTC")},
36
- {name: "Nick", score: 3, created_at: Time.parse("2013-04-02 00:00:20.800 UTC")}
34
+ {name: "Andrew", score: 1, created_at: Time.parse("2013-04-01 00:00:00 UTC")},
35
+ {name: "Jordan", score: 2, created_at: Time.parse("2013-04-01 00:00:00 UTC")},
36
+ {name: "Nick", score: 3, created_at: Time.parse("2013-04-02 00:00:00 UTC")}
37
37
  ].each{|u| User.create!(u) }
38
38
 
39
39
  assert_equal(
40
- {"2013-04-01 00:00:00+00".to_time => 1, "2013-04-02 00:00:00+00".to_time => 1},
40
+ {
41
+ time_key("2013-04-01 00:00:00 UTC") => 1,
42
+ time_key("2013-04-02 00:00:00 UTC") => 1
43
+ },
41
44
  User.where("score > 1").group_by_day(:created_at).count
42
45
  )
43
46
  end
44
47
 
48
+ it "doesn't throw exception with order" do
49
+ User.group_by_day(:created_at).order("group_field desc").limit(20).count == {}
50
+ end
51
+
45
52
  it "group_by_second" do
46
- create_user "2013-04-01 00:00:01 UTC"
47
- assert_equal({"2013-04-01 00:00:01+00".to_time => 1}, User.group_by_second(:created_at).count)
53
+ assert_group :second, "2013-04-01 00:00:01 UTC", "2013-04-01 00:00:01 UTC"
48
54
  end
49
55
 
50
56
  it "group_by_minute" do
51
- create_user "2013-04-01 00:01:01 UTC"
52
- assert_equal({"2013-04-01 00:01:00+00".to_time => 1}, User.group_by_minute(:created_at).count)
57
+ assert_group :minute, "2013-04-01 00:01:01 UTC", "2013-04-01 00:01:00 UTC"
53
58
  end
54
59
 
55
60
  it "group_by_hour" do
56
- create_user "2013-04-01 01:01:01 UTC"
57
- assert_equal({"2013-04-01 01:00:00+00".to_time => 1}, User.group_by_hour(:created_at).count)
61
+ assert_group :hour, "2013-04-01 01:01:01 UTC", "2013-04-01 01:00:00 UTC"
58
62
  end
59
63
 
60
64
  it "group_by_day" do
61
- create_user "2013-04-01 01:01:01 UTC"
62
- assert_equal({"2013-04-01 00:00:00+00".to_time => 1}, User.group_by_day(:created_at).count)
65
+ assert_group :day, "2013-04-01 01:01:01 UTC", "2013-04-01 00:00:00 UTC"
63
66
  end
64
67
 
65
68
  it "group_by_day with time zone" do
66
- create_user "2013-04-01 01:01:01 UTC"
67
- assert_equal({"2013-03-31 07:00:00+00".to_time => 1}, User.group_by_day(:created_at, "Pacific Time (US & Canada)").count)
69
+ assert_group_tz :day, "2013-04-01 01:01:01 UTC", "2013-03-31 07:00:00 UTC"
68
70
  end
69
71
 
70
72
  it "group_by_week" do
71
- create_user "2013-03-17 01:01:01 UTC"
72
- assert_equal({"2013-03-17 00:00:00+00".to_time => 1}, User.group_by_week(:created_at).count)
73
+ assert_group :week, "2013-03-17 01:01:01 UTC", "2013-03-17 00:00:00 UTC"
73
74
  end
74
75
 
75
76
  it "group_by_week with time zone" do # day of DST
76
- create_user "2013-03-17 01:01:01 UTC"
77
- assert_equal({"2013-03-10 08:00:00+00".to_time => 1}, User.group_by_week(:created_at, "Pacific Time (US & Canada)").count)
77
+ assert_group_tz :week, "2013-03-17 01:01:01 UTC", "2013-03-10 08:00:00 UTC"
78
78
  end
79
79
 
80
80
  it "group_by_month" do
81
- create_user "2013-04-01 01:01:01 UTC"
82
- assert_equal({"2013-04-01 00:00:00+00".to_time(:utc) => 1}, User.group_by_month(:created_at).count)
81
+ assert_group :month, "2013-04-01 01:01:01 UTC", "2013-04-01 00:00:00 UTC"
83
82
  end
84
83
 
85
84
  it "group_by_month with time zone" do
86
- create_user "2013-04-01 01:01:01 UTC"
87
- assert_equal({"2013-03-01 08:00:00+00".to_time(:utc) => 1}, User.group_by_month(:created_at, "Pacific Time (US & Canada)").count)
85
+ assert_group_tz :month, "2013-04-01 01:01:01 UTC", "2013-03-01 08:00:00 UTC"
88
86
  end
89
87
 
90
88
  it "group_by_year" do
91
- create_user "2013-01-01 01:01:01 UTC"
92
- assert_equal({"2013-01-01 00:00:00+00".to_time(:utc) => 1}, User.group_by_year(:created_at).count)
89
+ assert_group :year, "2013-01-01 01:01:01 UTC", "2013-01-01 00:00:00 UTC"
93
90
  end
94
91
 
95
92
  it "group_by_year with time zone" do
96
- create_user "2013-01-01 01:01:01 UTC"
97
- assert_equal({"2012-01-01 08:00:00+00".to_time(:utc) => 1}, User.group_by_year(:created_at, "Pacific Time (US & Canada)").count)
93
+ assert_group_tz :year, "2013-01-01 01:01:01 UTC", "2012-01-01 08:00:00 UTC"
98
94
  end
99
95
 
100
96
  it "group_by_hour_of_day" do
101
- create_user "2013-01-01 11:00:00 UTC"
102
- expected = adapter == "mysql2" ? {11 => 1} : {11.0 => 1}
103
- assert_equal(expected, User.group_by_hour_of_day(:created_at).count)
97
+ assert_group_number :hour_of_day, "2013-01-01 11:00:00 UTC", 11
104
98
  end
105
99
 
106
100
  it "group_by_hour_of_day with time zone" do
107
- create_user "2013-01-01 11:00:00 UTC"
108
- expected = adapter == "mysql2" ? {3 => 1} : {3.0 => 1}
109
- assert_equal(expected, User.group_by_hour_of_day(:created_at, "Pacific Time (US & Canada)").count)
101
+ assert_group_number_tz :hour_of_day, "2013-01-01 11:00:00 UTC", 3
110
102
  end
111
103
 
112
104
  it "group_by_day_of_week" do
113
- create_user "2013-03-03 00:00:00 UTC"
114
- expected = adapter == "mysql2" ? {0 => 1} : {0.0 => 1}
115
- assert_equal(expected, User.group_by_day_of_week(:created_at).count)
105
+ assert_group_number :day_of_week, "2013-03-03 00:00:00 UTC", 0
116
106
  end
117
107
 
118
108
  it "group_by_day_of_week with time zone" do
119
- create_user "2013-03-03 00:00:00 UTC"
120
- expected = adapter == "mysql2" ? {6 => 1} : {6.0 => 1}
121
- assert_equal(expected, User.group_by_day_of_week(:created_at, "Pacific Time (US & Canada)").count)
109
+ assert_group_number_tz :day_of_week, "2013-03-03 00:00:00 UTC", 6
122
110
  end
111
+ end
112
+ end
123
113
 
124
- # helper methods
114
+ # helper methods
125
115
 
126
- def create_user(created_at)
127
- User.create!(name: "Andrew", score: 1, created_at: Time.parse(created_at))
128
- end
116
+ def assert_group(method, created_at, key, time_zone = nil)
117
+ create_user created_at
118
+ assert_equal({time_key(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count)
119
+ end
129
120
 
130
- end
121
+ def assert_group_tz(method, created_at, key)
122
+ assert_group method, created_at, key, "Pacific Time (US & Canada)"
123
+ end
124
+
125
+ def assert_group_number(method, created_at, key, time_zone = nil)
126
+ create_user created_at
127
+ assert_equal({number_key(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count)
131
128
  end
129
+
130
+ def assert_group_number_tz(method, created_at, key)
131
+ assert_group_number method, created_at, key, "Pacific Time (US & Canada)"
132
+ end
133
+
134
+ def time_key(key)
135
+ User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S+00") : Time.parse(key)
136
+ end
137
+
138
+ def number_key(key)
139
+ User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key
140
+ end
141
+
142
+ def create_user(created_at)
143
+ User.create!(name: "Andrew", score: 1, created_at: Time.parse(created_at))
144
+ end
145
+
132
146
  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: 0.1.1
4
+ version: 0.1.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: 2013-04-22 00:00:00.000000000 Z
11
+ date: 2013-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -136,3 +136,4 @@ specification_version: 4
136
136
  summary: The simplest way to group temporal data
137
137
  test_files:
138
138
  - test/groupdate_test.rb
139
+ has_rdoc: