deep_pluck 1.1.2 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +9 -9
- data/.rubocop.yml +1228 -1228
- data/.travis.yml +34 -34
- data/CHANGELOG.md +54 -47
- data/CODE_OF_CONDUCT.md +48 -48
- data/LICENSE.txt +21 -21
- data/README.md +171 -171
- data/bin/console +0 -0
- data/bin/setup +8 -8
- data/deep_pluck.gemspec +45 -44
- data/gemfiles/3.2.gemfile +11 -11
- data/gemfiles/4.2.gemfile +11 -11
- data/gemfiles/5.0.gemfile +11 -11
- data/gemfiles/5.1.gemfile +11 -11
- data/gemfiles/5.2.gemfile +11 -11
- data/gemfiles/6.0.gemfile +11 -11
- data/lib/deep_pluck.rb +23 -23
- data/lib/deep_pluck/model.rb +209 -196
- data/lib/deep_pluck/version.rb +3 -3
- metadata +17 -4
data/.travis.yml
CHANGED
@@ -1,34 +1,34 @@
|
|
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
|
-
gemfile:
|
10
|
-
- gemfiles/3.2.gemfile
|
11
|
-
- gemfiles/4.2.gemfile
|
12
|
-
- gemfiles/5.0.gemfile
|
13
|
-
- gemfiles/5.1.gemfile
|
14
|
-
- gemfiles/5.2.gemfile
|
15
|
-
- gemfiles/6.0.gemfile
|
16
|
-
matrix:
|
17
|
-
exclude:
|
18
|
-
- gemfile: gemfiles/3.2.gemfile
|
19
|
-
rvm: 2.6
|
20
|
-
- gemfile: gemfiles/4.0.gemfile
|
21
|
-
rvm: 2.6
|
22
|
-
- gemfile: gemfiles/6.0.gemfile
|
23
|
-
rvm: 2.2
|
24
|
-
before_install:
|
25
|
-
- gem i rubygems-update -v '<3' && update_rubygems
|
26
|
-
- gem install bundler -v 1.17.3
|
27
|
-
- gem --version
|
28
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
29
|
-
- chmod +x ./cc-test-reporter
|
30
|
-
- ./cc-test-reporter before-build
|
31
|
-
script:
|
32
|
-
- bundle exec rake test
|
33
|
-
after_script:
|
34
|
-
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
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
|
+
gemfile:
|
10
|
+
- gemfiles/3.2.gemfile
|
11
|
+
- gemfiles/4.2.gemfile
|
12
|
+
- gemfiles/5.0.gemfile
|
13
|
+
- gemfiles/5.1.gemfile
|
14
|
+
- gemfiles/5.2.gemfile
|
15
|
+
- gemfiles/6.0.gemfile
|
16
|
+
matrix:
|
17
|
+
exclude:
|
18
|
+
- gemfile: gemfiles/3.2.gemfile
|
19
|
+
rvm: 2.6
|
20
|
+
- gemfile: gemfiles/4.0.gemfile
|
21
|
+
rvm: 2.6
|
22
|
+
- gemfile: gemfiles/6.0.gemfile
|
23
|
+
rvm: 2.2
|
24
|
+
before_install:
|
25
|
+
- gem i rubygems-update -v '<3' && update_rubygems
|
26
|
+
- gem install bundler -v 1.17.3
|
27
|
+
- gem --version
|
28
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
29
|
+
- chmod +x ./cc-test-reporter
|
30
|
+
- ./cc-test-reporter before-build
|
31
|
+
script:
|
32
|
+
- bundle exec rake test
|
33
|
+
after_script:
|
34
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/CHANGELOG.md
CHANGED
@@ -1,47 +1,54 @@
|
|
1
|
-
## Change Log
|
2
|
-
|
3
|
-
### [v1.1.
|
4
|
-
- [#
|
5
|
-
- [#
|
6
|
-
- [#
|
7
|
-
- [#
|
8
|
-
- [#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
- [#
|
13
|
-
- [#
|
14
|
-
|
15
|
-
|
16
|
-
- [#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- [#
|
21
|
-
|
22
|
-
### [
|
23
|
-
- [#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- [#
|
28
|
-
|
29
|
-
### [v0.1.
|
30
|
-
- [#
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
- [#
|
35
|
-
|
36
|
-
### [v0.1.
|
37
|
-
- [#
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
- [#
|
1
|
+
## Change Log
|
2
|
+
|
3
|
+
### [v1.1.2](https://github.com/khiav223577/deep_pluck/compare/v1.1.1...v1.1.2) 2019/09/25
|
4
|
+
- [#32](https://github.com/khiav223577/deep_pluck/pull/32) Remove unneeded `PreloadedModel` (@khiav223577)
|
5
|
+
- [#31](https://github.com/khiav223577/deep_pluck/pull/31) Support Rails 6.0 (@khiav223577)
|
6
|
+
- [#30](https://github.com/khiav223577/deep_pluck/pull/30) Lock sqlite3 version to 1.3.x (@khiav223577)
|
7
|
+
- [#28](https://github.com/khiav223577/deep_pluck/pull/28) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
|
8
|
+
- [#27](https://github.com/khiav223577/deep_pluck/pull/27) Remove deprecated codeclimate-test-reporter gem and update travis config (@khiav223577)
|
9
|
+
|
10
|
+
### [v1.1.1](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...v1.1.1) 2018/07/08
|
11
|
+
- [#26](https://github.com/khiav223577/deep_pluck/pull/26) Fix: `id` may disappear when plucking at model instance (@khiav223577)
|
12
|
+
- [#25](https://github.com/khiav223577/deep_pluck/pull/25) Refactor - move models definition to separate files (@khiav223577)
|
13
|
+
- [#24](https://github.com/khiav223577/deep_pluck/pull/24) test Rails 5.2 (@khiav223577)
|
14
|
+
- [#23](https://github.com/khiav223577/deep_pluck/pull/23) should test both 5.0.x and 5.1.x (@khiav223577)
|
15
|
+
- [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
|
16
|
+
- [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
|
17
|
+
|
18
|
+
### [v1.1.0](https://github.com/khiav223577/deep_pluck/compare/v1.0.3...v1.1.0) 2018/02/15
|
19
|
+
- [#19](https://github.com/khiav223577/deep_pluck/pull/19) Support deep_pluck at active model (@khiav223577)
|
20
|
+
- [#18](https://github.com/khiav223577/deep_pluck/pull/18) Add rubocop and Improve code quality (@khiav223577)
|
21
|
+
|
22
|
+
### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.2...v1.0.3) 2017/06/30
|
23
|
+
- [#15](https://github.com/khiav223577/deep_pluck/pull/15) test deep_pluck in rails 5.1.x (@khiav223577)
|
24
|
+
- [#14](https://github.com/khiav223577/deep_pluck/pull/14) Handle polymorphic associations correctly. (@Bogadon)
|
25
|
+
|
26
|
+
### [v1.0.0](https://github.com/khiav223577/deep_pluck/compare/v0.1.4...v1.0.0) 2017/03/28
|
27
|
+
- [#12](https://github.com/khiav223577/deep_pluck/pull/12) reduce cyclomatic complexity in model.rb (@khiav223577)
|
28
|
+
|
29
|
+
### [v0.1.4](https://github.com/khiav223577/deep_pluck/compare/v0.1.3...v0.1.4) 2017/03/27
|
30
|
+
- [#11](https://github.com/khiav223577/deep_pluck/pull/11) Fix conditional associations (@khiav223577)
|
31
|
+
|
32
|
+
### [v0.1.3](https://github.com/khiav223577/deep_pluck/compare/v0.1.2...v0.1.3) 2017/03/20
|
33
|
+
- [#10](https://github.com/khiav223577/deep_pluck/pull/10) fix custom foreign_key, custom primary_key issues (@khiav223577)
|
34
|
+
- [#9](https://github.com/khiav223577/deep_pluck/pull/9) fix has_and_belongs_to_many (@khiav223577)
|
35
|
+
|
36
|
+
### [v0.1.2](https://github.com/khiav223577/deep_pluck/compare/v0.1.1...v0.1.2) 2017/03/17
|
37
|
+
- [#8](https://github.com/khiav223577/deep_pluck/pull/8) fix some need columns is missing (@khiav223577)
|
38
|
+
- [#7](https://github.com/khiav223577/deep_pluck/pull/7) raise error message when association not found (@khiav223577)
|
39
|
+
|
40
|
+
### [v0.1.1](https://github.com/khiav223577/deep_pluck/compare/v0.1.0...v0.1.1) 2017/03/16
|
41
|
+
- [#6](https://github.com/khiav223577/deep_pluck/pull/6) fix deep_pluck with #joins (@khiav223577)
|
42
|
+
|
43
|
+
### [v0.1.0](https://github.com/khiav223577/deep_pluck/compare/v0.0.4...v0.1.0) 2017/03/15
|
44
|
+
- [#5](https://github.com/khiav223577/deep_pluck/pull/5) Support pluck many-to-many associations (@khiav223577)
|
45
|
+
|
46
|
+
### [v0.0.4](https://github.com/khiav223577/deep_pluck/compare/v0.0.3...v0.0.4) 2017/03/14
|
47
|
+
- [#4](https://github.com/khiav223577/deep_pluck/pull/4) Fix use deep_pluck on NullRelation will raise exception and prevent unneeded query. (@khiav223577)
|
48
|
+
|
49
|
+
### [v0.0.3](https://github.com/khiav223577/deep_pluck/compare/v0.0.2...v0.0.3) 2017/03/06
|
50
|
+
- [#3](https://github.com/khiav223577/deep_pluck/pull/3) support more than two level (@khiav223577)
|
51
|
+
|
52
|
+
### [v0.0.2](https://github.com/khiav223577/deep_pluck/compare/v0.0.1...v0.0.2) 2017/03/06
|
53
|
+
- [#2](https://github.com/khiav223577/deep_pluck/pull/2) The result of has_one association should not be array (@khiav223577)
|
54
|
+
- [#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,171 +1,171 @@
|
|
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
|
-
Support Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0.
|
12
|
-
|
13
|
-
|
14
|
-
## Installation
|
15
|
-
|
16
|
-
Add this line to your application's Gemfile:
|
17
|
-
|
18
|
-
```ruby
|
19
|
-
gem 'deep_pluck'
|
20
|
-
```
|
21
|
-
|
22
|
-
And then execute:
|
23
|
-
|
24
|
-
$ bundle
|
25
|
-
|
26
|
-
Or install it yourself as:
|
27
|
-
|
28
|
-
$ gem install deep_pluck
|
29
|
-
|
30
|
-
## Usage
|
31
|
-
|
32
|
-
### Similar to #pluck method
|
33
|
-
|
34
|
-
```rb
|
35
|
-
User.deep_pluck(:id, :name)
|
36
|
-
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
37
|
-
# =>
|
38
|
-
# [
|
39
|
-
# {'id' => 1, 'name' => 'David'},
|
40
|
-
# {'id' => 2, 'name' => 'Jeremy'},
|
41
|
-
# ]
|
42
|
-
```
|
43
|
-
|
44
|
-
### Pluck attributes from nested associations
|
45
|
-
|
46
|
-
```rb
|
47
|
-
User.deep_pluck(:name, 'posts' => :title)
|
48
|
-
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
49
|
-
# SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
|
50
|
-
# =>
|
51
|
-
# [
|
52
|
-
# {
|
53
|
-
# 'name' => 'David' ,
|
54
|
-
# 'posts' => [
|
55
|
-
# {'title' => 'post1'},
|
56
|
-
# {'title' => 'post2'},
|
57
|
-
# ],
|
58
|
-
# },
|
59
|
-
# {
|
60
|
-
# 'name' => 'Jeremy',
|
61
|
-
# 'posts' => [
|
62
|
-
# {'title' => 'post3'},
|
63
|
-
# ],
|
64
|
-
# },
|
65
|
-
# ]
|
66
|
-
```
|
67
|
-
|
68
|
-
### Pluck at models
|
69
|
-
|
70
|
-
```rb
|
71
|
-
user = User.find_by(name: 'David')
|
72
|
-
user.deep_pluck(:name, :posts => :title)
|
73
|
-
# =>
|
74
|
-
# {
|
75
|
-
# 'name' => 'David' ,
|
76
|
-
# :posts => [
|
77
|
-
# {'title' => 'post1'},
|
78
|
-
# {'title' => 'post2'},
|
79
|
-
# ],
|
80
|
-
# }
|
81
|
-
```
|
82
|
-
|
83
|
-
### Compare with using `#as_json`
|
84
|
-
|
85
|
-
Assume the following relations:
|
86
|
-
|
87
|
-
> User has_many Posts.<br>
|
88
|
-
> Post has_many PostComments.<br>
|
89
|
-
> User has_one Contact.<br>
|
90
|
-
|
91
|
-
And the following #as_json example:
|
92
|
-
```rb
|
93
|
-
User.where(:name => %w(Pearl
|
94
|
-
:root => false,
|
95
|
-
:only => [:name, :email],
|
96
|
-
:include => {
|
97
|
-
'posts' => {
|
98
|
-
:only => :name,
|
99
|
-
:include => {
|
100
|
-
'post_comments' => {
|
101
|
-
:only => :comment,
|
102
|
-
},
|
103
|
-
},
|
104
|
-
},
|
105
|
-
'contact' => {
|
106
|
-
:only => :address,
|
107
|
-
},
|
108
|
-
},
|
109
|
-
})
|
110
|
-
|
111
|
-
```
|
112
|
-
It works as expected, but is not very DRY, repeat writing `include`, `posts`, `post_comments` so many times.
|
113
|
-
|
114
|
-
Not to mention the huge performace improvement by using #deep_pluck.
|
115
|
-
|
116
|
-
You could refactor the example with #deep_pluck:
|
117
|
-
```rb
|
118
|
-
User.where(:name => %w(Pearl
|
119
|
-
:name,
|
120
|
-
:email,
|
121
|
-
'posts' => [:name, 'post_comments' => :comment],
|
122
|
-
'contact' => :address,
|
123
|
-
)
|
124
|
-
```
|
125
|
-
|
126
|
-
### Better Performance
|
127
|
-
|
128
|
-
#deep_pluck return raw hash data without loading a bunch of records, so that faster than #as_json, or #select.
|
129
|
-
|
130
|
-
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`.
|
131
|
-
|
132
|
-
|
133
|
-
```rb
|
134
|
-
# Repeat 500 times
|
135
|
-
# User.includes(:posts).as_json(:only => :email, :include => {:posts => {:only => :title}})
|
136
|
-
# User.deep_pluck(:email, {'posts' => :title})
|
137
|
-
|
138
|
-
user system total real
|
139
|
-
as_json 1.740000 1.230000 2.970000 ( 3.231465)
|
140
|
-
deep_pluck 0.660000 0.030000 0.690000 ( 0.880018)
|
141
|
-
```
|
142
|
-
|
143
|
-
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`.
|
144
|
-
```rb
|
145
|
-
# Repeat 1 times
|
146
|
-
# User.select('account, email').map{|s| {'account' => s.account, 'email' => s.email}}
|
147
|
-
# User.select('account, email').as_json(:only => [:account, :email])
|
148
|
-
# User.deep_pluck(:account, :email)
|
149
|
-
|
150
|
-
user system total real
|
151
|
-
map 0.210000 0.000000 0.210000 ( 0.225421)
|
152
|
-
as_json 1.980000 0.060000 2.040000 ( 2.042205)
|
153
|
-
deep_pluck 0.040000 0.000000 0.040000 ( 0.051673)
|
154
|
-
```
|
155
|
-
|
156
|
-
|
157
|
-
## Development
|
158
|
-
|
159
|
-
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.
|
160
|
-
|
161
|
-
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).
|
162
|
-
|
163
|
-
## Contributing
|
164
|
-
|
165
|
-
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.
|
166
|
-
|
167
|
-
|
168
|
-
## License
|
169
|
-
|
170
|
-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
171
|
-
|
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
|
+
Support Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0.
|
12
|
+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'deep_pluck'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install deep_pluck
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Similar to #pluck method
|
33
|
+
|
34
|
+
```rb
|
35
|
+
User.deep_pluck(:id, :name)
|
36
|
+
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
37
|
+
# =>
|
38
|
+
# [
|
39
|
+
# {'id' => 1, 'name' => 'David'},
|
40
|
+
# {'id' => 2, 'name' => 'Jeremy'},
|
41
|
+
# ]
|
42
|
+
```
|
43
|
+
|
44
|
+
### Pluck attributes from nested associations
|
45
|
+
|
46
|
+
```rb
|
47
|
+
User.deep_pluck(:name, 'posts' => :title)
|
48
|
+
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
49
|
+
# SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
|
50
|
+
# =>
|
51
|
+
# [
|
52
|
+
# {
|
53
|
+
# 'name' => 'David' ,
|
54
|
+
# 'posts' => [
|
55
|
+
# {'title' => 'post1'},
|
56
|
+
# {'title' => 'post2'},
|
57
|
+
# ],
|
58
|
+
# },
|
59
|
+
# {
|
60
|
+
# 'name' => 'Jeremy',
|
61
|
+
# 'posts' => [
|
62
|
+
# {'title' => 'post3'},
|
63
|
+
# ],
|
64
|
+
# },
|
65
|
+
# ]
|
66
|
+
```
|
67
|
+
|
68
|
+
### Pluck at models
|
69
|
+
|
70
|
+
```rb
|
71
|
+
user = User.find_by(name: 'David')
|
72
|
+
user.deep_pluck(:name, :posts => :title)
|
73
|
+
# =>
|
74
|
+
# {
|
75
|
+
# 'name' => 'David' ,
|
76
|
+
# :posts => [
|
77
|
+
# {'title' => 'post1'},
|
78
|
+
# {'title' => 'post2'},
|
79
|
+
# ],
|
80
|
+
# }
|
81
|
+
```
|
82
|
+
|
83
|
+
### Compare with using `#as_json`
|
84
|
+
|
85
|
+
Assume the following relations:
|
86
|
+
|
87
|
+
> User has_many Posts.<br>
|
88
|
+
> Post has_many PostComments.<br>
|
89
|
+
> User has_one Contact.<br>
|
90
|
+
|
91
|
+
And the following #as_json example:
|
92
|
+
```rb
|
93
|
+
User.where(:name => %w(Pearl Doggy)).includes([{:posts => :post_comments}, :contact]).as_json({
|
94
|
+
:root => false,
|
95
|
+
:only => [:name, :email],
|
96
|
+
:include => {
|
97
|
+
'posts' => {
|
98
|
+
:only => :name,
|
99
|
+
:include => {
|
100
|
+
'post_comments' => {
|
101
|
+
:only => :comment,
|
102
|
+
},
|
103
|
+
},
|
104
|
+
},
|
105
|
+
'contact' => {
|
106
|
+
:only => :address,
|
107
|
+
},
|
108
|
+
},
|
109
|
+
})
|
110
|
+
|
111
|
+
```
|
112
|
+
It works as expected, but is not very DRY, repeat writing `include`, `posts`, `post_comments` so many times.
|
113
|
+
|
114
|
+
Not to mention the huge performace improvement by using #deep_pluck.
|
115
|
+
|
116
|
+
You could refactor the example with #deep_pluck:
|
117
|
+
```rb
|
118
|
+
User.where(:name => %w(Pearl Doggy)).deep_pluck(
|
119
|
+
:name,
|
120
|
+
:email,
|
121
|
+
'posts' => [:name, 'post_comments' => :comment],
|
122
|
+
'contact' => :address,
|
123
|
+
)
|
124
|
+
```
|
125
|
+
|
126
|
+
### Better Performance
|
127
|
+
|
128
|
+
#deep_pluck return raw hash data without loading a bunch of records, so that faster than #as_json, or #select.
|
129
|
+
|
130
|
+
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`.
|
131
|
+
|
132
|
+
|
133
|
+
```rb
|
134
|
+
# Repeat 500 times
|
135
|
+
# User.includes(:posts).as_json(:only => :email, :include => {:posts => {:only => :title}})
|
136
|
+
# User.deep_pluck(:email, {'posts' => :title})
|
137
|
+
|
138
|
+
user system total real
|
139
|
+
as_json 1.740000 1.230000 2.970000 ( 3.231465)
|
140
|
+
deep_pluck 0.660000 0.030000 0.690000 ( 0.880018)
|
141
|
+
```
|
142
|
+
|
143
|
+
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`.
|
144
|
+
```rb
|
145
|
+
# Repeat 1 times
|
146
|
+
# User.select('account, email').map{|s| {'account' => s.account, 'email' => s.email}}
|
147
|
+
# User.select('account, email').as_json(:only => [:account, :email])
|
148
|
+
# User.deep_pluck(:account, :email)
|
149
|
+
|
150
|
+
user system total real
|
151
|
+
map 0.210000 0.000000 0.210000 ( 0.225421)
|
152
|
+
as_json 1.980000 0.060000 2.040000 ( 2.042205)
|
153
|
+
deep_pluck 0.040000 0.000000 0.040000 ( 0.051673)
|
154
|
+
```
|
155
|
+
|
156
|
+
|
157
|
+
## Development
|
158
|
+
|
159
|
+
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.
|
160
|
+
|
161
|
+
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).
|
162
|
+
|
163
|
+
## Contributing
|
164
|
+
|
165
|
+
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.
|
166
|
+
|
167
|
+
|
168
|
+
## License
|
169
|
+
|
170
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
171
|
+
|