pluck_all 2.0.1 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +1227 -0
- data/.travis.yml +58 -13
- data/CHANGELOG.md +28 -1
- data/README.md +47 -10
- data/Rakefile +17 -5
- data/bin/console +3 -3
- data/bin/setup +1 -1
- data/gemfiles/active_record_32.gemfile +13 -0
- data/gemfiles/active_record_42.gemfile +13 -0
- data/gemfiles/active_record_50.gemfile +13 -0
- data/gemfiles/active_record_51.gemfile +13 -0
- data/gemfiles/active_record_52.gemfile +13 -0
- data/gemfiles/active_record_60.gemfile +13 -0
- data/gemfiles/active_record_61.gemfile +13 -0
- data/gemfiles/{no_mongoid.gemfile → mongoid_54.gemfile} +3 -5
- data/gemfiles/mongoid_64.gemfile +11 -0
- data/gemfiles/mongoid_70.gemfile +11 -0
- data/lib/pluck_all.rb +1 -130
- data/lib/pluck_all/hooks.rb +21 -0
- data/lib/pluck_all/models/active_record_extension.rb +131 -0
- data/lib/pluck_all/{mongoid_pluck_all.rb → models/mongoid_extension.rb} +24 -9
- data/lib/pluck_all/models/patches/deserialize.rb +13 -0
- data/lib/pluck_all/version.rb +1 -1
- data/pluck_all.gemspec +26 -20
- metadata +56 -38
- data/gemfiles/3.2.gemfile +0 -14
- data/gemfiles/4.2.gemfile +0 -14
- data/gemfiles/5.0.gemfile +0 -14
- data/gemfiles/5.1.gemfile +0 -14
- data/gemfiles/5.2.gemfile +0 -14
data/.travis.yml
CHANGED
@@ -1,28 +1,73 @@
|
|
1
1
|
sudo: false
|
2
|
-
env:
|
3
|
-
global:
|
4
|
-
- CC_TEST_REPORTER_ID=db72eba1ff8fb1329dae5fb9b9dcd234243899d7a464ceb374e14a05ead27b7c
|
5
2
|
language: ruby
|
6
3
|
rvm:
|
7
4
|
- 2.2
|
8
|
-
- 2.
|
5
|
+
- 2.6
|
6
|
+
- 2.7
|
9
7
|
services:
|
10
8
|
- mongodb
|
9
|
+
env:
|
10
|
+
global:
|
11
|
+
- CC_TEST_REPORTER_ID=db72eba1ff8fb1329dae5fb9b9dcd234243899d7a464ceb374e14a05ead27b7c
|
12
|
+
matrix:
|
13
|
+
- ORM_TYPE=ACTIVE_RECORD
|
14
|
+
- ORM_TYPE=MONGOID
|
11
15
|
gemfile:
|
12
|
-
- gemfiles/
|
13
|
-
- gemfiles/
|
14
|
-
- gemfiles/
|
15
|
-
- gemfiles/
|
16
|
-
- gemfiles/
|
17
|
-
- gemfiles/
|
16
|
+
- gemfiles/active_record_32.gemfile
|
17
|
+
- gemfiles/active_record_42.gemfile
|
18
|
+
- gemfiles/active_record_50.gemfile
|
19
|
+
- gemfiles/active_record_51.gemfile
|
20
|
+
- gemfiles/active_record_52.gemfile
|
21
|
+
- gemfiles/active_record_60.gemfile
|
22
|
+
- gemfiles/active_record_61.gemfile
|
23
|
+
- gemfiles/mongoid_54.gemfile
|
24
|
+
- gemfiles/mongoid_64.gemfile
|
25
|
+
- gemfiles/mongoid_70.gemfile
|
26
|
+
matrix:
|
27
|
+
exclude:
|
28
|
+
- gemfile: gemfiles/active_record_32.gemfile
|
29
|
+
rvm: 2.6
|
30
|
+
- gemfile: gemfiles/active_record_32.gemfile
|
31
|
+
rvm: 2.7
|
32
|
+
- gemfile: gemfiles/active_record_32.gemfile
|
33
|
+
env: ORM_TYPE=MONGOID
|
34
|
+
- gemfile: gemfiles/active_record_42.gemfile
|
35
|
+
rvm: 2.7
|
36
|
+
- gemfile: gemfiles/active_record_42.gemfile
|
37
|
+
env: ORM_TYPE=MONGOID
|
38
|
+
- gemfile: gemfiles/active_record_50.gemfile
|
39
|
+
env: ORM_TYPE=MONGOID
|
40
|
+
- gemfile: gemfiles/active_record_51.gemfile
|
41
|
+
env: ORM_TYPE=MONGOID
|
42
|
+
- gemfile: gemfiles/active_record_52.gemfile
|
43
|
+
env: ORM_TYPE=MONGOID
|
44
|
+
- gemfile: gemfiles/active_record_60.gemfile
|
45
|
+
rvm: 2.2
|
46
|
+
- gemfile: gemfiles/active_record_60.gemfile
|
47
|
+
env: ORM_TYPE=MONGOID
|
48
|
+
- gemfile: gemfiles/active_record_61.gemfile
|
49
|
+
rvm: 2.2
|
50
|
+
- gemfile: gemfiles/active_record_61.gemfile
|
51
|
+
env: ORM_TYPE=MONGOID
|
52
|
+
- gemfile: gemfiles/mongoid_54.gemfile
|
53
|
+
rvm: 2.7
|
54
|
+
- gemfile: gemfiles/mongoid_54.gemfile
|
55
|
+
env: ORM_TYPE=ACTIVE_RECORD
|
56
|
+
- gemfile: gemfiles/mongoid_64.gemfile
|
57
|
+
env: ORM_TYPE=ACTIVE_RECORD
|
58
|
+
- gemfile: gemfiles/mongoid_70.gemfile
|
59
|
+
env: ORM_TYPE=ACTIVE_RECORD
|
18
60
|
before_install:
|
19
|
-
-
|
20
|
-
|
61
|
+
- if `ruby -e 'exit(RUBY_VERSION.to_f < 2.7)'`; then
|
62
|
+
gem i rubygems-update -v '< 3' && update_rubygems;
|
63
|
+
gem install bundler -v '< 2';
|
64
|
+
fi
|
21
65
|
- gem --version
|
22
66
|
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
23
67
|
- chmod +x ./cc-test-reporter
|
24
68
|
- ./cc-test-reporter before-build
|
25
69
|
script:
|
26
|
-
- bundle exec rake
|
70
|
+
- if [ "$ORM_TYPE" = "ACTIVE_RECORD" ]; then bundle exec rake test_active_record; fi
|
71
|
+
- if [ "$ORM_TYPE" = "MONGOID" ]; then bundle exec rake test_mongoid; fi
|
27
72
|
after_script:
|
28
73
|
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,32 @@
|
|
1
1
|
## Change Log
|
2
2
|
|
3
|
+
### [v2.1.0](https://github.com/khiav223577/pluck_all/compare/v2.0.4...v2.1.0) 2020/10/19
|
4
|
+
- [#47](https://github.com/khiav223577/pluck_all/pull/47) Use instantiate method to initialize model without calling callbacks (@khiav223577)
|
5
|
+
- [#45](https://github.com/khiav223577/pluck_all/pull/45) Support Ruby 2.7 (@khiav223577)
|
6
|
+
- [#44](https://github.com/khiav223577/pluck_all/pull/44) Move `require` from begin..rescue block for better debug message (@khiav223577)
|
7
|
+
- [#43](https://github.com/khiav223577/pluck_all/pull/43) Use rails_compatibility to get attribute_types for better compatibility (@khiav223577)
|
8
|
+
- [#41](https://github.com/khiav223577/pluck_all/pull/41) Support Rails 6.0 (@khiav223577)
|
9
|
+
|
10
|
+
### [v2.0.4](https://github.com/khiav223577/pluck_all/compare/v2.0.3...v2.0.4) 2019/04/04
|
11
|
+
- [#40](https://github.com/khiav223577/pluck_all/pull/40) Fix: inconsistent with pluck when having `select` on relation (@khiav223577)
|
12
|
+
- [#39](https://github.com/khiav223577/pluck_all/pull/39) Fix: undefined local variable or method `construct_relation_for_association_calculations` in Rails 5.2.2.1 (@MasashiYokota)
|
13
|
+
- [#36](https://github.com/khiav223577/pluck_all/pull/36) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
|
14
|
+
- [#35](https://github.com/khiav223577/pluck_all/pull/35) refactor #test_pluck_with_includes (@khiav223577)
|
15
|
+
- [#34](https://github.com/khiav223577/pluck_all/pull/34) Provide Arel support as well (@snkashis)
|
16
|
+
- [#33](https://github.com/khiav223577/pluck_all/pull/33) Fix gemfile path in bin/setup (@snkashis)
|
17
|
+
- [#32](https://github.com/khiav223577/pluck_all/pull/32) Move patches into separate files (@khiav223577)
|
18
|
+
|
19
|
+
### [v2.0.3](https://github.com/khiav223577/pluck_all/compare/v2.0.2...v2.0.3) 2018/07/19
|
20
|
+
- [#30](https://github.com/khiav223577/pluck_all/pull/30) Fix: includes + pluck_all results in strange output (@khiav223577)
|
21
|
+
|
22
|
+
### [v2.0.2](https://github.com/khiav223577/pluck_all/compare/v2.0.1...v2.0.2) 2018/06/29
|
23
|
+
- [#28](https://github.com/khiav223577/pluck_all/pull/28) Fix test cases and the result format may be wrong when plucking multiple columns with nil value may (@khiav223577)
|
24
|
+
- [#27](https://github.com/khiav223577/pluck_all/pull/27) [Feature] Mongoid Hooks And Tests Separation (@berniechiu)
|
25
|
+
- [#26](https://github.com/khiav223577/pluck_all/pull/26) [FIX] Wrong Module Skipped (@berniechiu)
|
26
|
+
|
27
|
+
### [v2.0.1](https://github.com/khiav223577/pluck_all/compare/v2.0.0...v2.0.1) 2018/05/27
|
28
|
+
- [#25](https://github.com/khiav223577/pluck_all/pull/25) Fix that project without mongoid will raise LoadError (@khiav223577)
|
29
|
+
|
3
30
|
### [v2.0.0](https://github.com/khiav223577/pluck_all/compare/v1.2.4...v2.0.0) 2018/05/27
|
4
31
|
- [#24](https://github.com/khiav223577/pluck_all/pull/24) Support Mongoid! (@khiav223577)
|
5
32
|
- [#23](https://github.com/khiav223577/pluck_all/pull/23) Refactoring Coding Style (@khiav223577)
|
@@ -38,6 +65,6 @@
|
|
38
65
|
### [v1.1.0](https://github.com/khiav223577/pluck_all/compare/v1.0.1...v1.1.0) 2016/12/21
|
39
66
|
- [#3](https://github.com/khiav223577/pluck_all/pull/3) Feature/pluck array (@khiav223577)
|
40
67
|
|
41
|
-
### v1.0.1 2016/12/20
|
68
|
+
### [v1.0.1](https://github.com/khiav223577/pluck_all/compare/v1.0.0...v1.0.1) 2016/12/20
|
42
69
|
- [#2](https://github.com/khiav223577/pluck_all/pull/2) test with multiple rails version (@khiav223577)
|
43
70
|
- [#1](https://github.com/khiav223577/pluck_all/pull/1) add basic test cases (@khiav223577)
|
data/README.md
CHANGED
@@ -6,15 +6,17 @@
|
|
6
6
|
[](https://codeclimate.com/github/khiav223577/pluck_all)
|
7
7
|
[](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.
|
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,18 +90,50 @@ 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
|
102
139
|
### Support Pluck Carrierwave Uploader (if you use carrierwave)
|
@@ -110,12 +147,12 @@ User.where(xxx).map(&:profile_pic)
|
|
110
147
|
If the uploader use something like: `model.id`, `model.name`
|
111
148
|
You may have to send these columns manually:
|
112
149
|
```rb
|
113
|
-
User.where(xxx).cast_need_columns(%i
|
150
|
+
User.where(xxx).cast_need_columns(%i[id name]).pluck_all(:id, :name, :profile_pic).map{|s| s['profile_pic'] }
|
114
151
|
```
|
115
152
|
|
116
153
|
## Development
|
117
154
|
|
118
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake
|
155
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test_active_record` or `rake test_mongoid` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
119
156
|
|
120
157
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
121
158
|
|
data/Rakefile
CHANGED
@@ -1,10 +1,22 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
3
|
|
4
4
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs <<
|
6
|
-
t.libs <<
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
7
|
t.test_files = FileList['test/**/*_test.rb']
|
8
8
|
end
|
9
9
|
|
10
|
-
|
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,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pluck_all'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "pluck_all"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/bin/setup
CHANGED
@@ -0,0 +1,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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -0,0 +1,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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -0,0 +1,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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -0,0 +1,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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -0,0 +1,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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -0,0 +1,13 @@
|
|
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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -0,0 +1,13 @@
|
|
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 'carrierwave', '~> 0.11.0'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'simplecov'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec path: '../'
|
@@ -2,12 +2,10 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in pluck_all.gemspec
|
4
4
|
|
5
|
-
gem
|
5
|
+
gem 'mongoid', '~> 5.4.0'
|
6
6
|
|
7
7
|
group :test do
|
8
|
-
gem
|
9
|
-
gem 'carrierwave', '~> 0.11.0'
|
8
|
+
gem 'simplecov'
|
10
9
|
end
|
11
10
|
|
12
|
-
gemspec :
|
13
|
-
|
11
|
+
gemspec path: '../'
|
data/lib/pluck_all.rb
CHANGED
@@ -1,132 +1,3 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
require 'pluck_all/version'
|
3
|
-
require '
|
4
|
-
begin
|
5
|
-
require 'mongoid'
|
6
|
-
require 'pluck_all/mongoid_pluck_all'
|
7
|
-
rescue LoadError, Gem::LoadError
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
class ActiveRecord::Base
|
12
|
-
if !defined?(attribute_types) && defined?(column_types)
|
13
|
-
class << self
|
14
|
-
# column_types was changed to attribute_types in Rails 5
|
15
|
-
alias_method :attribute_types, :column_types
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
module ActiveRecord
|
21
|
-
[
|
22
|
-
*([Type::Value, Type::Integer, Type::Serialized] if defined?(Type::Value)),
|
23
|
-
*([Enum::EnumType] if defined?(Enum::EnumType)),
|
24
|
-
].each do |s|
|
25
|
-
s.class_eval do
|
26
|
-
if !method_defined?(:deserialize) && method_defined?(:type_cast_from_database)
|
27
|
-
# column_types was changed to attribute_types in Rails 5
|
28
|
-
alias deserialize type_cast_from_database
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class ActiveRecord::Relation
|
35
|
-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
|
36
|
-
def pluck_all(*args)
|
37
|
-
result = select_all(*args)
|
38
|
-
result.map! do |attributes| # This map! behaves different to array#map!
|
39
|
-
initialized_attributes = klass.initialize_attributes(attributes)
|
40
|
-
attributes.each do |key, attribute|
|
41
|
-
attributes[key] = klass.type_cast_attribute(key, initialized_attributes) #TODO 現在AS過後的type cast會有一點問題
|
42
|
-
end
|
43
|
-
cast_carrier_wave_uploader_url(attributes)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
else
|
47
|
-
def pluck_all(*args)
|
48
|
-
result = select_all(*args)
|
49
|
-
attribute_types = klass.attribute_types
|
50
|
-
result.map! do |attributes| # This map! behaves different to array#map!
|
51
|
-
attributes.each do |key, attribute|
|
52
|
-
attributes[key] = result.send(:column_type, key, attribute_types).deserialize(attribute) #TODO 現在AS過後的type cast會有一點問題,但似乎原生的pluck也有此問題
|
53
|
-
end
|
54
|
-
cast_carrier_wave_uploader_url(attributes)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def cast_need_columns(column_names, _klass = nil)
|
60
|
-
@pluck_all_cast_need_columns = column_names.map(&:to_s)
|
61
|
-
@pluck_all_cast_klass = _klass
|
62
|
-
return self
|
63
|
-
end
|
64
|
-
|
65
|
-
private
|
66
|
-
|
67
|
-
def select_all(*args)
|
68
|
-
args.map! do |column_name|
|
69
|
-
if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s)
|
70
|
-
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
|
71
|
-
else
|
72
|
-
column_name.to_s
|
73
|
-
end
|
74
|
-
end
|
75
|
-
relation = clone
|
76
|
-
return klass.connection.select_all(relation.select(args).to_sql)
|
77
|
-
#return klass.connection.select_all(relation.arel)
|
78
|
-
end
|
79
|
-
|
80
|
-
# ----------------------------------------------------------------
|
81
|
-
# ● Support casting CarrierWave url
|
82
|
-
# ----------------------------------------------------------------
|
83
|
-
def cast_carrier_wave_uploader_url(attributes)
|
84
|
-
if defined?(CarrierWave) && klass.respond_to?(:uploaders)
|
85
|
-
@pluck_all_cast_klass ||= klass
|
86
|
-
@pluck_all_uploaders ||= @pluck_all_cast_klass.uploaders.select{|key, uploader| attributes.key?(key.to_s) }
|
87
|
-
@pluck_all_uploaders.each do |key, uploader|
|
88
|
-
hash = {}
|
89
|
-
@pluck_all_cast_need_columns.each{|k| hash[k] = attributes[k] } if @pluck_all_cast_need_columns
|
90
|
-
obj = @pluck_all_cast_klass.new(hash)
|
91
|
-
obj[key] = attributes[key_s = key.to_s]
|
92
|
-
#https://github.com/carrierwaveuploader/carrierwave/blob/87c37b706c560de6d01816f9ebaa15ce1c51ed58/lib/carrierwave/mount.rb#L142
|
93
|
-
attributes[key_s] = obj.send(key)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
return attributes
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
class ActiveRecord::Relation
|
101
|
-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.2')
|
102
|
-
def pluck_array(*args)
|
103
|
-
return pluck_all(*args).map{|hash|
|
104
|
-
result = hash.values #P.S. 這裡是相信ruby 1.9以後,hash.values的順序跟insert的順序一樣。
|
105
|
-
next (result.one? ? result.first : result)
|
106
|
-
}
|
107
|
-
end
|
108
|
-
else
|
109
|
-
alias_method :pluck_array, :pluck if not method_defined?(:pluck_array)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
|
114
|
-
class << ActiveRecord::Base
|
115
|
-
def cast_need_columns(*args)
|
116
|
-
where(nil).cast_need_columns(*args)
|
117
|
-
end
|
118
|
-
|
119
|
-
def pluck_all(*args)
|
120
|
-
where(nil).pluck_all(*args)
|
121
|
-
end
|
122
|
-
|
123
|
-
def pluck_array(*args)
|
124
|
-
where(nil).pluck_array(*args)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
module ActiveRecord::NullRelation
|
129
|
-
def pluck_all(*args)
|
130
|
-
[]
|
131
|
-
end
|
132
|
-
end
|
3
|
+
require 'pluck_all/hooks'
|