deep_pluck 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +54 -19
- data/.travis.yml +25 -13
- data/CHANGELOG.md +5 -1
- data/README.md +16 -16
- data/Rakefile +5 -5
- data/bin/console +3 -3
- data/deep_pluck.gemspec +27 -21
- data/gemfiles/3.2.gemfile +5 -8
- data/gemfiles/4.2.gemfile +5 -8
- data/gemfiles/5.0.gemfile +5 -8
- data/gemfiles/5.1.gemfile +5 -8
- data/gemfiles/5.2.gemfile +5 -8
- data/gemfiles/6.0.gemfile +11 -0
- data/lib/deep_pluck.rb +4 -5
- data/lib/deep_pluck/data_combiner.rb +1 -1
- data/lib/deep_pluck/model.rb +33 -10
- data/lib/deep_pluck/version.rb +1 -1
- metadata +20 -9
- data/lib/deep_pluck/preloaded_model.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7c31e52771b994ea336dcb1ccdae8b5973e84bea
|
4
|
+
data.tar.gz: 948fba4e1893f1dffa78f3d9cbbe822c79089f9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 056d01c1256f82927a0e4e9e24883b4222860d910ec21c4dfa4b2e56441ecb2232803389c9f5c66fcb05ba5896d5de6438b42a1f2a50d735c2bcfd5252b0ac58
|
7
|
+
data.tar.gz: ee892dd4fab06b89e15e9fcf5c67277fb6354344bc81ac1c62f18988b180ca3d3d2fdd4894d025b21b1e9ad43c94d816a132ff0f4be42196621846810039e108
|
data/.rubocop.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
AllCops:
|
2
2
|
DisabledByDefault: true
|
3
|
-
Exclude:
|
3
|
+
Exclude: []
|
4
4
|
|
5
5
|
#################### Lint ################################
|
6
6
|
|
@@ -22,7 +22,7 @@ Lint/AssignmentInCondition:
|
|
22
22
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition'
|
23
23
|
Enabled: true
|
24
24
|
|
25
|
-
|
25
|
+
Layout/BlockAlignment:
|
26
26
|
Description: 'Align block ends correctly.'
|
27
27
|
Enabled: true
|
28
28
|
|
@@ -30,7 +30,7 @@ Lint/CircularArgumentReference:
|
|
30
30
|
Description: "Don't refer to the keyword argument in the default value."
|
31
31
|
Enabled: true
|
32
32
|
|
33
|
-
|
33
|
+
Layout/ConditionPosition:
|
34
34
|
Description: >-
|
35
35
|
Checks for condition placed in a confusing position relative to
|
36
36
|
the keyword.
|
@@ -41,7 +41,7 @@ Lint/Debugger:
|
|
41
41
|
Description: 'Check for debugger calls.'
|
42
42
|
Enabled: true
|
43
43
|
|
44
|
-
|
44
|
+
Layout/DefEndAlignment:
|
45
45
|
Description: 'Align ends corresponding to defs correctly.'
|
46
46
|
Enabled: true
|
47
47
|
|
@@ -69,7 +69,7 @@ Lint/EmptyInterpolation:
|
|
69
69
|
Description: 'Checks for empty string interpolation.'
|
70
70
|
Enabled: true
|
71
71
|
|
72
|
-
|
72
|
+
Layout/EndAlignment:
|
73
73
|
Description: 'Align ends correctly.'
|
74
74
|
Enabled: true
|
75
75
|
|
@@ -148,7 +148,7 @@ Lint/UnderscorePrefixedVariableName:
|
|
148
148
|
Description: 'Do not use prefix `_` for a variable that is used.'
|
149
149
|
Enabled: true
|
150
150
|
|
151
|
-
Lint/
|
151
|
+
Lint/UnneededCopDisableDirective:
|
152
152
|
Description: >-
|
153
153
|
Checks for rubocop:disable comments that can be removed.
|
154
154
|
Note: this cop is not disabled when disabling all cops.
|
@@ -386,6 +386,7 @@ Layout/AlignHash:
|
|
386
386
|
Description: >-
|
387
387
|
Align the elements of a hash literal if they span more than
|
388
388
|
one line.
|
389
|
+
EnforcedHashRocketStyle: table
|
389
390
|
Enabled: true
|
390
391
|
|
391
392
|
Layout/AlignParameters:
|
@@ -787,7 +788,8 @@ Style/Next:
|
|
787
788
|
Style/NilComparison:
|
788
789
|
Description: 'Prefer x.nil? to x == nil.'
|
789
790
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#predicate-methods'
|
790
|
-
|
791
|
+
EnforcedStyle: comparison
|
792
|
+
Enabled: true
|
791
793
|
|
792
794
|
Style/NonNilCheck:
|
793
795
|
# With `IncludeSemanticChanges` set to `true`, this cop reports offenses for
|
@@ -799,7 +801,7 @@ Style/NonNilCheck:
|
|
799
801
|
Description: 'Checks for redundant nil checks.'
|
800
802
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-non-nil-checks'
|
801
803
|
IncludeSemanticChanges: true
|
802
|
-
Enabled:
|
804
|
+
Enabled: false
|
803
805
|
|
804
806
|
Style/Not:
|
805
807
|
Description: 'Use ! instead of not.'
|
@@ -903,7 +905,7 @@ Style/RegexpLiteral:
|
|
903
905
|
Description: 'Use / or %r around regular expressions.'
|
904
906
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#percent-r'
|
905
907
|
EnforcedStyle: slashes
|
906
|
-
AllowInnerSlashes:
|
908
|
+
AllowInnerSlashes: false
|
907
909
|
Enabled: true
|
908
910
|
|
909
911
|
Layout/RescueEnsureAlignment:
|
@@ -1013,7 +1015,8 @@ Layout/SpaceInsideBlockBraces:
|
|
1013
1015
|
Checks that block braces have or don't have surrounding space.
|
1014
1016
|
For blocks taking parameters, checks that the left brace has
|
1015
1017
|
or doesn't have trailing space.
|
1016
|
-
|
1018
|
+
SpaceBeforeBlockParameters: false
|
1019
|
+
Enabled: true
|
1017
1020
|
|
1018
1021
|
Layout/SpaceAroundBlockParameters:
|
1019
1022
|
Description: 'Checks the spacing inside and after block parameters pipes.'
|
@@ -1032,10 +1035,32 @@ Layout/SpaceAroundOperators:
|
|
1032
1035
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#spaces-operators'
|
1033
1036
|
Enabled: true
|
1034
1037
|
|
1035
|
-
Layout/
|
1036
|
-
Description: '
|
1037
|
-
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-spaces-braces'
|
1038
|
+
Layout/SpaceInsideArrayLiteralBrackets:
|
1039
|
+
Description: 'Checks the spacing inside array literal brackets.'
|
1038
1040
|
Enabled: true
|
1041
|
+
EnforcedStyle: no_space
|
1042
|
+
SupportedStyles:
|
1043
|
+
- space
|
1044
|
+
- no_space
|
1045
|
+
# 'compact' normally requires a space inside the brackets, with the exception
|
1046
|
+
# that successive left brackets or right brackets are collapsed together
|
1047
|
+
- compact
|
1048
|
+
EnforcedStyleForEmptyBrackets: no_space
|
1049
|
+
SupportedStylesForEmptyBrackets:
|
1050
|
+
- space
|
1051
|
+
- no_space
|
1052
|
+
|
1053
|
+
Layout/SpaceInsideReferenceBrackets:
|
1054
|
+
Description: 'Checks the spacing inside referential brackets.'
|
1055
|
+
Enabled: true
|
1056
|
+
EnforcedStyle: no_space
|
1057
|
+
SupportedStyles:
|
1058
|
+
- space
|
1059
|
+
- no_space
|
1060
|
+
EnforcedStyleForEmptyBrackets: no_space
|
1061
|
+
SupportedStylesForEmptyBrackets:
|
1062
|
+
- space
|
1063
|
+
- no_space
|
1039
1064
|
|
1040
1065
|
Layout/SpaceInsideHashLiteralBraces:
|
1041
1066
|
Description: "Use spaces inside hash literal braces - or don't."
|
@@ -1116,14 +1141,24 @@ Style/TrailingCommaInArguments:
|
|
1116
1141
|
EnforcedStyleForMultiline: comma
|
1117
1142
|
Enabled: true
|
1118
1143
|
|
1119
|
-
Style/
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1144
|
+
Style/TrailingCommaInArrayLiteral:
|
1145
|
+
Description: 'Checks for trailing comma in array literals.'
|
1146
|
+
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
1147
|
+
EnforcedStyleForMultiline: comma
|
1148
|
+
SupportedStylesForMultiline:
|
1149
|
+
- comma
|
1150
|
+
- consistent_comma
|
1151
|
+
- no_comma
|
1152
|
+
Enabled: true
|
1153
|
+
|
1154
|
+
Style/TrailingCommaInHashLiteral:
|
1155
|
+
Description: 'Checks for trailing comma in hash literals.'
|
1125
1156
|
StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
|
1126
1157
|
EnforcedStyleForMultiline: comma
|
1158
|
+
SupportedStylesForMultiline:
|
1159
|
+
- comma
|
1160
|
+
- consistent_comma
|
1161
|
+
- no_comma
|
1127
1162
|
Enabled: true
|
1128
1163
|
|
1129
1164
|
Layout/TrailingWhitespace:
|
data/.travis.yml
CHANGED
@@ -1,22 +1,34 @@
|
|
1
|
+
sudo: false
|
2
|
+
env:
|
3
|
+
global:
|
4
|
+
- CC_TEST_REPORTER_ID=29e69e7da6b55678f27b7de2444b6161a6c98e3c4b22e0e017ebab87ada18fd5
|
1
5
|
language: ruby
|
2
6
|
rvm:
|
3
7
|
- 2.2
|
4
|
-
- 2.
|
5
|
-
sudo: required
|
6
|
-
dist: trusty
|
7
|
-
before_install:
|
8
|
-
- gem install bundler
|
9
|
-
- gem update --system
|
10
|
-
- gem --version
|
8
|
+
- 2.6
|
11
9
|
gemfile:
|
12
10
|
- gemfiles/3.2.gemfile
|
13
11
|
- gemfiles/4.2.gemfile
|
14
12
|
- gemfiles/5.0.gemfile
|
15
13
|
- gemfiles/5.1.gemfile
|
16
14
|
- gemfiles/5.2.gemfile
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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,6 +1,10 @@
|
|
1
1
|
## Change Log
|
2
2
|
|
3
|
-
### [
|
3
|
+
### [v1.1.1](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...v1.1.1) 2018/07/08
|
4
|
+
- [#26](https://github.com/khiav223577/deep_pluck/pull/26) Fix: `id` may disappear when plucking at model instance (@khiav223577)
|
5
|
+
- [#25](https://github.com/khiav223577/deep_pluck/pull/25) Refactor - move models definition to separate files (@khiav223577)
|
6
|
+
- [#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)
|
4
8
|
- [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
|
5
9
|
- [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
|
6
10
|
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
|
9
9
|
Allow you to pluck deeply into nested associations without loading a bunch of records.
|
10
10
|
|
11
|
-
Support Rails 3.2, 4.2, 5.0, 5.1, 5.2.
|
11
|
+
Support Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0.
|
12
12
|
|
13
13
|
|
14
14
|
## Installation
|
@@ -33,10 +33,10 @@ Or install it yourself as:
|
|
33
33
|
|
34
34
|
```rb
|
35
35
|
User.deep_pluck(:id, :name)
|
36
|
-
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
37
|
-
# =>
|
36
|
+
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
37
|
+
# =>
|
38
38
|
# [
|
39
|
-
# {'id' => 1, 'name' => 'David'},
|
39
|
+
# {'id' => 1, 'name' => 'David'},
|
40
40
|
# {'id' => 2, 'name' => 'Jeremy'},
|
41
41
|
# ]
|
42
42
|
```
|
@@ -47,17 +47,17 @@ User.deep_pluck(:id, :name)
|
|
47
47
|
User.deep_pluck(:name, 'posts' => :title)
|
48
48
|
# SELECT `users`.`id`, `users`.`name` FROM `users`
|
49
49
|
# SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)
|
50
|
-
# =>
|
50
|
+
# =>
|
51
51
|
# [
|
52
52
|
# {
|
53
|
-
# 'name' => 'David' ,
|
53
|
+
# 'name' => 'David' ,
|
54
54
|
# 'posts' => [
|
55
|
-
# {'title' => 'post1'},
|
55
|
+
# {'title' => 'post1'},
|
56
56
|
# {'title' => 'post2'},
|
57
57
|
# ],
|
58
|
-
# },
|
58
|
+
# },
|
59
59
|
# {
|
60
|
-
# 'name' => 'Jeremy',
|
60
|
+
# 'name' => 'Jeremy',
|
61
61
|
# 'posts' => [
|
62
62
|
# {'title' => 'post3'},
|
63
63
|
# ],
|
@@ -72,9 +72,9 @@ user = User.find_by(name: 'David')
|
|
72
72
|
user.deep_pluck(:name, :posts => :title)
|
73
73
|
# =>
|
74
74
|
# {
|
75
|
-
# 'name' => 'David' ,
|
75
|
+
# 'name' => 'David' ,
|
76
76
|
# :posts => [
|
77
|
-
# {'title' => 'post1'},
|
77
|
+
# {'title' => 'post1'},
|
78
78
|
# {'title' => 'post2'},
|
79
79
|
# ],
|
80
80
|
# }
|
@@ -92,10 +92,10 @@ And the following #as_json example:
|
|
92
92
|
```rb
|
93
93
|
User.where(:name => %w(Pearl Kathenrie)).includes([{:posts => :post_comments}, :contact]).as_json({
|
94
94
|
:root => false,
|
95
|
-
:only => [:name, :email],
|
95
|
+
:only => [:name, :email],
|
96
96
|
:include => {
|
97
97
|
'posts' => {
|
98
|
-
:only => :name,
|
98
|
+
:only => :name,
|
99
99
|
:include => {
|
100
100
|
'post_comments' => {
|
101
101
|
:only => :comment,
|
@@ -116,9 +116,9 @@ Not to mention the huge performace improvement by using #deep_pluck.
|
|
116
116
|
You could refactor the example with #deep_pluck:
|
117
117
|
```rb
|
118
118
|
User.where(:name => %w(Pearl Kathenrie)).deep_pluck(
|
119
|
-
:name,
|
120
|
-
:email,
|
121
|
-
'posts' => [:name, 'post_comments' => :comment],
|
119
|
+
:name,
|
120
|
+
:email,
|
121
|
+
'posts' => [:name, 'post_comments' => :comment],
|
122
122
|
'contact' => :address,
|
123
123
|
)
|
124
124
|
```
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
3
|
|
4
4
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs <<
|
6
|
-
t.libs <<
|
5
|
+
t.libs << 'test'
|
6
|
+
t.libs << 'lib'
|
7
7
|
t.test_files = FileList['test/**/*_test.rb']
|
8
8
|
end
|
9
9
|
|
10
|
-
task :
|
10
|
+
task default: :test
|
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'deep_pluck'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "deep_pluck"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/deep_pluck.gemspec
CHANGED
@@ -4,35 +4,41 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'deep_pluck/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'deep_pluck'
|
8
8
|
spec.version = DeepPluck::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['khiav reoy']
|
10
|
+
spec.email = ['mrtmrt15xn@yahoo.com.tw']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
12
|
+
spec.summary = 'Use deep_pluck as a shortcut to select one or more attributes and include associated models without loading a bunch of records.'
|
13
|
+
spec.description = 'Use deep_pluck as a shortcut to select one or more attributes and include associated models without loading a bunch of records. And DRY up your code when using #as_json.'
|
14
|
+
spec.homepage = 'https://github.com/khiav223577/deep_pluck'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
18
|
# delete this section to allow pushing this gem to any host.
|
19
|
-
#if spec.respond_to?(:metadata)
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
20
|
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
-
#else
|
21
|
+
# else
|
22
22
|
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
-
#end
|
23
|
+
# end
|
24
24
|
|
25
|
-
spec.files = `git ls-files -z`.split("\x0").reject
|
26
|
-
spec.bindir =
|
27
|
-
spec.executables = spec.files.grep(%r{^exe/})
|
28
|
-
spec.require_paths = [
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject{|f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}){|f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
spec.metadata = {
|
30
|
+
'homepage_uri' => 'https://github.com/khiav223577/deep_pluck',
|
31
|
+
'changelog_uri' => 'https://github.com/khiav223577/deep_pluck/blob/master/CHANGELOG.md',
|
32
|
+
'source_code_uri' => 'https://github.com/khiav223577/deep_pluck',
|
33
|
+
'documentation_uri' => 'https://www.rubydoc.info/gems/deep_pluck',
|
34
|
+
'bug_tracker_uri' => 'https://github.com/khiav223577/deep_pluck/issues',
|
35
|
+
}
|
29
36
|
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
34
|
-
|
35
|
-
spec.add_dependency "activerecord", ">= 3"
|
36
|
-
spec.add_dependency "pluck_all", ">= 1.2.3"
|
37
|
+
spec.add_development_dependency 'bundler', '>= 1.17', '< 3.x'
|
38
|
+
spec.add_development_dependency 'rake', '~> 12.0'
|
39
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
40
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
37
41
|
|
42
|
+
spec.add_dependency 'activerecord', '>= 3'
|
43
|
+
spec.add_dependency 'pluck_all', '>= 1.2.3'
|
38
44
|
end
|
data/gemfiles/3.2.gemfile
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "activerecord", "~> 3.2.0"
|
6
|
-
gem "pluck_all", "~> 1.2.2"
|
3
|
+
gem 'activerecord', '~> 3.2.0'
|
4
|
+
gem 'pluck_all', '~> 1.2.2'
|
7
5
|
|
8
6
|
group :test do
|
9
|
-
gem
|
10
|
-
gem
|
7
|
+
gem 'simplecov'
|
8
|
+
gem 'sqlite3', '~> 1.3.6'
|
11
9
|
end
|
12
10
|
|
13
|
-
gemspec :
|
14
|
-
|
11
|
+
gemspec path: '../'
|
data/gemfiles/4.2.gemfile
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "activerecord", "~> 4.2.0"
|
6
|
-
gem "pluck_all", "~> 1.2.2"
|
3
|
+
gem 'activerecord', '~> 4.2.0'
|
4
|
+
gem 'pluck_all', '~> 1.2.2'
|
7
5
|
|
8
6
|
group :test do
|
9
|
-
gem
|
10
|
-
gem
|
7
|
+
gem 'simplecov'
|
8
|
+
gem 'sqlite3', '~> 1.3.6'
|
11
9
|
end
|
12
10
|
|
13
|
-
gemspec :
|
14
|
-
|
11
|
+
gemspec path: '../'
|
data/gemfiles/5.0.gemfile
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "activerecord", "~> 5.0.0"
|
6
|
-
gem "pluck_all", "~> 1.2.2"
|
3
|
+
gem 'activerecord', '~> 5.0.0'
|
4
|
+
gem 'pluck_all', '~> 1.2.2'
|
7
5
|
|
8
6
|
group :test do
|
9
|
-
gem
|
10
|
-
gem
|
7
|
+
gem 'simplecov'
|
8
|
+
gem 'sqlite3', '~> 1.3.6'
|
11
9
|
end
|
12
10
|
|
13
|
-
gemspec :
|
14
|
-
|
11
|
+
gemspec path: '../'
|
data/gemfiles/5.1.gemfile
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "activerecord", "~> 5.1.0"
|
6
|
-
gem "pluck_all", "~> 1.2.2"
|
3
|
+
gem 'activerecord', '~> 5.1.0'
|
4
|
+
gem 'pluck_all', '~> 1.2.2'
|
7
5
|
|
8
6
|
group :test do
|
9
|
-
gem
|
10
|
-
gem
|
7
|
+
gem 'simplecov'
|
8
|
+
gem 'sqlite3', '~> 1.3.6'
|
11
9
|
end
|
12
10
|
|
13
|
-
gemspec :
|
14
|
-
|
11
|
+
gemspec path: '../'
|
data/gemfiles/5.2.gemfile
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
gem "activerecord", "~> 5.2.0"
|
6
|
-
gem "pluck_all", "~> 1.2.2"
|
3
|
+
gem 'activerecord', '~> 5.2.0'
|
4
|
+
gem 'pluck_all', '~> 1.2.2'
|
7
5
|
|
8
6
|
group :test do
|
9
|
-
gem
|
10
|
-
gem
|
7
|
+
gem 'simplecov'
|
8
|
+
gem 'sqlite3', '~> 1.3.6'
|
11
9
|
end
|
12
10
|
|
13
|
-
gemspec :
|
14
|
-
|
11
|
+
gemspec path: '../'
|
data/lib/deep_pluck.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'deep_pluck/version'
|
2
2
|
require 'deep_pluck/model'
|
3
3
|
require 'active_record'
|
4
4
|
require 'pluck_all'
|
@@ -11,13 +11,12 @@ end
|
|
11
11
|
|
12
12
|
class ActiveRecord::Base
|
13
13
|
def self.deep_pluck(*args)
|
14
|
-
|
14
|
+
where('').deep_pluck(*args)
|
15
15
|
end
|
16
16
|
|
17
17
|
def deep_pluck(*args)
|
18
|
-
hash_args, other_args = args.partition
|
19
|
-
|
20
|
-
model = DeepPluck::Model.new(self.class.where(id: id), preloaded_model: preloaded_model)
|
18
|
+
hash_args, other_args = args.partition{|s| s.is_a?(Hash) }
|
19
|
+
model = DeepPluck::Model.new(self, need_columns: other_args)
|
21
20
|
model.add(*hash_args) if hash_args.any?
|
22
21
|
return model.load_all.first
|
23
22
|
end
|
@@ -12,7 +12,7 @@ module DeepPluck
|
|
12
12
|
private
|
13
13
|
|
14
14
|
def make_data_hash(collection, parent, primary_key, column_name)
|
15
|
-
return parent.map{|s| [s[primary_key], s]}.to_h if !collection
|
15
|
+
return parent.map{|s| [s[primary_key], s] }.to_h if !collection
|
16
16
|
hash = {}
|
17
17
|
parent.each do |model_hash|
|
18
18
|
key = model_hash[primary_key]
|
data/lib/deep_pluck/model.rb
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
-
require 'deep_pluck/preloaded_model'
|
2
1
|
require 'deep_pluck/data_combiner'
|
2
|
+
|
3
3
|
module DeepPluck
|
4
4
|
class Model
|
5
5
|
# ----------------------------------------------------------------
|
6
6
|
# ● Initialize
|
7
7
|
# ----------------------------------------------------------------
|
8
|
-
def initialize(relation, parent_association_key = nil, parent_model = nil,
|
9
|
-
|
10
|
-
|
8
|
+
def initialize(relation, parent_association_key = nil, parent_model = nil, need_columns: [])
|
9
|
+
if relation.is_a?(ActiveRecord::Base)
|
10
|
+
@model = relation
|
11
|
+
@relation = nil
|
12
|
+
@klass = @model.class
|
13
|
+
else
|
14
|
+
@model = nil
|
15
|
+
@relation = relation
|
16
|
+
@klass = @relation.klass
|
17
|
+
end
|
18
|
+
|
11
19
|
@parent_association_key = parent_association_key
|
12
20
|
@parent_model = parent_model
|
13
|
-
@need_columns =
|
21
|
+
@need_columns = need_columns
|
14
22
|
@associations = {}
|
15
23
|
end
|
16
24
|
|
@@ -18,10 +26,10 @@ module DeepPluck
|
|
18
26
|
# ● Reader
|
19
27
|
# ----------------------------------------------------------------
|
20
28
|
def get_reflect(association_key)
|
21
|
-
@
|
29
|
+
@klass.reflect_on_association(association_key.to_sym) || # add to_sym since rails 3 only support symbol
|
22
30
|
fail(ActiveRecord::ConfigurationError, "ActiveRecord::ConfigurationError: Association named \
|
23
|
-
'#{association_key}' was not found on #{@
|
24
|
-
|
31
|
+
'#{association_key}' was not found on #{@klass.name}; perhaps you misspelled it?"
|
32
|
+
)
|
25
33
|
end
|
26
34
|
|
27
35
|
def with_conditions(reflect, relation)
|
@@ -92,7 +100,7 @@ module DeepPluck
|
|
92
100
|
def do_query(parent, reflect, relation)
|
93
101
|
parent_key = get_foreign_key(reflect)
|
94
102
|
relation_key = get_foreign_key(reflect, reverse: true, with_table_name: true)
|
95
|
-
ids = parent.map{|s| s[parent_key]}
|
103
|
+
ids = parent.map{|s| s[parent_key] }
|
96
104
|
ids.uniq!
|
97
105
|
ids.compact!
|
98
106
|
relation = with_conditions(reflect, relation)
|
@@ -129,13 +137,28 @@ module DeepPluck
|
|
129
137
|
return [*prev_need_columns, *next_need_columns, *@need_columns].uniq(&Helper::TO_KEY_PROC)
|
130
138
|
end
|
131
139
|
|
140
|
+
def pluck_values(columns)
|
141
|
+
includes_values = @relation.includes_values
|
142
|
+
@relation.includes_values = []
|
143
|
+
|
144
|
+
result = @relation.pluck_all(*columns)
|
145
|
+
|
146
|
+
@relation.includes_values = includes_values
|
147
|
+
return result
|
148
|
+
end
|
149
|
+
|
150
|
+
def loaded_models
|
151
|
+
return [@model] if @model
|
152
|
+
return @relation if @relation.loaded
|
153
|
+
end
|
154
|
+
|
132
155
|
public
|
133
156
|
|
134
157
|
def load_data
|
135
158
|
columns = get_query_columns
|
136
159
|
key_columns = columns.map(&Helper::TO_KEY_PROC)
|
137
160
|
@relation = yield(@relation) if block_given?
|
138
|
-
@data =
|
161
|
+
@data = loaded_models ? loaded_models.as_json(root: false, only: key_columns) : pluck_values(columns)
|
139
162
|
if @data.size != 0
|
140
163
|
# for delete_extra_column_data!
|
141
164
|
@extra_columns = key_columns - @need_columns.map(&Helper::TO_KEY_PROC)
|
data/lib/deep_pluck/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deep_pluck
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
- - "<"
|
18
21
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
22
|
+
version: 3.x
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.17'
|
30
|
+
- - "<"
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
32
|
+
version: 3.x
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,15 +125,20 @@ files:
|
|
119
125
|
- gemfiles/5.0.gemfile
|
120
126
|
- gemfiles/5.1.gemfile
|
121
127
|
- gemfiles/5.2.gemfile
|
128
|
+
- gemfiles/6.0.gemfile
|
122
129
|
- lib/deep_pluck.rb
|
123
130
|
- lib/deep_pluck/data_combiner.rb
|
124
131
|
- lib/deep_pluck/model.rb
|
125
|
-
- lib/deep_pluck/preloaded_model.rb
|
126
132
|
- lib/deep_pluck/version.rb
|
127
133
|
homepage: https://github.com/khiav223577/deep_pluck
|
128
134
|
licenses:
|
129
135
|
- MIT
|
130
|
-
metadata:
|
136
|
+
metadata:
|
137
|
+
homepage_uri: https://github.com/khiav223577/deep_pluck
|
138
|
+
changelog_uri: https://github.com/khiav223577/deep_pluck/blob/master/CHANGELOG.md
|
139
|
+
source_code_uri: https://github.com/khiav223577/deep_pluck
|
140
|
+
documentation_uri: https://www.rubydoc.info/gems/deep_pluck
|
141
|
+
bug_tracker_uri: https://github.com/khiav223577/deep_pluck/issues
|
131
142
|
post_install_message:
|
132
143
|
rdoc_options: []
|
133
144
|
require_paths:
|
@@ -144,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
155
|
version: '0'
|
145
156
|
requirements: []
|
146
157
|
rubyforge_project:
|
147
|
-
rubygems_version: 2.
|
158
|
+
rubygems_version: 2.6.14
|
148
159
|
signing_key:
|
149
160
|
specification_version: 4
|
150
161
|
summary: Use deep_pluck as a shortcut to select one or more attributes and include
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module DeepPluck
|
2
|
-
class PreloadedModel
|
3
|
-
attr_reader :need_columns
|
4
|
-
|
5
|
-
def initialize(active_model, need_columns)
|
6
|
-
@active_model = active_model
|
7
|
-
@need_columns = need_columns
|
8
|
-
end
|
9
|
-
|
10
|
-
def get_hash_data(extra_columns)
|
11
|
-
@active_model.as_json(root: false, only: @need_columns + extra_columns)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|