active_record_inherit_assoc 2.5.1 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 5744502d4b1e1941a08f829565227940755e2242
4
- data.tar.gz: 5f27dc1364e9859a0cae9dd40d03cafa25f57534
2
+ SHA256:
3
+ metadata.gz: 3114b730174fd26ee0f56c6e133fb5d93706d80770fa265d3c040eb1c29638ec
4
+ data.tar.gz: b3e5f39e1255c5f588b7b250920eadea41d50fac9285b19f387958d8367eb4b9
5
5
  SHA512:
6
- metadata.gz: ac5a23f39ff9ed338c004966f7e0077c27c71309932636379af39a49598abab216718481f0f91d2b21550cc9acc5b202a48599dadea739aa3e31865111e70924
7
- data.tar.gz: e9eaa77c7e46fec4faf8401828144b1d84b12d6250cef9205d1ad817fdd330d3e7c60a59c7896b0ad278cb46c6693d13bc29c2269ddfd3e7feebbd65421478da
6
+ metadata.gz: 3fae511b4f18e0908e2c155a841896f76b2bd10d30a699c4d104233ad92d1666af3e51d93429e6ec430ec02a1bdc1d90454373b391893f7734536186e364ff39
7
+ data.tar.gz: 1d270c6bc45fdbbf784ac4c7e465a303a06f2f4248f960f68b788fe85516c1848ddc7a4cdbe92ee5b85378f3b2434b8a5270832f9226df397e06314a6a7aeab7
@@ -1,12 +1,9 @@
1
1
  require 'active_record'
2
2
 
3
3
  case ActiveRecord::VERSION::MAJOR
4
- when 3
5
- ActiveRecord::Associations::Builder::HasMany.valid_options << :inherit
6
- ActiveRecord::Associations::Builder::HasOne.valid_options << :inherit
7
- ActiveRecord::Associations::Builder::BelongsTo.valid_options << :inherit
8
4
  when 4
9
5
  ActiveRecord::Associations::Builder::Association.valid_options << :inherit
6
+ ActiveRecord::Associations::Builder::Association.valid_options << :inherit_allowed_list
10
7
  when 5
11
8
  # We can't add options into `valid_options` anymore.
12
9
  # Here are the possible solutions:
@@ -16,6 +13,7 @@ when 5
16
13
  #
17
14
  # I went with the first one out of simplicity.
18
15
  ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :inherit
16
+ ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :inherit_allowed_list
19
17
  end
20
18
 
21
19
  module ActiveRecordInheritAssocPrepend
@@ -31,7 +29,14 @@ module ActiveRecordInheritAssocPrepend
31
29
 
32
30
  def attribute_inheritance_hash
33
31
  return nil unless reflection.options[:inherit]
34
- Array(reflection.options[:inherit]).inject({}) { |hash, association| hash[association] = owner.send(association) ; hash }
32
+ inherit_allowed_list = reflection.options[:inherit_allowed_list]
33
+
34
+ Array(reflection.options[:inherit]).each_with_object({}) do |association, hash|
35
+ assoc_value = owner.send(association)
36
+ assoc_value = Array(assoc_value).concat(inherit_allowed_list) if inherit_allowed_list
37
+ hash[association] = assoc_value
38
+ hash["#{through_reflection.table_name}.#{association}"] = assoc_value if reflection.options.key?(:through)
39
+ end
35
40
  end
36
41
 
37
42
  if ActiveRecord::VERSION::MAJOR >= 4
@@ -63,10 +68,27 @@ module ActiveRecordInheritPreloadAssocPrepend
63
68
  end
64
69
  end
65
70
 
71
+ def scope
72
+ prescope = super
73
+
74
+ if inherit = reflection.options[:inherit]
75
+ Array(inherit).each do |inherit_assoc|
76
+ owner_values = owners.map(&inherit_assoc)
77
+ owner_values.compact!
78
+ owner_values.uniq!
79
+ owner_values.concat(reflection.options[:inherit_allowed_list]) if reflection.options[:inherit_allowed_list]
80
+ prescope = prescope.where(inherit_assoc => owner_values)
81
+ end
82
+ end
83
+
84
+ prescope
85
+ end
86
+
66
87
  def filter_associated_records_with_inherit!(owner, associated_records, inherit)
67
88
  associated_records.select! do |record|
68
89
  Array(inherit).all? do |association|
69
- record.send(association) == owner.send(association)
90
+ record_value = record.send(association)
91
+ record_value == owner.send(association) || reflection.options[:inherit_allowed_list]&.include?(record_value)
70
92
  end
71
93
  end
72
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_inherit_assoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.1
4
+ version: 2.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Osheroff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-29 00:00:00.000000000 Z
11
+ date: 2020-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.0
19
+ version: 4.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.2'
22
+ version: '5.3'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 3.2.0
29
+ version: 4.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.2'
32
+ version: '5.3'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: minitest
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -162,15 +162,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
162
  requirements:
163
163
  - - ">="
164
164
  - !ruby/object:Gem::Version
165
- version: '0'
165
+ version: '2.4'
166
166
  required_rubygems_version: !ruby/object:Gem::Requirement
167
167
  requirements:
168
168
  - - ">="
169
169
  - !ruby/object:Gem::Version
170
170
  version: '0'
171
171
  requirements: []
172
- rubyforge_project:
173
- rubygems_version: 2.6.14
172
+ rubygems_version: 3.0.3
174
173
  signing_key:
175
174
  specification_version: 4
176
175
  summary: Attribute inheritance for AR associations