deep_pluck 1.1.2 → 1.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,10 +1,32 @@
1
1
  ## Change Log
2
2
 
3
+ ### [v1.1.6](https://github.com/khiav223577/deep_pluck/compare/v1.1.5...v1.1.6) 2021/02/09
4
+ - [#43](https://github.com/khiav223577/deep_pluck/pull/43) Fix has_and_belongs_to_many issues when it does not specify "through" table (@khiav223577)
5
+ - [#42](https://github.com/khiav223577/deep_pluck/pull/42) Migrating from Travis CI to GitHub Actions (@khiav223577)
6
+
7
+ ### [v1.1.5](https://github.com/khiav223577/deep_pluck/compare/v1.1.4...v1.1.5) 2021/01/10
8
+ - [#39](https://github.com/khiav223577/deep_pluck/pull/39) Compatibility with Rails 6.1.1 (@klausbadelt)
9
+ - [#40](https://github.com/khiav223577/deep_pluck/pull/40) Exclude tests from coverage (@klausbadelt)
10
+
11
+ ### [v1.1.4](https://github.com/khiav223577/deep_pluck/compare/v1.1.3...v1.1.4) 2020/01/13
12
+ - [#36](https://github.com/khiav223577/deep_pluck/pull/36) A workaround to fix mismatched association named (@khiav223577)
13
+ - [#35](https://github.com/khiav223577/deep_pluck/pull/35) Support Ruby 2.7 (@khiav223577)
14
+
15
+ ### [v1.1.3](https://github.com/khiav223577/deep_pluck/compare/v1.1.2...v1.1.3) 2019/12/17
16
+ - [#34](https://github.com/khiav223577/deep_pluck/pull/34) Support for plucking directly on a has_one through association (@khiav223577)
17
+
18
+ ### [v1.1.2](https://github.com/khiav223577/deep_pluck/compare/v1.1.1...v1.1.2) 2019/09/25
19
+ - [#32](https://github.com/khiav223577/deep_pluck/pull/32) Remove unneeded `PreloadedModel` (@khiav223577)
20
+ - [#31](https://github.com/khiav223577/deep_pluck/pull/31) Support Rails 6.0 (@khiav223577)
21
+ - [#30](https://github.com/khiav223577/deep_pluck/pull/30) Lock sqlite3 version to 1.3.x (@khiav223577)
22
+ - [#28](https://github.com/khiav223577/deep_pluck/pull/28) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
23
+ - [#27](https://github.com/khiav223577/deep_pluck/pull/27) Remove deprecated codeclimate-test-reporter gem and update travis config (@khiav223577)
24
+
3
25
  ### [v1.1.1](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...v1.1.1) 2018/07/08
4
26
  - [#26](https://github.com/khiav223577/deep_pluck/pull/26) Fix: `id` may disappear when plucking at model instance (@khiav223577)
5
27
  - [#25](https://github.com/khiav223577/deep_pluck/pull/25) Refactor - move models definition to separate files (@khiav223577)
6
28
  - [#24](https://github.com/khiav223577/deep_pluck/pull/24) test Rails 5.2 (@khiav223577)
7
- - [#23](https://github.com/khiav223577/deep_pluck/pull/23) should test both 5.0.x and 5.1.x (@khiav223577)
29
+ - [#23](https://github.com/khiav223577/deep_pluck/pull/23) It should test both 5.0.x and 5.1.x (@khiav223577)
8
30
  - [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
9
31
  - [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
10
32
 
@@ -12,26 +34,26 @@
12
34
  - [#19](https://github.com/khiav223577/deep_pluck/pull/19) Support deep_pluck at active model (@khiav223577)
13
35
  - [#18](https://github.com/khiav223577/deep_pluck/pull/18) Add rubocop and Improve code quality (@khiav223577)
14
36
 
15
- ### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.0...v1.0.3) 2017/06/30
16
- - [#15](https://github.com/khiav223577/deep_pluck/pull/15) test deep_pluck in rails 5.1.x (@khiav223577)
37
+ ### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.2...v1.0.3) 2017/06/30
38
+ - [#15](https://github.com/khiav223577/deep_pluck/pull/15) Test deep_pluck in rails 5.1.x (@khiav223577)
17
39
  - [#14](https://github.com/khiav223577/deep_pluck/pull/14) Handle polymorphic associations correctly. (@Bogadon)
18
40
 
19
41
  ### [v1.0.0](https://github.com/khiav223577/deep_pluck/compare/v0.1.4...v1.0.0) 2017/03/28
20
- - [#12](https://github.com/khiav223577/deep_pluck/pull/12) reduce cyclomatic complexity in model.rb (@khiav223577)
42
+ - [#12](https://github.com/khiav223577/deep_pluck/pull/12) Reduce cyclomatic complexity in model.rb (@khiav223577)
21
43
 
22
44
  ### [v0.1.4](https://github.com/khiav223577/deep_pluck/compare/v0.1.3...v0.1.4) 2017/03/27
23
45
  - [#11](https://github.com/khiav223577/deep_pluck/pull/11) Fix conditional associations (@khiav223577)
24
46
 
25
47
  ### [v0.1.3](https://github.com/khiav223577/deep_pluck/compare/v0.1.2...v0.1.3) 2017/03/20
26
- - [#10](https://github.com/khiav223577/deep_pluck/pull/10) fix custom foreign_key, custom primary_key issues (@khiav223577)
27
- - [#9](https://github.com/khiav223577/deep_pluck/pull/9) fix has_and_belongs_to_many (@khiav223577)
48
+ - [#10](https://github.com/khiav223577/deep_pluck/pull/10) Fix custom foreign_key, custom primary_key issues (@khiav223577)
49
+ - [#9](https://github.com/khiav223577/deep_pluck/pull/9) Fix has_and_belongs_to_many (@khiav223577)
28
50
 
29
51
  ### [v0.1.2](https://github.com/khiav223577/deep_pluck/compare/v0.1.1...v0.1.2) 2017/03/17
30
- - [#8](https://github.com/khiav223577/deep_pluck/pull/8) fix some need columns is missing (@khiav223577)
31
- - [#7](https://github.com/khiav223577/deep_pluck/pull/7) raise error message when association not found (@khiav223577)
52
+ - [#8](https://github.com/khiav223577/deep_pluck/pull/8) Fix that some need columns are missing (@khiav223577)
53
+ - [#7](https://github.com/khiav223577/deep_pluck/pull/7) Raise error message when association not found (@khiav223577)
32
54
 
33
55
  ### [v0.1.1](https://github.com/khiav223577/deep_pluck/compare/v0.1.0...v0.1.1) 2017/03/16
34
- - [#6](https://github.com/khiav223577/deep_pluck/pull/6) fix deep_pluck with #joins (@khiav223577)
56
+ - [#6](https://github.com/khiav223577/deep_pluck/pull/6) Fix deep_pluck with #joins (@khiav223577)
35
57
 
36
58
  ### [v0.1.0](https://github.com/khiav223577/deep_pluck/compare/v0.0.4...v0.1.0) 2017/03/15
37
59
  - [#5](https://github.com/khiav223577/deep_pluck/pull/5) Support pluck many-to-many associations (@khiav223577)
@@ -40,8 +62,10 @@
40
62
  - [#4](https://github.com/khiav223577/deep_pluck/pull/4) Fix use deep_pluck on NullRelation will raise exception and prevent unneeded query. (@khiav223577)
41
63
 
42
64
  ### [v0.0.3](https://github.com/khiav223577/deep_pluck/compare/v0.0.2...v0.0.3) 2017/03/06
43
- - [#3](https://github.com/khiav223577/deep_pluck/pull/3) support more than two level (@khiav223577)
65
+ - [#3](https://github.com/khiav223577/deep_pluck/pull/3) Support more than two level (@khiav223577)
44
66
 
45
- ### v0.0.2 2017/03/06
67
+ ### [v0.0.2](https://github.com/khiav223577/deep_pluck/compare/v0.0.1...v0.0.2) 2017/03/06
46
68
  - [#2](https://github.com/khiav223577/deep_pluck/pull/2) The result of has_one association should not be array (@khiav223577)
47
- - [#1](https://github.com/khiav223577/deep_pluck/pull/1) deep pluck (@khiav223577)
69
+
70
+ ### v0.0.1 2017/03/04
71
+ - [#1](https://github.com/khiav223577/deep_pluck/pull/1) Implement 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,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
- 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 Kathenrie)).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 Kathenrie)).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
-
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://github.com/khiav223577/deep_pluck/workflows/Ruby/badge.svg)](https://github.com/khiav223577/deep_pluck/actions)
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
+