deep_pluck 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 005fdc992119dbb5eceeb1f62ad7ab4c884af86cf556dacba454eb389f45ec7b
4
- data.tar.gz: bd2e0b8915f71a956238bf2b2a6cd32243c5bf7c18374fd5b0111c20f5550100
3
+ metadata.gz: 2ccf8d7148c3cc85af9e85d04417e445a7f67bbe8c6d7feabfef12fbce6207a9
4
+ data.tar.gz: 2999ce4ea1a1224f9618f56014926887352bae0e3a6724073d518ddc64c99e9c
5
5
  SHA512:
6
- metadata.gz: 3fc6109cac1f4afbd414d63aa5b92fda46ec48fde691d5456c8e6d71582e8bd01dbffdf4f1244e2c825a7790f3278113ceafecc6166fcd8aea80fc84e2c66fb7
7
- data.tar.gz: 04c2c14548aaee28873a2167cd88ab86cab91143bc315b717d03446e10357c22247d5db851c8b577aedb3ef6961b5a4a411c9f83eb172b146b3f606ed9d89220
6
+ metadata.gz: e92f37c683aa8dab7c73555bb03e35ec3791a7eeebcba494af915efb3490e946785995b0a21cfc5f7ee5acd90b5479b99963dc05b327266d89ad1ccce5622719
7
+ data.tar.gz: 986d208e8861aae0536584add5703813c8a1b3572af9ed82705a0904536ff28b6f69041c84c6ad5e13043a14b9dcfbe53853250bd8d4969857d566cf19afb5a6
@@ -34,6 +34,7 @@ jobs:
34
34
  - 6.0.gemfile
35
35
  - 6.1.gemfile
36
36
  - 7.0.gemfile
37
+ - 7.1.gemfile
37
38
  exclude:
38
39
  - gemfile: 3.2.gemfile
39
40
  ruby: 2.6
@@ -69,6 +70,10 @@ jobs:
69
70
  ruby: 2.3
70
71
  - gemfile: 7.0.gemfile
71
72
  ruby: 2.6
73
+ - gemfile: 7.1.gemfile
74
+ ruby: 2.3
75
+ - gemfile: 7.1.gemfile
76
+ ruby: 2.6
72
77
  env:
73
78
  BUNDLE_GEMFILE: "gemfiles/${{ matrix.gemfile }}"
74
79
 
@@ -83,7 +88,7 @@ jobs:
83
88
  - name: Run tests
84
89
  run: bundle exec rake
85
90
  - name: Publish code coverage
86
- if: ${{ success() }}
91
+ if: ${{ success() && env.CC_TEST_REPORTER_ID }}
87
92
  uses: paambaati/codeclimate-action@v2.7.5
88
93
  env:
89
94
  CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
