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.
@@ -1,21 +1,42 @@
1
- language: ruby
2
- rvm:
3
- - 2.2
4
- - 2.3
5
- sudo: required
6
- dist: trusty
7
- before_install:
8
- - gem install bundler
9
- - gem update --system
10
- - gem --version
11
- gemfile:
12
- - gemfiles/3.2.gemfile
13
- - gemfiles/4.2.gemfile
14
- - gemfiles/5.0.gemfile
15
- - gemfiles/5.1.gemfile
16
- addons:
17
- code_climate:
18
- repo_token: 29e69e7da6b55678f27b7de2444b6161a6c98e3c4b22e0e017ebab87ada18fd5
19
- after_success:
20
- - bundle exec codeclimate-test-reporter
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
@@ -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)
@@ -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/
@@ -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
- And DRY up your code when using #as_json.
12
-
13
- Works in Rails 3+.
14
-
15
-
16
- ## Installation
17
-
18
- Add this line to your application's Gemfile:
19
-
20
- ```ruby
21
- gem 'deep_pluck'
22
- ```
23
-
24
- And then execute:
25
-
26
- $ bundle
27
-
28
- Or install it yourself as:
29
-
30
- $ gem install deep_pluck
31
-
32
- ## Usage
33
-
34
- ### Similar to #pluck method
35
- ```rb
36
- User.deep_pluck(:id, :name)
37
- # SELECT `users`.`id`, `users`.`name` FROM `users`
38
- # => [{'id' => 1, 'name' => 'David'}, {'id' => 2, 'name' => 'Jeremy'}]
39
- ```
40
-
41
- ### Pluck deep into associations
42
- ```rb
43
- User.deep_pluck(:name, :posts => :title)
44
- # SELECT `users`.`id`, `users`.`name` FROM `users`
45
- # SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
46
- # => [
47
- # {'name' => 'David' , :posts => [{'title' => 'post1'}, {'title' => 'post2'}]},
48
- # {'name' => 'Jeremy', :posts => [{'title' => 'post3'}]}
49
- # ]
50
- ```
51
- ### Support plucking at active models
52
- ```rb
53
- user = User.find_by(name: 'David').deep_pluck(:name, :posts => :title)
54
- # => {'name' => 'David' , :posts => [{'title' => 'post1'}, {'title' => 'post2'}]}
55
- ```
56
-
57
- ### DRY up Rails/ActiveRecord includes when using as_json
58
-
59
- Assume the following relations:
60
-
61
- > User has_many Posts.<br>
62
- > Post has_many PostComments.<br>
63
- > User has_one Contact.<br>
64
-
65
- And the following #as_json example:
66
- ```rb
67
- User.where(:name => %w(Pearl Kathenrie)).includes([{:posts => :post_comments}, :contact]).as_json({
68
- :root => false,
69
- :only => [:name, :email],
70
- :include => {
71
- 'posts' => {
72
- :only => :name,
73
- :include => {
74
- 'post_comments' => {
75
- :only => :comment,
76
- },
77
- },
78
- },
79
- 'contact' => {
80
- :only => :address,
81
- },
82
- },
83
- })
84
-
85
- ```
86
- It works as expected, but is not very DRY, repeat writing `include`, `posts`, `post_comments` so many times.
87
-
88
- You could refactor it with #deep_pluck like:
89
- ```rb
90
- User.where(:name => %w(Pearl Kathenrie)).deep_pluck(
91
- :name,
92
- :email,
93
- 'posts' => [:name, 'post_comments' => :comment],
94
- 'contact' => :address,
95
- )
96
- ```
97
-
98
- ### Better Performance
99
-
100
- #deep_pluck return raw hash data without loading a bunch of records.
101
-
102
- In that faster than #as_json, or #select.
103
-
104
- 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`.
105
-
106
-
107
- ```rb
108
- # Repeat 500 times
109
- # User.includes(:posts).as_json(:only => :email, :include => {:posts => {:only => :title}})
110
- # User.deep_pluck(:email, {'posts' => :title})
111
-
112
- user system total real
113
- as_json 1.740000 1.230000 2.970000 ( 3.231465)
114
- deep_pluck 0.660000 0.030000 0.690000 ( 0.880018)
115
- ```
116
-
117
- 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`.
118
- ```rb
119
- # Repeat 1 times
120
- # User.select('account, email').map{|s| {'account' => s.account, 'email' => s.email}}
121
- # User.select('account, email').as_json(:only => [:account, :email])
122
- # User.deep_pluck(:account, :email)
123
-
124
- user system total real
125
- map 0.210000 0.000000 0.210000 ( 0.225421)
126
- as_json 1.980000 0.060000 2.040000 ( 2.042205)
127
- deep_pluck 0.040000 0.000000 0.040000 ( 0.051673)
128
- ```
129
-
130
-
131
- ## Development
132
-
133
- 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.
134
-
135
- 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).
136
-
137
- ## Contributing
138
-
139
- 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.
140
-
141
-
142
- ## License
143
-
144
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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
+