groupdate 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +10 -0
- data/Gemfile +5 -0
- data/README.md +22 -4
- data/Rakefile +8 -0
- data/groupdate.gemspec +8 -2
- data/lib/groupdate.rb +12 -5
- data/lib/groupdate/version.rb +1 -1
- data/test/groupdate_test.rb +28 -11
- metadata +31 -16
- checksums.yaml +0 -7
data/.travis.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
- jruby
|
6
|
+
script: bundle exec rake test
|
7
|
+
before_script:
|
8
|
+
- mysql -e 'create database groupdate_test;'
|
9
|
+
- mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
10
|
+
- psql -c 'create database groupdate_test;' -U postgres
|
data/Gemfile
CHANGED
@@ -4,3 +4,8 @@ source 'https://rubygems.org'
|
|
4
4
|
gemspec
|
5
5
|
|
6
6
|
# gem "activerecord", github: "rails/rails"
|
7
|
+
|
8
|
+
platform :jruby do
|
9
|
+
gem "activerecord-jdbcpostgresql-adapter", github: "jruby/activerecord-jdbc-adapter"
|
10
|
+
gem "activerecord-jdbcmysql-adapter", github: "jruby/activerecord-jdbc-adapter"
|
11
|
+
end
|
data/README.md
CHANGED
@@ -15,6 +15,8 @@ Works with Rails 3.0+
|
|
15
15
|
|
16
16
|
Supports PostgreSQL and MySQL
|
17
17
|
|
18
|
+
[![Build Status](https://travis-ci.org/ankane/groupdate.png)](https://travis-ci.org/ankane/groupdate)
|
19
|
+
|
18
20
|
## Usage
|
19
21
|
|
20
22
|
```ruby
|
@@ -79,13 +81,17 @@ User.group_by_hour_of_day(:created_at, "Pacific Time (US & Canada)").count
|
|
79
81
|
# }
|
80
82
|
```
|
81
83
|
|
82
|
-
You can order results with
|
84
|
+
You can order results with:
|
83
85
|
|
84
86
|
```ruby
|
85
|
-
User.group_by_day(:created_at).order("
|
87
|
+
User.group_by_day(:created_at).order("day asc").count
|
88
|
+
|
89
|
+
User.group_by_week(:created_at).order("week desc").count
|
90
|
+
|
91
|
+
User.group_by_hour_of_day(:created_at).order("hour_of_day asc").count
|
86
92
|
```
|
87
93
|
|
88
|
-
Use it with
|
94
|
+
Use it with anywhere you can use `group`.
|
89
95
|
|
90
96
|
```ruby
|
91
97
|
Task.completed.group_by_hour(:completed_at).average(:priority)
|
@@ -109,10 +115,22 @@ gem 'groupdate'
|
|
109
115
|
|
110
116
|
[Time zone support](http://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html) must be installed on the server.
|
111
117
|
|
112
|
-
```
|
118
|
+
```sh
|
113
119
|
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
|
114
120
|
```
|
115
121
|
|
122
|
+
#### For JRuby
|
123
|
+
|
124
|
+
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).
|
125
|
+
|
126
|
+
```sh
|
127
|
+
# postgresql
|
128
|
+
gem "activerecord-jdbcpostgresql-adapter", github: "jruby/activerecord-jdbc-adapter"
|
129
|
+
|
130
|
+
# mysql
|
131
|
+
gem "activerecord-jdbcmysql-adapter", github: "jruby/activerecord-jdbc-adapter"
|
132
|
+
```
|
133
|
+
|
116
134
|
## Complete list
|
117
135
|
|
118
136
|
group_by_?
|
data/Rakefile
CHANGED
data/groupdate.gemspec
CHANGED
@@ -23,6 +23,12 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
spec.add_development_dependency "minitest"
|
26
|
-
|
27
|
-
|
26
|
+
|
27
|
+
if RUBY_PLATFORM == "java"
|
28
|
+
spec.add_development_dependency "activerecord-jdbcpostgresql-adapter"
|
29
|
+
spec.add_development_dependency "activerecord-jdbcmysql-adapter"
|
30
|
+
else
|
31
|
+
spec.add_development_dependency "pg"
|
32
|
+
spec.add_development_dependency "mysql2"
|
33
|
+
end
|
28
34
|
end
|
data/lib/groupdate.rb
CHANGED
@@ -40,7 +40,7 @@ module Groupdate
|
|
40
40
|
end
|
41
41
|
query =
|
42
42
|
case connection.adapter_name
|
43
|
-
when "Mysql2"
|
43
|
+
when "MySQL", "Mysql2"
|
44
44
|
case field
|
45
45
|
when "day_of_week" # Sunday = 0, Monday = 1, etc
|
46
46
|
# use CONCAT for consistent return type (String)
|
@@ -80,16 +80,23 @@ module Groupdate
|
|
80
80
|
["DATE_TRUNC('#{field}', #{column}::timestamptz AT TIME ZONE ?) AT TIME ZONE ?", time_zone, time_zone]
|
81
81
|
end
|
82
82
|
else
|
83
|
-
raise "Connection adapter not supported"
|
83
|
+
raise "Connection adapter not supported: #{connection.adapter_name}"
|
84
84
|
end
|
85
85
|
|
86
|
-
group(Groupdate::OrderHack.new(sanitize_sql_array(query)))
|
86
|
+
group(Groupdate::OrderHack.new(sanitize_sql_array(query), field))
|
87
87
|
}
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
class OrderHack < String
|
92
|
+
class OrderHack < String
|
93
|
+
attr_reader :field
|
94
|
+
|
95
|
+
def initialize(str, field)
|
96
|
+
super(str)
|
97
|
+
@field = field
|
98
|
+
end
|
99
|
+
end
|
93
100
|
end
|
94
101
|
|
95
102
|
ActiveRecord::Base.send :include, Groupdate
|
@@ -101,7 +108,7 @@ module ActiveRecord
|
|
101
108
|
|
102
109
|
def column_alias_for_with_hack(*keys)
|
103
110
|
if keys.first.is_a?(Groupdate::OrderHack)
|
104
|
-
|
111
|
+
keys.first.field
|
105
112
|
else
|
106
113
|
column_alias_for_without_hack(*keys)
|
107
114
|
end
|
data/lib/groupdate/version.rb
CHANGED
data/test/groupdate_test.rb
CHANGED
@@ -14,18 +14,25 @@ ActiveRecord::Base.time_zone_aware_attributes = true
|
|
14
14
|
class User < ActiveRecord::Base
|
15
15
|
end
|
16
16
|
|
17
|
+
# migrations
|
18
|
+
%w(postgresql mysql2).each do |adapter|
|
19
|
+
ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil
|
20
|
+
|
21
|
+
unless ActiveRecord::Base.connection.table_exists? "users"
|
22
|
+
ActiveRecord::Migration.create_table :users do |t|
|
23
|
+
t.string :name
|
24
|
+
t.integer :score
|
25
|
+
t.timestamps
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
17
30
|
describe Groupdate do
|
18
31
|
%w(postgresql mysql2).each do |adapter|
|
19
32
|
describe adapter do
|
20
|
-
before do
|
21
|
-
ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate"
|
22
|
-
|
23
|
-
# ActiveRecord::Migration.create_table :users do |t|
|
24
|
-
# t.string :name
|
25
|
-
# t.integer :score
|
26
|
-
# t.timestamps
|
27
|
-
# end
|
28
33
|
|
34
|
+
before do
|
35
|
+
User.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil
|
29
36
|
User.delete_all
|
30
37
|
end
|
31
38
|
|
@@ -46,7 +53,9 @@ describe Groupdate do
|
|
46
53
|
end
|
47
54
|
|
48
55
|
it "doesn't throw exception with order" do
|
49
|
-
User.group_by_day(:created_at).order("
|
56
|
+
assert_equal({}, User.group_by_day(:created_at).order("day desc").limit(20).count)
|
57
|
+
assert_equal({}, User.group_by_week(:created_at).order("week asc").count)
|
58
|
+
assert_equal({}, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count)
|
50
59
|
end
|
51
60
|
|
52
61
|
it "group_by_second" do
|
@@ -132,11 +141,19 @@ describe Groupdate do
|
|
132
141
|
end
|
133
142
|
|
134
143
|
def time_key(key)
|
135
|
-
|
144
|
+
if RUBY_PLATFORM == "java"
|
145
|
+
User.connection.adapter_name == "PostgreSQL" ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S%z")[0..-3] : Time.parse(key).strftime("%Y-%m-%d %H:%M:%S").gsub(/ 00\:00\:00\z/, "")
|
146
|
+
else
|
147
|
+
User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S%z")[0..-3] : Time.parse(key)
|
148
|
+
end
|
136
149
|
end
|
137
150
|
|
138
151
|
def number_key(key)
|
139
|
-
|
152
|
+
if RUBY_PLATFORM == "java"
|
153
|
+
User.connection.adapter_name == "PostgreSQL" ? key.to_f : key
|
154
|
+
else
|
155
|
+
User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key
|
156
|
+
end
|
140
157
|
end
|
141
158
|
|
142
159
|
def create_user(created_at)
|
metadata
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groupdate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Andrew Kane
|
@@ -13,20 +14,23 @@ dependencies:
|
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: activerecord
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
|
-
- - '>='
|
19
|
+
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: 3.0.0
|
20
22
|
type: :runtime
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
|
-
- - '>='
|
27
|
+
- - ! '>='
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: 3.0.0
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: bundler
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
35
|
- - ~>
|
32
36
|
- !ruby/object:Gem::Version
|
@@ -34,6 +38,7 @@ dependencies:
|
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
43
|
- - ~>
|
39
44
|
- !ruby/object:Gem::Version
|
@@ -41,57 +46,65 @@ dependencies:
|
|
41
46
|
- !ruby/object:Gem::Dependency
|
42
47
|
name: rake
|
43
48
|
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
44
50
|
requirements:
|
45
|
-
- - '>='
|
51
|
+
- - ! '>='
|
46
52
|
- !ruby/object:Gem::Version
|
47
53
|
version: '0'
|
48
54
|
type: :development
|
49
55
|
prerelease: false
|
50
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
51
58
|
requirements:
|
52
|
-
- - '>='
|
59
|
+
- - ! '>='
|
53
60
|
- !ruby/object:Gem::Version
|
54
61
|
version: '0'
|
55
62
|
- !ruby/object:Gem::Dependency
|
56
63
|
name: minitest
|
57
64
|
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
58
66
|
requirements:
|
59
|
-
- - '>='
|
67
|
+
- - ! '>='
|
60
68
|
- !ruby/object:Gem::Version
|
61
69
|
version: '0'
|
62
70
|
type: :development
|
63
71
|
prerelease: false
|
64
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
65
74
|
requirements:
|
66
|
-
- - '>='
|
75
|
+
- - ! '>='
|
67
76
|
- !ruby/object:Gem::Version
|
68
77
|
version: '0'
|
69
78
|
- !ruby/object:Gem::Dependency
|
70
79
|
name: pg
|
71
80
|
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
72
82
|
requirements:
|
73
|
-
- - '>='
|
83
|
+
- - ! '>='
|
74
84
|
- !ruby/object:Gem::Version
|
75
85
|
version: '0'
|
76
86
|
type: :development
|
77
87
|
prerelease: false
|
78
88
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
79
90
|
requirements:
|
80
|
-
- - '>='
|
91
|
+
- - ! '>='
|
81
92
|
- !ruby/object:Gem::Version
|
82
93
|
version: '0'
|
83
94
|
- !ruby/object:Gem::Dependency
|
84
95
|
name: mysql2
|
85
96
|
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
86
98
|
requirements:
|
87
|
-
- - '>='
|
99
|
+
- - ! '>='
|
88
100
|
- !ruby/object:Gem::Version
|
89
101
|
version: '0'
|
90
102
|
type: :development
|
91
103
|
prerelease: false
|
92
104
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
93
106
|
requirements:
|
94
|
-
- - '>='
|
107
|
+
- - ! '>='
|
95
108
|
- !ruby/object:Gem::Version
|
96
109
|
version: '0'
|
97
110
|
description: The simplest way to group temporal data
|
@@ -102,6 +115,7 @@ extensions: []
|
|
102
115
|
extra_rdoc_files: []
|
103
116
|
files:
|
104
117
|
- .gitignore
|
118
|
+
- .travis.yml
|
105
119
|
- Gemfile
|
106
120
|
- LICENSE.txt
|
107
121
|
- README.md
|
@@ -113,26 +127,27 @@ files:
|
|
113
127
|
homepage: ''
|
114
128
|
licenses:
|
115
129
|
- MIT
|
116
|
-
metadata: {}
|
117
130
|
post_install_message:
|
118
131
|
rdoc_options: []
|
119
132
|
require_paths:
|
120
133
|
- lib
|
121
134
|
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
122
136
|
requirements:
|
123
|
-
- - '>='
|
137
|
+
- - ! '>='
|
124
138
|
- !ruby/object:Gem::Version
|
125
139
|
version: '0'
|
126
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
none: false
|
127
142
|
requirements:
|
128
|
-
- - '>='
|
143
|
+
- - ! '>='
|
129
144
|
- !ruby/object:Gem::Version
|
130
145
|
version: '0'
|
131
146
|
requirements: []
|
132
147
|
rubyforge_project:
|
133
|
-
rubygems_version:
|
148
|
+
rubygems_version: 1.8.23
|
134
149
|
signing_key:
|
135
|
-
specification_version:
|
150
|
+
specification_version: 3
|
136
151
|
summary: The simplest way to group temporal data
|
137
152
|
test_files:
|
138
153
|
- test/groupdate_test.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: ecaeb21da82e1d6200205f6c8f0de7675bd9a047
|
4
|
-
data.tar.gz: 0f5c4ca2715331c187ba9fd343f68b9a5a5143b4
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 5a21d57c6b249f6de6e27bf27ce6acd5886c3395d5e98edfadb3c5dcebe77a52ec0fb4640a4ca731b59a15a54b398fcca8ecc72e09bbb07386f0c579d0cc3101
|
7
|
-
data.tar.gz: dc3063e2885001609d9ee8cbccd38558b6e33cc2e7ae4af49b0b8b922fa68180c38b92e62b9416536fb399c7f20ad4d10686781f7f9452d6f86f548641b90b7d
|