data/CHANGELOG.md CHANGED
@@ -1,78 +1,94 @@
1
- ## Change Log
2
-
3
- ### [v1.2.0](https://github.com/khiav223577/deep_pluck/compare/v1.1.7...v1.2.0) 2021/06/10
4
- - [#50](https://github.com/khiav223577/deep_pluck/pull/50) Support `globalize` gem (@khiav223577)
5
-
6
- ### [v1.1.7](https://github.com/khiav223577/deep_pluck/compare/v1.1.6...v1.1.7) 2021/04/10
7
- - [#48](https://github.com/khiav223577/deep_pluck/pull/48) Fix: joins incorrect table when doing inverse lookup for HABTM associations with custom name (@khiav223577)
8
- - [#45](https://github.com/khiav223577/deep_pluck/pull/45) Do not publish code coverage for PRs from forks (@moon-moon-husky)
9
-
10
- ### [v1.1.6](https://github.com/khiav223577/deep_pluck/compare/v1.1.5...v1.1.6) 2021/02/09
11
- - [#43](https://github.com/khiav223577/deep_pluck/pull/43) Fix has_and_belongs_to_many issues when it does not specify "through" table (@khiav223577)
12
- - [#42](https://github.com/khiav223577/deep_pluck/pull/42) Migrating from Travis CI to GitHub Actions (@khiav223577)
13
-
14
- ### [v1.1.5](https://github.com/khiav223577/deep_pluck/compare/v1.1.4...v1.1.5) 2021/01/10
15
- - [#39](https://github.com/khiav223577/deep_pluck/pull/39) Compatibility with Rails 6.1.1 (@klausbadelt)
16
- - [#40](https://github.com/khiav223577/deep_pluck/pull/40) Exclude tests from coverage (@klausbadelt)
17
-
18
- ### [v1.1.4](https://github.com/khiav223577/deep_pluck/compare/v1.1.3...v1.1.4) 2020/01/13
19
- - [#36](https://github.com/khiav223577/deep_pluck/pull/36) A workaround to fix mismatched association named (@khiav223577)
20
- - [#35](https://github.com/khiav223577/deep_pluck/pull/35) Support Ruby 2.7 (@khiav223577)
21
-
22
- ### [v1.1.3](https://github.com/khiav223577/deep_pluck/compare/v1.1.2...v1.1.3) 2019/12/17
23
- - [#34](https://github.com/khiav223577/deep_pluck/pull/34) Support for plucking directly on a has_one through association (@khiav223577)
24
-
25
- ### [v1.1.2](https://github.com/khiav223577/deep_pluck/compare/v1.1.1...v1.1.2) 2019/09/25
26
- - [#32](https://github.com/khiav223577/deep_pluck/pull/32) Remove unneeded `PreloadedModel` (@khiav223577)
27
- - [#31](https://github.com/khiav223577/deep_pluck/pull/31) Support Rails 6.0 (@khiav223577)
28
- - [#30](https://github.com/khiav223577/deep_pluck/pull/30) Lock sqlite3 version to 1.3.x (@khiav223577)
29
- - [#28](https://github.com/khiav223577/deep_pluck/pull/28) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
30
- - [#27](https://github.com/khiav223577/deep_pluck/pull/27) Remove deprecated codeclimate-test-reporter gem and update travis config (@khiav223577)
31
-
32
- ### [v1.1.1](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...v1.1.1) 2018/07/08
33
- - [#26](https://github.com/khiav223577/deep_pluck/pull/26) Fix: `id` may disappear when plucking at model instance (@khiav223577)
34
- - [#25](https://github.com/khiav223577/deep_pluck/pull/25) Refactor - move models definition to separate files (@khiav223577)
35
- - [#24](https://github.com/khiav223577/deep_pluck/pull/24) test Rails 5.2 (@khiav223577)
36
- - [#23](https://github.com/khiav223577/deep_pluck/pull/23) It should test both 5.0.x and 5.1.x (@khiav223577)
37
- - [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
38
- - [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
39
-
40
- ### [v1.1.0](https://github.com/khiav223577/deep_pluck/compare/v1.0.3...v1.1.0) 2018/02/15
41
- - [#19](https://github.com/khiav223577/deep_pluck/pull/19) Support deep_pluck at active model (@khiav223577)
42
- - [#18](https://github.com/khiav223577/deep_pluck/pull/18) Add rubocop and Improve code quality (@khiav223577)
43
-
44
- ### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.2...v1.0.3) 2017/06/30
45
- - [#15](https://github.com/khiav223577/deep_pluck/pull/15) Test deep_pluck in rails 5.1.x (@khiav223577)
46
- - [#14](https://github.com/khiav223577/deep_pluck/pull/14) Handle polymorphic associations correctly. (@Bogadon)
47
-
48
- ### [v1.0.0](https://github.com/khiav223577/deep_pluck/compare/v0.1.4...v1.0.0) 2017/03/28
49
- - [#12](https://github.com/khiav223577/deep_pluck/pull/12) Reduce cyclomatic complexity in model.rb (@khiav223577)
50
-
51
- ### [v0.1.4](https://github.com/khiav223577/deep_pluck/compare/v0.1.3...v0.1.4) 2017/03/27
52
- - [#11](https://github.com/khiav223577/deep_pluck/pull/11) Fix conditional associations (@khiav223577)
53
-
54
- ### [v0.1.3](https://github.com/khiav223577/deep_pluck/compare/v0.1.2...v0.1.3) 2017/03/20
55
- - [#10](https://github.com/khiav223577/deep_pluck/pull/10) Fix custom foreign_key, custom primary_key issues (@khiav223577)
56
- - [#9](https://github.com/khiav223577/deep_pluck/pull/9) Fix has_and_belongs_to_many (@khiav223577)
57
-
58
- ### [v0.1.2](https://github.com/khiav223577/deep_pluck/compare/v0.1.1...v0.1.2) 2017/03/17
59
- - [#8](https://github.com/khiav223577/deep_pluck/pull/8) Fix that some need columns are missing (@khiav223577)
60
- - [#7](https://github.com/khiav223577/deep_pluck/pull/7) Raise error message when association not found (@khiav223577)
61
-
62
- ### [v0.1.1](https://github.com/khiav223577/deep_pluck/compare/v0.1.0...v0.1.1) 2017/03/16
63
- - [#6](https://github.com/khiav223577/deep_pluck/pull/6) Fix deep_pluck with #joins (@khiav223577)
64
-
65
- ### [v0.1.0](https://github.com/khiav223577/deep_pluck/compare/v0.0.4...v0.1.0) 2017/03/15
66
- - [#5](https://github.com/khiav223577/deep_pluck/pull/5) Support pluck many-to-many associations (@khiav223577)
67
-
68
- ### [v0.0.4](https://github.com/khiav223577/deep_pluck/compare/v0.0.3...v0.0.4) 2017/03/14
69
- - [#4](https://github.com/khiav223577/deep_pluck/pull/4) Fix use deep_pluck on NullRelation will raise exception and prevent unneeded query. (@khiav223577)
70
-
71
- ### [v0.0.3](https://github.com/khiav223577/deep_pluck/compare/v0.0.2...v0.0.3) 2017/03/06
72
- - [#3](https://github.com/khiav223577/deep_pluck/pull/3) Support more than two level (@khiav223577)
73
-
74
- ### [v0.0.2](https://github.com/khiav223577/deep_pluck/compare/v0.0.1...v0.0.2) 2017/03/06
75
- - [#2](https://github.com/khiav223577/deep_pluck/pull/2) The result of has_one association should not be array (@khiav223577)
76
-
77
- ### v0.0.1 2017/03/04
78
- - [#1](https://github.com/khiav223577/deep_pluck/pull/1) Implement deep_pluck (@khiav223577)
1
+ ## Change Log
2
+
3
+ ### [upcoming](https://github.com/khiav223577/deep_pluck/compare/v1.3.0...HEAD) 2023/12/24
4
+ - [#61](https://github.com/khiav223577/deep_pluck/pull/61) Skip the step of publishing code coverage for the PR from forked repos (@khiav223577)
5
+ - [#59](https://github.com/khiav223577/deep_pluck/pull/59) Fix: missing data caused by wrong association definition in test cases (@khiav223577)
6
+
7
+ ### [v1.3.0](https://github.com/khiav223577/deep_pluck/compare/v1.2.1...v1.3.0) 2023/08/10
8
+ - [#58](https://github.com/khiav223577/deep_pluck/pull/58) add frozen_string_literal (@khiav223577)
9
+ - [#57](https://github.com/khiav223577/deep_pluck/pull/57) Always show the association key even if there is no data (@khiav223577)
10
+
11
+ ### [v1.2.1](https://github.com/khiav223577/deep_pluck/compare/v1.2.0...v1.2.1) 2023/08/06
12
+ - [#56](https://github.com/khiav223577/deep_pluck/pull/56) Fix: deep_pluck at has_many association with primary_key options will fail (@khiav223577)
13
+ - [#55](https://github.com/khiav223577/deep_pluck/pull/55) Drop the support of ruby 2.2 (@khiav223577)
14
+ - [#54](https://github.com/khiav223577/deep_pluck/pull/54) Refactor: use gem to setup autoload paths in tests (@khiav223577)
15
+ - [#53](https://github.com/khiav223577/deep_pluck/pull/53) Support Rails 7.0 (@khiav223577)
16
+ - [#52](https://github.com/khiav223577/deep_pluck/pull/52) Support Ruby 3.1 (@khiav223577)
17
+ - [#51](https://github.com/khiav223577/deep_pluck/pull/51) Support Ruby 3.0 (@khiav223577)
18
+
19
+ ### [v1.2.0](https://github.com/khiav223577/deep_pluck/compare/v1.1.7...v1.2.0) 2021/06/10
20
+ - [#50](https://github.com/khiav223577/deep_pluck/pull/50) Support `globalize` gem (@khiav223577)
21
+
22
+ ### [v1.1.7](https://github.com/khiav223577/deep_pluck/compare/v1.1.6...v1.1.7) 2021/04/10
23
+ - [#48](https://github.com/khiav223577/deep_pluck/pull/48) Fix: joins incorrect table when doing inverse lookup for HABTM associations with custom name (@khiav223577)
24
+ - [#45](https://github.com/khiav223577/deep_pluck/pull/45) Do not publish code coverage for PRs from forks (@moon-moon-husky)
25
+
26
+ ### [v1.1.6](https://github.com/khiav223577/deep_pluck/compare/v1.1.5...v1.1.6) 2021/02/09
27
+ - [#43](https://github.com/khiav223577/deep_pluck/pull/43) Fix has_and_belongs_to_many issues when it does not specify "through" table (@khiav223577)
28
+ - [#42](https://github.com/khiav223577/deep_pluck/pull/42) Migrating from Travis CI to GitHub Actions (@khiav223577)
29
+
30
+ ### [v1.1.5](https://github.com/khiav223577/deep_pluck/compare/v1.1.4...v1.1.5) 2021/01/10
31
+ - [#39](https://github.com/khiav223577/deep_pluck/pull/39) Compatibility with Rails 6.1.1 (@klausbadelt)
32
+ - [#40](https://github.com/khiav223577/deep_pluck/pull/40) Exclude tests from coverage (@klausbadelt)
33
+
34
+ ### [v1.1.4](https://github.com/khiav223577/deep_pluck/compare/v1.1.3...v1.1.4) 2020/01/13
35
+ - [#36](https://github.com/khiav223577/deep_pluck/pull/36) A workaround to fix mismatched association named (@khiav223577)
36
+ - [#35](https://github.com/khiav223577/deep_pluck/pull/35) Support Ruby 2.7 (@khiav223577)
37
+
38
+ ### [v1.1.3](https://github.com/khiav223577/deep_pluck/compare/v1.1.2...v1.1.3) 2019/12/17
39
+ - [#34](https://github.com/khiav223577/deep_pluck/pull/34) Support for plucking directly on a has_one through association (@khiav223577)
40
+
41
+ ### [v1.1.2](https://github.com/khiav223577/deep_pluck/compare/v1.1.1...v1.1.2) 2019/09/25
42
+ - [#32](https://github.com/khiav223577/deep_pluck/pull/32) Remove unneeded `PreloadedModel` (@khiav223577)
43
+ - [#31](https://github.com/khiav223577/deep_pluck/pull/31) Support Rails 6.0 (@khiav223577)
44
+ - [#30](https://github.com/khiav223577/deep_pluck/pull/30) Lock sqlite3 version to 1.3.x (@khiav223577)
45
+ - [#28](https://github.com/khiav223577/deep_pluck/pull/28) Fix: broken test cases after bundler 2.0 was released (@khiav223577)
46
+ - [#27](https://github.com/khiav223577/deep_pluck/pull/27) Remove deprecated codeclimate-test-reporter gem and update travis config (@khiav223577)
47
+
48
+ ### [v1.1.1](https://github.com/khiav223577/deep_pluck/compare/v1.1.0...v1.1.1) 2018/07/08
49
+ - [#26](https://github.com/khiav223577/deep_pluck/pull/26) Fix: `id` may disappear when plucking at model instance (@khiav223577)
50
+ - [#25](https://github.com/khiav223577/deep_pluck/pull/25) Refactor - move models definition to separate files (@khiav223577)
51
+ - [#24](https://github.com/khiav223577/deep_pluck/pull/24) test Rails 5.2 (@khiav223577)
52
+ - [#23](https://github.com/khiav223577/deep_pluck/pull/23) It should test both 5.0.x and 5.1.x (@khiav223577)
53
+ - [#22](https://github.com/khiav223577/deep_pluck/pull/22) #deep_pluck at active model without plucking deeply will cause ArgumentError (@khiav223577)
54
+ - [#20](https://github.com/khiav223577/deep_pluck/pull/20) [ENHANCE] Eliminate Extra Select Loop in Hash Lookup (@berniechiu)
55
+
56
+ ### [v1.1.0](https://github.com/khiav223577/deep_pluck/compare/v1.0.3...v1.1.0) 2018/02/15
57
+ - [#19](https://github.com/khiav223577/deep_pluck/pull/19) Support deep_pluck at active model (@khiav223577)
58
+ - [#18](https://github.com/khiav223577/deep_pluck/pull/18) Add rubocop and Improve code quality (@khiav223577)
59
+
60
+ ### [v1.0.3](https://github.com/khiav223577/deep_pluck/compare/v1.0.2...v1.0.3) 2017/06/30
61
+ - [#15](https://github.com/khiav223577/deep_pluck/pull/15) Test deep_pluck in rails 5.1.x (@khiav223577)
62
+ - [#14](https://github.com/khiav223577/deep_pluck/pull/14) Handle polymorphic associations correctly. (@Bogadon)
63
+
64
+ ### [v1.0.0](https://github.com/khiav223577/deep_pluck/compare/v0.1.4...v1.0.0) 2017/03/28
65
+ - [#12](https://github.com/khiav223577/deep_pluck/pull/12) Reduce cyclomatic complexity in model.rb (@khiav223577)
66
+
67
+ ### [v0.1.4](https://github.com/khiav223577/deep_pluck/compare/v0.1.3...v0.1.4) 2017/03/27
68
+ - [#11](https://github.com/khiav223577/deep_pluck/pull/11) Fix conditional associations (@khiav223577)
69
+
70
+ ### [v0.1.3](https://github.com/khiav223577/deep_pluck/compare/v0.1.2...v0.1.3) 2017/03/20
71
+ - [#10](https://github.com/khiav223577/deep_pluck/pull/10) Fix custom foreign_key, custom primary_key issues (@khiav223577)
72
+ - [#9](https://github.com/khiav223577/deep_pluck/pull/9) Fix has_and_belongs_to_many (@khiav223577)
73
+
74
+ ### [v0.1.2](https://github.com/khiav223577/deep_pluck/compare/v0.1.1...v0.1.2) 2017/03/17
75
+ - [#8](https://github.com/khiav223577/deep_pluck/pull/8) Fix that some need columns are missing (@khiav223577)
76
+ - [#7](https://github.com/khiav223577/deep_pluck/pull/7) Raise error message when association not found (@khiav223577)
77
+
78
+ ### [v0.1.1](https://github.com/khiav223577/deep_pluck/compare/v0.1.0...v0.1.1) 2017/03/16
79
+ - [#6](https://github.com/khiav223577/deep_pluck/pull/6) Fix deep_pluck with #joins (@khiav223577)
80
+
81
+ ### [v0.1.0](https://github.com/khiav223577/deep_pluck/compare/v0.0.4...v0.1.0) 2017/03/15
82
+ - [#5](https://github.com/khiav223577/deep_pluck/pull/5) Support pluck many-to-many associations (@khiav223577)
83
+
84
+ ### [v0.0.4](https://github.com/khiav223577/deep_pluck/compare/v0.0.3...v0.0.4) 2017/03/14
85
+ - [#4](https://github.com/khiav223577/deep_pluck/pull/4) Fix use deep_pluck on NullRelation will raise exception and prevent unneeded query. (@khiav223577)
86
+
87
+ ### [v0.0.3](https://github.com/khiav223577/deep_pluck/compare/v0.0.2...v0.0.3) 2017/03/06
88
+ - [#3](https://github.com/khiav223577/deep_pluck/pull/3) Support more than two level (@khiav223577)
89
+
90
+ ### [v0.0.2](https://github.com/khiav223577/deep_pluck/compare/v0.0.1...v0.0.2) 2017/03/06
91
+ - [#2](https://github.com/khiav223577/deep_pluck/pull/2) The result of has_one association should not be array (@khiav223577)
92
+
93
+ ### v0.0.1 2017/03/04
94
+ - [#1](https://github.com/khiav223577/deep_pluck/pull/1) Implement deep_pluck (@khiav223577)
data/README.md CHANGED
@@ -10,7 +10,7 @@ Allow you to pluck deeply into nested associations without loading a bunch of re
10
10
 
11
11
  ## Supports
12
12
  - Ruby 2.3 ~ 2.7, 3.0 ~ 3.1
13
- - Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 7.0
13
+ - Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 7.0, 7.1
14
14
 
15
15
  ## Installation
16
16
 
data/deep_pluck.gemspec CHANGED
@@ -40,6 +40,6 @@ Gem::Specification.new do |spec|
40
40
  spec.add_development_dependency 'minitest', '~> 5.0'
41
41
 
42
42
  spec.add_dependency 'activerecord', '>= 3'
43
- spec.add_dependency 'pluck_all', '>= 2.3.2'
43
+ spec.add_dependency 'pluck_all', '>= 2.3.4'
44
44
  spec.add_dependency 'rails_compatibility', '>= 0.0.8'
45
45
  end
data/gemfiles/7.0.gemfile CHANGED
@@ -1,8 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'zeitwerk'
4
- gem 'activerecord', '~> 7.0.0'
5
- gem 'pluck_all', '~> 2.3.2'
4
+ gem 'activerecord', '~> 7.0.8'
5
+ gem 'pluck_all', '~> 2.3.4'
6
6
 
7
7
  # gem 'globalize', '~> 6.0.1' # TODO: wait for globalize to support Rails 7.
8
8
 
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'zeitwerk'
4
+ gem 'activerecord', '~> 7.1.2'
5
+ gem 'pluck_all', '~> 2.3.4'
6
+
7
+ # gem 'globalize', '~> 6.0.1' # TODO: wait for globalize to support Rails 7.
8
+
9
+ group :test do
10
+ gem 'simplecov', '< 0.18'
11
+ gem 'sqlite3', '~> 1.4.1'
12
+ end
13
+
14
+ gemspec path: '../'
@@ -1,40 +1,53 @@
1
- module DeepPluck
2
- module DataCombiner
3
- class << self
4
- def combine_data(parent, children, primary_key, column_name, foreign_key, reverse, collection)
5
- source = reverse ? parent : children
6
- target = !reverse ? parent : children
7
- data_hash = make_data_hash(collection, source, primary_key, column_name)
8
- assign_values_to_parent(collection, target, data_hash, column_name, foreign_key, reverse: reverse)
9
- return children
10
- end
11
-
12
- private
13
-
14
- def make_data_hash(collection, parent, primary_key, column_name)
15
- return parent.map{|s| [s[primary_key], s] }.to_h if !collection
16
- hash = {}
17
- parent.each do |model_hash|
18
- key = model_hash[primary_key]
19
- array = (hash[key] ? hash[key][column_name] : []) # share the children if id is duplicated
20
- model_hash[column_name] = array
21
- hash[key] = model_hash
22
- end
23
- return hash
24
- end
25
-
26
- def assign_values_to_parent(collection, parent, children_hash, column_name, foreign_key, reverse: false)
27
- parent.each do |s|
28
- next if (id = s[foreign_key]) == nil
29
- left = reverse ? children_hash[id] : s
30
- right = !reverse ? children_hash[id] : s
31
- if collection
32
- left[column_name] << right
33
- else
34
- left[column_name] = right
35
- end
36
- end
37
- end
38
- end
39
- end
40
- end
1
+ # frozen_string_literal: true
2
+
3
+ module DeepPluck
4
+ module DataCombiner
5
+ class << self
6
+ def combine_data(parent, children, primary_key, column_name, foreign_key, reverse, collection)
7
+ source = reverse ? parent : children
8
+ target = !reverse ? parent : children
9
+ data_hash = make_data_hash(collection, source, primary_key, column_name, reverse)
10
+ assign_values_to_parent(collection, target, data_hash, column_name, foreign_key, reverse: reverse)
11
+ return children
12
+ end
13
+
14
+ private
15
+
16
+ def make_data_hash(collection, parent, primary_key, column_name, reverse)
17
+ hash = {}
18
+
19
+ parent.each do |model_hash|
20
+ key = model_hash[primary_key]
21
+
22
+ if reverse
23
+ if collection
24
+ array = (hash[key] ? hash[key][column_name] : []) # share the children if id is duplicated
25
+ model_hash[column_name] = array # set the value of this association key to empty array by default
26
+ else
27
+ model_hash[column_name] = nil # set the value of this association key to be nil by default
28
+ end
29
+ end
30
+
31
+ hash[key] = model_hash
32
+ end
33
+
34
+ return hash
35
+ end
36
+
37
+ def assign_values_to_parent(collection, parent, children_hash, column_name, foreign_key, reverse: false)
38
+ parent.each do |data|
39
+ id = data[foreign_key]
40
+
41
+ left = reverse ? children_hash[id] : data
42
+ right = !reverse ? children_hash[id] : data
43
+
44
+ if collection
45
+ left[column_name] << right
46
+ else
47
+ left[column_name] = right
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails_compatibility'
2
4
  require 'rails_compatibility/unscope_where'
3
5
  require 'rails_compatibility/build_joins'
@@ -68,7 +70,8 @@ module DeepPluck
68
70
  end
69
71
 
70
72
  def get_association_scope(reflect)
71
- RailsCompatibility.unscope_where(reflect.association_class.new({}, reflect).send(:association_scope))
73
+ owner = reflect.association_class.new(reflect.active_record.new, reflect)
74
+ return RailsCompatibility.unscope_where(owner.send(:association_scope))
72
75
  end
73
76
 
74
77
  def use_association_to_query?(reflect)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module DeepPluck
2
- VERSION = '1.2.1'
4
+ VERSION = '1.3.1'
3
5
  end
data/lib/deep_pluck.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'deep_pluck/version'
2
4
  require 'deep_pluck/model'
3
5
  require 'active_record'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_pluck
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - khiav reoy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-06 00:00:00.000000000 Z
11
+ date: 2023-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - ">="
94
94
  - !ruby/object:Gem::Version
95
- version: 2.3.2
95
+ version: 2.3.4
96
96
  type: :runtime
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
- version: 2.3.2
102
+ version: 2.3.4
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rails_compatibility
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -143,6 +143,7 @@ files:
143
143
  - gemfiles/6.0.gemfile
144
144
  - gemfiles/6.1.gemfile
145
145
  - gemfiles/7.0.gemfile
146
+ - gemfiles/7.1.gemfile
146
147
  - lib/deep_pluck.rb
147
148
  - lib/deep_pluck/data_combiner.rb
148
149
  - lib/deep_pluck/model.rb