pluck_all 2.0.4 → 2.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +101 -0
- data/.rubocop.yml +34 -35
- data/CHANGELOG.md +82 -60
- data/README.md +67 -10
- data/Rakefile +22 -22
- data/bin/console +14 -14
- data/gemfiles/active_record_32.gemfile +16 -13
- data/gemfiles/active_record_42.gemfile +18 -13
- data/gemfiles/active_record_50.gemfile +18 -13
- data/gemfiles/active_record_51.gemfile +18 -13
- data/gemfiles/active_record_52.gemfile +18 -13
- data/gemfiles/active_record_60.gemfile +18 -0
- data/gemfiles/active_record_61.gemfile +18 -0
- data/gemfiles/mongoid_54.gemfile +11 -11
- data/gemfiles/mongoid_64.gemfile +11 -11
- data/gemfiles/mongoid_70.gemfile +11 -11
- data/lib/pluck_all.rb +3 -3
- data/lib/pluck_all/hooks.rb +10 -10
- data/lib/pluck_all/models/active_record_extension.rb +23 -15
- data/lib/pluck_all/models/mongoid_extension.rb +70 -70
- data/lib/pluck_all/models/patches/deserialize.rb +13 -13
- data/lib/pluck_all/version.rb +1 -1
- data/pluck_all.gemspec +43 -35
- metadata +26 -7
- data/.travis.yml +0 -52
- data/lib/pluck_all/models/patches/attribute_types.rb +0 -8
data/README.md
CHANGED
@@ -1,20 +1,22 @@
|
|
1
1
|
# PluckAll
|
2
2
|
|
3
3
|
[![Gem Version](https://img.shields.io/gem/v/pluck_all.svg?style=flat)](http://rubygems.org/gems/pluck_all)
|
4
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/khiav223577/pluck_all/workflows/Ruby/badge.svg)](https://github.com/khiav223577/pluck_all/actions)
|
5
5
|
[![RubyGems](http://img.shields.io/gem/dt/pluck_all.svg?style=flat)](http://rubygems.org/gems/pluck_all)
|
6
6
|
[![Code Climate](https://codeclimate.com/github/khiav223577/pluck_all/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/pluck_all)
|
7
7
|
[![Test Coverage](https://codeclimate.com/github/khiav223577/pluck_all/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/pluck_all/coverage)
|
8
8
|
|
9
|
-
Pluck multiple columns/attributes in Rails 3, 4, 5, and can return data as hash instead of only array. Also supports Mongoid
|
9
|
+
Pluck multiple columns/attributes in Rails 3, 4, 5, 6, and can return data as hash instead of only array. Also supports `Mongoid`.
|
10
10
|
|
11
|
-
This Gem stands on the shoulders of this article: [Plucking Multiple Columns in Rails 3](
|
11
|
+
This Gem stands on the shoulders of this article: [Plucking Multiple Columns in Rails 3](https://meltingice.dev/2013/06/11/pluck-multiple-columns-rails/).
|
12
12
|
And modified to support not only Rail 3.
|
13
13
|
|
14
14
|
If you have a Rails 3 project, and want to pluck not only one column,
|
15
|
-
feel free to use this gem and no need to worry about upgrading to Rails 4, 5 in the future will break this.
|
16
|
-
|
15
|
+
feel free to use this gem and no need to worry about upgrading to Rails 4, 5, 6 in the future will break this.
|
17
16
|
|
17
|
+
## Supports
|
18
|
+
- Ruby 2.2 ~ 2.7
|
19
|
+
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1
|
18
20
|
|
19
21
|
## Installation
|
20
22
|
|
@@ -36,7 +38,7 @@ Or install it yourself as:
|
|
36
38
|
|
37
39
|
### pluck to array
|
38
40
|
|
39
|
-
Behaves the same as
|
41
|
+
Behaves the same as `#pluck` method, but you can use it to pluck multiple columns in Rails 3
|
40
42
|
|
41
43
|
```rb
|
42
44
|
User.where('id < 3').pluck_array(:id, :account)
|
@@ -45,12 +47,15 @@ User.where('id < 3').pluck_array(:id, :account)
|
|
45
47
|
|
46
48
|
### pluck to hash
|
47
49
|
|
48
|
-
Similar to `
|
50
|
+
Similar to `#pluck` method, but return array of hashes instead.
|
49
51
|
|
50
52
|
```rb
|
51
53
|
User.where('id < 3').pluck_all(:id, :account)
|
52
54
|
# => [{"id"=>1, "account"=>"account1"}, {"id"=>2, "account"=>"account2"}]
|
53
55
|
|
56
|
+
User.where('id < 3').pluck_all(:id, 'account AS name')
|
57
|
+
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
58
|
+
|
54
59
|
User.where('id < 3').pluck_all('id, account AS name')
|
55
60
|
# => [{"id"=>1, "name"=>"account1"}, {"id"=>2, "name"=>"account2"}]
|
56
61
|
```
|
@@ -85,20 +90,72 @@ select + map 10.530000 0.660000 11.190000 ( 12.550974)
|
|
85
90
|
select + as_json 49.040000 1.120000 50.160000 ( 55.417534)
|
86
91
|
pluck_all 3.310000 0.100000 3.410000 ( 3.527775)
|
87
92
|
```
|
93
|
+
Test by `benchmark-ips` and `limit 100` in each iteration:
|
94
|
+
```
|
95
|
+
Warming up --------------------------------------
|
96
|
+
map 1.000 i/100ms
|
97
|
+
select + map 28.000 i/100ms
|
98
|
+
select + as_json 7.000 i/100ms
|
99
|
+
pluck_all 54.000 i/100ms
|
100
|
+
Calculating -------------------------------------
|
101
|
+
map 14.230 (± 0.0%) i/s - 72.000 in 5.065349s
|
102
|
+
select + map 281.638 (± 4.6%) i/s - 1.428k in 5.081216s
|
103
|
+
select + as_json 73.241 (± 4.1%) i/s - 371.000 in 5.076235s
|
104
|
+
pluck_all 539.057 (± 6.7%) i/s - 2.700k in 5.034858s
|
105
|
+
|
106
|
+
Comparison:
|
107
|
+
pluck_all: 539.1 i/s
|
108
|
+
select + map: 281.6 i/s - 1.91x slower
|
109
|
+
select + as_json: 73.2 i/s - 7.36x slower
|
110
|
+
map: 14.2 i/s - 37.88x slower
|
111
|
+
```
|
88
112
|
[test script](https://github.com/khiav223577/pluck_all/issues/18)
|
89
113
|
|
90
114
|
### Compare with [pluck_to_hash](https://github.com/girishso/pluck_to_hash) gem
|
91
115
|
|
92
|
-
`pluck_all` has better performace since it uses raw `hash` data from `ActiveRecord::Base.connection.select_all`, while `pluck_to_hash` uses `pluck` method, which calls `ActiveRecord::Base.connection.select_all` and transfers the raw `hash` data to `array` format, and then transfer the data to `hash` format again. The following benchmark
|
116
|
+
`pluck_all` has better performace since it uses raw `hash` data from `ActiveRecord::Base.connection.select_all`, while `pluck_to_hash` uses `pluck` method, which calls `ActiveRecord::Base.connection.select_all` and transfers the raw `hash` data to `array` format, and then transfer the data to `hash` format again. The following benchmark shows the performance difference:
|
93
117
|
|
94
118
|
```rb
|
95
119
|
user system total real
|
96
120
|
pluck_to_hash 2.960000 0.130000 3.090000 ( 3.421640)
|
97
121
|
pluck_all 2.160000 0.120000 2.280000 ( 2.605118)
|
98
122
|
```
|
99
|
-
|
123
|
+
Tested by `benchmark-ips` and `limit 1000` in each iteration:
|
124
|
+
```
|
125
|
+
Warming up --------------------------------------
|
126
|
+
pluck_to_hash 7.000 i/100ms
|
127
|
+
pluck_all 9.000 i/100ms
|
128
|
+
Calculating -------------------------------------
|
129
|
+
pluck_to_hash 84.526 (± 4.7%) i/s - 427.000 in 5.065792s
|
130
|
+
pluck_all 95.133 (± 4.2%) i/s - 477.000 in 5.021555s
|
131
|
+
|
132
|
+
Comparison:
|
133
|
+
pluck_all: 95.1 i/s
|
134
|
+
pluck_to_hash: 84.5 i/s - 1.13x slower
|
135
|
+
```
|
136
|
+
See the [test script](https://github.com/khiav223577/pluck_all/issues/18#issuecomment-325407080) for more details.
|
100
137
|
|
101
138
|
## Other Support
|
139
|
+
|
140
|
+
### Support globalize gem
|
141
|
+
|
142
|
+
```rb
|
143
|
+
class Post < ActiveRecord::Base
|
144
|
+
translates :title
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
```rb
|
149
|
+
I18n.locale = :en
|
150
|
+
Post.pluck_all(:title)
|
151
|
+
# => [{ 'title' => 'english' }, { 'title' => 'english' }, ...]
|
152
|
+
|
153
|
+
I18n.locale = :'zh-TW'
|
154
|
+
Post.pluck_all(:title)
|
155
|
+
# => [{ 'title' => '中文' }, { 'title' => '中文' }, ...]
|
156
|
+
```
|
157
|
+
|
158
|
+
|
102
159
|
### Support Pluck Carrierwave Uploader (if you use carrierwave)
|
103
160
|
```rb
|
104
161
|
User.where(xxx).pluck_all(:profile_pic).map{|s| s['profile_pic'] }
|
@@ -110,7 +167,7 @@ User.where(xxx).map(&:profile_pic)
|
|
110
167
|
If the uploader use something like: `model.id`, `model.name`
|
111
168
|
You may have to send these columns manually:
|
112
169
|
```rb
|
113
|
-
User.where(xxx).cast_need_columns(%i
|
170
|
+
User.where(xxx).cast_need_columns(%i[id name]).pluck_all(:id, :name, :profile_pic).map{|s| s['profile_pic'] }
|
114
171
|
```
|
115
172
|
|
116
173
|
## Development
|
data/Rakefile
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
require 'bundler/gem_tasks'
|
2
|
-
require 'rake/testtask'
|
3
|
-
|
4
|
-
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs << 'test'
|
6
|
-
t.libs << 'lib'
|
7
|
-
t.test_files = FileList['test/**/*_test.rb']
|
8
|
-
end
|
9
|
-
|
10
|
-
Rake::TestTask.new(:test_active_record) do |t|
|
11
|
-
t.libs << 'test'
|
12
|
-
t.libs << 'lib'
|
13
|
-
t.test_files = FileList['test/active_record/**/*_test.rb']
|
14
|
-
end
|
15
|
-
|
16
|
-
Rake::TestTask.new(:test_mongoid) do |t|
|
17
|
-
t.libs << 'test'
|
18
|
-
t.libs << 'lib'
|
19
|
-
t.test_files = FileList['test/mongoid/**/*_test.rb']
|
20
|
-
end
|
21
|
-
|
22
|
-
task default: :test
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.test_files = FileList['test/**/*_test.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
Rake::TestTask.new(:test_active_record) do |t|
|
11
|
+
t.libs << 'test'
|
12
|
+
t.libs << 'lib'
|
13
|
+
t.test_files = FileList['test/active_record/**/*_test.rb']
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::TestTask.new(:test_mongoid) do |t|
|
17
|
+
t.libs << 'test'
|
18
|
+
t.libs << 'lib'
|
19
|
+
t.test_files = FileList['test/mongoid/**/*_test.rb']
|
20
|
+
end
|
21
|
+
|
22
|
+
task default: :test
|
data/bin/console
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'pluck_all'
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require 'irb'
|
14
|
-
IRB.start
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pluck_all'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
@@ -1,13 +1,16 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 3.2.0'
|
7
|
-
gem '
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 3.2.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'simplecov', '< 0.18'
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec path: '../'
|
@@ -1,13 +1,18 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 4.2.0'
|
7
|
-
gem '
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 4.2.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
gem 'globalize'
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'simplecov', '< 0.18'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec path: '../'
|
@@ -1,13 +1,18 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 5.0.0'
|
7
|
-
gem '
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 5.0.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
gem 'globalize'
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'simplecov', '< 0.18'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec path: '../'
|
@@ -1,13 +1,18 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 5.1.0'
|
7
|
-
gem '
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 5.1.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
gem 'globalize'
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'simplecov', '< 0.18'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec path: '../'
|
@@ -1,13 +1,18 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'sqlite3', '~> 1.3.0'
|
6
|
-
gem 'activerecord', '~> 5.2.0'
|
7
|
-
gem '
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.3.0'
|
6
|
+
gem 'activerecord', '~> 5.2.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
gem 'globalize'
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'simplecov', '< 0.18'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec path: '../'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.4.1'
|
6
|
+
gem 'activerecord', '~> 6.0.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
gem 'globalize'
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'simplecov', '< 0.18'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec path: '../'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'sqlite3', '~> 1.4.1'
|
6
|
+
gem 'activerecord', '~> 6.1.0'
|
7
|
+
gem 'rails_compatibility', '~> 0.0.7'
|
8
|
+
|
9
|
+
gem 'carrierwave', '~> 0.11.0'
|
10
|
+
gem 'mimemagic', '< 0.4.3' # Used by carrierwave gem
|
11
|
+
|
12
|
+
gem 'globalize'
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'simplecov', '< 0.18'
|
16
|
+
end
|
17
|
+
|
18
|
+
gemspec path: '../'
|
data/gemfiles/mongoid_54.gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'mongoid', '~> 5.4.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'simplecov'
|
9
|
-
end
|
10
|
-
|
11
|
-
gemspec path: '../'
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'mongoid', '~> 5.4.0'
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'simplecov', '< 0.18'
|
9
|
+
end
|
10
|
+
|
11
|
+
gemspec path: '../'
|
data/gemfiles/mongoid_64.gemfile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
-
|
5
|
-
gem 'mongoid', '~> 6.4.0'
|
6
|
-
|
7
|
-
group :test do
|
8
|
-
gem 'simplecov'
|
9
|
-
end
|
10
|
-
|
11
|
-
gemspec path: '../'
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in pluck_all.gemspec
|
4
|
+
|
5
|
+
gem 'mongoid', '~> 6.4.0'
|
6
|
+
|
7
|
+
group :test do
|
8
|
+
gem 'simplecov', '< 0.18'
|
9
|
+
end
|
10
|
+
|
11
|
+
gemspec path: '../'
|