deep_pluck 1.1.0 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/FUNDING.yml +12 -0
- data/.gitignore +10 -9
- data/.rubocop.yml +1227 -1193
- data/.travis.yml +42 -21
- data/CHANGELOG.md +63 -0
- data/CODE_OF_CONDUCT.md +48 -48
- data/LICENSE.txt +21 -21
- data/README.md +172 -145
- data/Rakefile +5 -5
- data/bin/console +3 -3
- data/bin/setup +8 -8
- data/deep_pluck.gemspec +45 -38
- data/gemfiles/3.2.gemfile +11 -14
- data/gemfiles/4.2.gemfile +11 -14
- data/gemfiles/5.0.gemfile +11 -14
- data/gemfiles/5.1.gemfile +11 -14
- data/gemfiles/5.2.gemfile +11 -0
- data/gemfiles/6.0.gemfile +11 -0
- data/gemfiles/6.1.gemfile +11 -0
- data/lib/deep_pluck.rb +23 -24
- data/lib/deep_pluck/data_combiner.rb +3 -3
- data/lib/deep_pluck/model.rb +64 -12
- data/lib/deep_pluck/version.rb +3 -3
- metadata +38 -10
- data/lib/deep_pluck/preloaded_model.rb +0 -12
data/.travis.yml
CHANGED
@@ -1,21 +1,42 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
-
|
9
|
-
-
|
10
|
-
|
11
|
-
gemfile
|
12
|
-
- gemfiles/
|
13
|
-
- gemfiles/
|
14
|
-
- gemfiles/5.
|
15
|
-
- gemfiles/5.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
1
|
+
sudo: false
|
2
|
+
env:
|
3
|
+
global:
|
4
|
+
- CC_TEST_REPORTER_ID=29e69e7da6b55678f27b7de2444b6161a6c98e3c4b22e0e017ebab87ada18fd5
|
5
|
+
language: ruby
|
6
|
+
rvm:
|
7
|
+
- 2.2
|
8
|
+
- 2.6
|
9
|
+
- 2.7
|
10
|
+
gemfile:
|
11
|
+
- gemfiles/3.2.gemfile
|
12
|
+
- gemfiles/4.2.gemfile
|
13
|
+
- gemfiles/5.0.gemfile
|
14
|
+
- gemfiles/5.1.gemfile
|
15
|
+
- gemfiles/5.2.gemfile
|
16
|
+
- gemfiles/6.0.gemfile
|
17
|
+
- gemfiles/6.1.gemfile
|
18
|
+
matrix:
|
19
|
+
exclude:
|
20
|
+
- gemfile: gemfiles/3.2.gemfile
|
21
|
+
rvm: 2.6
|
22
|
+
- gemfile: gemfiles/3.2.gemfile
|
23
|
+
rvm: 2.7
|
24
|
+
- gemfile: gemfiles/4.2.gemfile
|
25
|
+
rvm: 2.7
|
26
|
+
- gemfile: gemfiles/6.0.gemfile
|
27
|
+
rvm: 2.2
|
28
|
+
- gemfile: gemfiles/6.1.gemfile
|
29
|
+
rvm: 2.2
|
30
|
+
before_install:
|
31
|
+
- if `ruby -e 'exit(RUBY_VERSION.to_f < 2.7)'`; then
|
32
|
+
gem i rubygems-update -v '< 3' && update_rubygems;
|
33
|
+
gem install bundler -v '< 2';
|
34
|
+
fi
|
35
|
+
- gem --version
|
36
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
37
|
+
- chmod +x ./cc-test-reporter
|
38
|
+
- ./cc-test-reporter before-build
|
39
|
+
script:
|
40
|
+
- bundle exec rake test
|
41
|
+
after_script:
|
42
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
## Change Log
|
2
|
+
|
3
|
+
### [v1.1.4](https://github.com/khiav223577/deep_pluck/compare/v1.1.3...v1.1.4) 2020/01/13
|
4
|
+
- [#36](https://github.com/khiav223577/deep_pluck/pull/36) A workaround to fix mismatched association named. (@khiav223577)
|
5
|
+
- [#35](https://github.com/khiav223577/deep_pluck/pull/35) Support Ruby 2.7 (@khiav223577)
|
6
|
+
|
7
|
+
### [v1.1.3](https://github.com/khiav223577/deep_pluck/compare/v1.1.2...v1.1.3) 2019/12/17
|
8
|
+
- [#34](https://github.com/khiav223577/deep_pluck/pull/34) Support for plucking directly on a has_one through association (@khiav223577)
|
9
|
+
|
10
|
+
### [v1.1.2](https://github.com/khiav223577/deep_pluck/compare/v1.1.1...v1.1.2) 2019/09/25
|
11
|
+
- [#32](https://github.com/khiav223577/deep_pluck/pull/32) Remove unneeded `PreloadedModel` (@khiav223577)
|
12
|
+
- [#31](https://github.com/khiav223577/deep_pluck/pull/31) Support Rails 6.0 (@khiav223577)
|
13
|
+
- [#30](https://github.com/khiav223577/deep_pluck/pull/30) Lock sqlite3 version to 1.3.x (@khiav223577)
|
14
|
+
- [#28](https://github.com/khiav223577/deep_pluck/pull/28) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
|
15
|
+
- [#27](https://github.com/khiav223577/deep_pluck/pull/27) Remove deprecated codeclimate-test-reporter gem and update travis config (@khiav223577)
|
16
|
+
|
17
|
+
### [v1.1.1](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...v1.1.1) 2018/07/08
|
18
|
+
- [#26](https://github.com/khiav223577/deep_pluck/pull/26) Fix: `id` may disappear when plucking at model instance (@khiav223577)
|
19
|
+
- [#25](https://github.com/khiav223577/deep_pluck/pull/25) Refactor - move models definition to separate files (@khiav223577)
|
20
|
+
- [#24](https://github.com/khiav223577/deep_pluck/pull/24) test Rails 5.2 (@khiav223577)
|
21
|
+
- [#23](https://github.com/khiav223577/deep_pluck/pull/23) should test both 5.0.x and 5.1.x (@khiav223577)
|
22
|
+
- [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
|
23
|
+
- [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
|
24
|
+
|
25
|
+
### [v1.1.0](https://github.com/khiav223577/deep_pluck/compare/v1.0.3...v1.1.0) 2018/02/15
|
26
|
+
- [#19](https://github.com/khiav223577/deep_pluck/pull/19) Support deep_pluck at active model (@khiav223577)
|
27
|
+
- [#18](https://github.com/khiav223577/deep_pluck/pull/18) Add rubocop and Improve code quality (@khiav223577)
|
28
|
+
|
29
|
+
### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.2...v1.0.3) 2017/06/30
|
30
|
+
- [#15](https://github.com/khiav223577/deep_pluck/pull/15) test deep_pluck in rails 5.1.x (@khiav223577)
|
31
|
+
- [#14](https://github.com/khiav223577/deep_pluck/pull/14) Handle polymorphic associations correctly. (@Bogadon)
|
32
|
+
|
33
|
+
### [v1.0.0](https://github.com/khiav223577/deep_pluck/compare/v0.1.4...v1.0.0) 2017/03/28
|
34
|
+
- [#12](https://github.com/khiav223577/deep_pluck/pull/12) reduce cyclomatic complexity in model.rb (@khiav223577)
|
35
|
+
|
36
|
+
### [v0.1.4](https://github.com/khiav223577/deep_pluck/compare/v0.1.3...v0.1.4) 2017/03/27
|
37
|
+
- [#11](https://github.com/khiav223577/deep_pluck/pull/11) Fix conditional associations (@khiav223577)
|
38
|
+
|
39
|
+
### [v0.1.3](https://github.com/khiav223577/deep_pluck/compare/v0.1.2...v0.1.3) 2017/03/20
|
40
|
+
- [#10](https://github.com/khiav223577/deep_pluck/pull/10) fix custom foreign_key, custom primary_key issues (@khiav223577)
|
41
|
+
- [#9](https://github.com/khiav223577/deep_pluck/pull/9) fix has_and_belongs_to_many (@khiav223577)
|
42
|
+
|
43
|
+
### [v0.1.2](https://github.com/khiav223577/deep_pluck/compare/v0.1.1...v0.1.2) 2017/03/17
|
44
|
+
- [#8](https://github.com/khiav223577/deep_pluck/pull/8) fix some need columns is missing (@khiav223577)
|
45
|
+
- [#7](https://github.com/khiav223577/deep_pluck/pull/7) raise error message when association not found (@khiav223577)
|
46
|
+
|
47
|
+
### [v0.1.1](https://github.com/khiav223577/deep_pluck/compare/v0.1.0...v0.1.1) 2017/03/16
|
48
|
+
- [#6](https://github.com/khiav223577/deep_pluck/pull/6) fix deep_pluck with #joins (@khiav223577)
|
49
|
+
|
50
|
+
### [v0.1.0](https://github.com/khiav223577/deep_pluck/compare/v0.0.4...v0.1.0) 2017/03/15
|
51
|
+
- [#5](https://github.com/khiav223577/deep_pluck/pull/5) Support pluck many-to-many associations (@khiav223577)
|
52
|
+
|
53
|
+
### [v0.0.4](https://github.com/khiav223577/deep_pluck/compare/v0.0.3...v0.0.4) 2017/03/14
|
54
|
+
- [#4](https://github.com/khiav223577/deep_pluck/pull/4) Fix use deep_pluck on NullRelation will raise exception and prevent unneeded query. (@khiav223577)
|
55
|
+
|
56
|
+
### [v0.0.3](https://github.com/khiav223577/deep_pluck/compare/v0.0.2...v0.0.3) 2017/03/06
|
57
|
+
- [#3](https://github.com/khiav223577/deep_pluck/pull/3) support more than two level (@khiav223577)
|
58
|
+
|
59
|
+
### [v0.0.2](https://github.com/khiav223577/deep_pluck/compare/v0.0.1...v0.0.2) 2017/03/06
|
60
|
+
- [#2](https://github.com/khiav223577/deep_pluck/pull/2) The result of has_one association should not be array (@khiav223577)
|
61
|
+
|
62
|
+
### v0.0.1 2017/03/04
|
63
|
+
- [#1](https://github.com/khiav223577/deep_pluck/pull/1) deep pluck (@khiav223577)
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,49 +1,49 @@
|
|
1
|
-
# Contributor Code of Conduct
|
2
|
-
|
3
|
-
As contributors and maintainers of this project, and in the interest of
|
4
|
-
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
-
contribute through reporting issues, posting feature requests, updating
|
6
|
-
documentation, submitting pull requests or patches, and other activities.
|
7
|
-
|
8
|
-
We are committed to making participation in this project a harassment-free
|
9
|
-
experience for everyone, regardless of level of experience, gender, gender
|
10
|
-
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
-
body size, race, ethnicity, age, religion, or nationality.
|
12
|
-
|
13
|
-
Examples of unacceptable behavior by participants include:
|
14
|
-
|
15
|
-
* The use of sexualized language or imagery
|
16
|
-
* Personal attacks
|
17
|
-
* Trolling or insulting/derogatory comments
|
18
|
-
* Public or private harassment
|
19
|
-
* Publishing other's private information, such as physical or electronic
|
20
|
-
addresses, without explicit permission
|
21
|
-
* Other unethical or unprofessional conduct
|
22
|
-
|
23
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
-
threatening, offensive, or harmful.
|
28
|
-
|
29
|
-
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
-
fairly and consistently applying these principles to every aspect of managing
|
31
|
-
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
-
Conduct may be permanently removed from the project team.
|
33
|
-
|
34
|
-
This code of conduct applies both within project spaces and in public spaces
|
35
|
-
when an individual is representing the project or its community.
|
36
|
-
|
37
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
-
reported by contacting a project maintainer at mrtmrt15xn@yahoo.com.tw. All
|
39
|
-
complaints will be reviewed and investigated and will result in a response that
|
40
|
-
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
-
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
-
incident.
|
43
|
-
|
44
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
-
version 1.3.0, available at
|
46
|
-
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
-
|
48
|
-
[homepage]: http://contributor-covenant.org
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at mrtmrt15xn@yahoo.com.tw. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
49
|
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/LICENSE.txt
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2016 khiav reoy
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 khiav reoy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,145 +1,172 @@
|
|
1
|
-
# DeepPluck
|
2
|
-
|
3
|
-
[![Gem Version](https://img.shields.io/gem/v/deep_pluck.svg?style=flat)](http://rubygems.org/gems/deep_pluck)
|
4
|
-
[![Build Status](https://travis-ci.org/khiav223577/deep_pluck.svg?branch=master)](https://travis-ci.org/khiav223577/deep_pluck)
|
5
|
-
[![RubyGems](http://img.shields.io/gem/dt/deep_pluck.svg?style=flat)](http://rubygems.org/gems/deep_pluck)
|
6
|
-
[![Code Climate](https://codeclimate.com/github/khiav223577/deep_pluck/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/deep_pluck)
|
7
|
-
[![Test Coverage](https://codeclimate.com/github/khiav223577/deep_pluck/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/deep_pluck/coverage)
|
8
|
-
|
9
|
-
Allow you to pluck deeply into nested associations without loading a bunch of records.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
```rb
|
36
|
-
User.deep_pluck(:id, :name)
|
37
|
-
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
38
|
-
# =>
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
#
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
User
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
```rb
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
The
|
145
|
-
|
1
|
+
# DeepPluck
|
2
|
+
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/deep_pluck.svg?style=flat)](http://rubygems.org/gems/deep_pluck)
|
4
|
+
[![Build Status](https://travis-ci.org/khiav223577/deep_pluck.svg?branch=master)](https://travis-ci.org/khiav223577/deep_pluck)
|
5
|
+
[![RubyGems](http://img.shields.io/gem/dt/deep_pluck.svg?style=flat)](http://rubygems.org/gems/deep_pluck)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/khiav223577/deep_pluck/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/deep_pluck)
|
7
|
+
[![Test Coverage](https://codeclimate.com/github/khiav223577/deep_pluck/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/deep_pluck/coverage)
|
8
|
+
|
9
|
+
Allow you to pluck deeply into nested associations without loading a bunch of records.
|
10
|
+
|
11
|
+
## Supports
|
12
|
+
- Ruby 2.2 ~ 2.7
|
13
|
+
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'deep_pluck'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install deep_pluck
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Similar to #pluck method
|
34
|
+
|
35
|
+
```rb
|
36
|
+
User.deep_pluck(:id, :name)
|
37
|
+
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
38
|
+
# =>
|
39
|
+
# [
|
40
|
+
# {'id' => 1, 'name' => 'David'},
|
41
|
+
# {'id' => 2, 'name' => 'Jeremy'},
|
42
|
+
# ]
|
43
|
+
```
|
44
|
+
|
45
|
+
### Pluck attributes from nested associations
|
46
|
+
|
47
|
+
```rb
|
48
|
+
User.deep_pluck(:name, 'posts' => :title)
|
49
|
+
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
50
|
+
# SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
|
51
|
+
# =>
|
52
|
+
# [
|
53
|
+
# {
|
54
|
+
# 'name' => 'David' ,
|
55
|
+
# 'posts' => [
|
56
|
+
# {'title' => 'post1'},
|
57
|
+
# {'title' => 'post2'},
|
58
|
+
# ],
|
59
|
+
# },
|
60
|
+
# {
|
61
|
+
# 'name' => 'Jeremy',
|
62
|
+
# 'posts' => [
|
63
|
+
# {'title' => 'post3'},
|
64
|
+
# ],
|
65
|
+
# },
|
66
|
+
# ]
|
67
|
+
```
|
68
|
+
|
69
|
+
### Pluck at models
|
70
|
+
|
71
|
+
```rb
|
72
|
+
user = User.find_by(name: 'David')
|
73
|
+
user.deep_pluck(:name, :posts => :title)
|
74
|
+
# =>
|
75
|
+
# {
|
76
|
+
# 'name' => 'David' ,
|
77
|
+
# :posts => [
|
78
|
+
# {'title' => 'post1'},
|
79
|
+
# {'title' => 'post2'},
|
80
|
+
# ],
|
81
|
+
# }
|
82
|
+
```
|
83
|
+
|
84
|
+
### Compare with using `#as_json`
|
85
|
+
|
86
|
+
Assume the following relations:
|
87
|
+
|
88
|
+
> User has_many Posts.<br>
|
89
|
+
> Post has_many PostComments.<br>
|
90
|
+
> User has_one Contact.<br>
|
91
|
+
|
92
|
+
And the following #as_json example:
|
93
|
+
```rb
|
94
|
+
User.where(:name => %w(Pearl Doggy)).includes([{:posts => :post_comments}, :contact]).as_json({
|
95
|
+
:root => false,
|
96
|
+
:only => [:name, :email],
|
97
|
+
:include => {
|
98
|
+
'posts' => {
|
99
|
+
:only => :name,
|
100
|
+
:include => {
|
101
|
+
'post_comments' => {
|
102
|
+
:only => :comment,
|
103
|
+
},
|
104
|
+
},
|
105
|
+
},
|
106
|
+
'contact' => {
|
107
|
+
:only => :address,
|
108
|
+
},
|
109
|
+
},
|
110
|
+
})
|
111
|
+
|
112
|
+
```
|
113
|
+
It works as expected, but is not very DRY, repeat writing `include`, `posts`, `post_comments` so many times.
|
114
|
+
|
115
|
+
Not to mention the huge performace improvement by using #deep_pluck.
|
116
|
+
|
117
|
+
You could refactor the example with #deep_pluck:
|
118
|
+
```rb
|
119
|
+
User.where(:name => %w(Pearl Doggy)).deep_pluck(
|
120
|
+
:name,
|
121
|
+
:email,
|
122
|
+
'posts' => [:name, 'post_comments' => :comment],
|
123
|
+
'contact' => :address,
|
124
|
+
)
|
125
|
+
```
|
126
|
+
|
127
|
+
### Better Performance
|
128
|
+
|
129
|
+
#deep_pluck return raw hash data without loading a bunch of records, so that faster than #as_json, or #select.
|
130
|
+
|
131
|
+
The following is the benchmark test on 3 users, 6 posts, where `users` table have 14 columns and `posts` have 6 columns. As it shows, `deep_pluck` is 4x faster than `as_json`.
|
132
|
+
|
133
|
+
|
134
|
+
```rb
|
135
|
+
# Repeat 500 times
|
136
|
+
# User.includes(:posts).as_json(:only => :email, :include => {:posts => {:only => :title}})
|
137
|
+
# User.deep_pluck(:email, {'posts' => :title})
|
138
|
+
|
139
|
+
user system total real
|
140
|
+
as_json 1.740000 1.230000 2.970000 ( 3.231465)
|
141
|
+
deep_pluck 0.660000 0.030000 0.690000 ( 0.880018)
|
142
|
+
```
|
143
|
+
|
144
|
+
The following is the benchmark test on 10000 users, where `users` table have 46 columns. As it shows, `deep_pluck` is 40x faster than `as_json` and 4x faster than `map`.
|
145
|
+
```rb
|
146
|
+
# Repeat 1 times
|
147
|
+
# User.select('account, email').map{|s| {'account' => s.account, 'email' => s.email}}
|
148
|
+
# User.select('account, email').as_json(:only => [:account, :email])
|
149
|
+
# User.deep_pluck(:account, :email)
|
150
|
+
|
151
|
+
user system total real
|
152
|
+
map 0.210000 0.000000 0.210000 ( 0.225421)
|
153
|
+
as_json 1.980000 0.060000 2.040000 ( 2.042205)
|
154
|
+
deep_pluck 0.040000 0.000000 0.040000 ( 0.051673)
|
155
|
+
```
|
156
|
+
|
157
|
+
|
158
|
+
## Development
|
159
|
+
|
160
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
161
|
+
|
162
|
+
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).
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/deep_pluck. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
167
|
+
|
168
|
+
|
169
|
+
## License
|
170
|
+
|
171
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
172
|
+
|