active_record_inherit_assoc 2.8.0 → 2.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/active_record_inherit_assoc.rb +16 -14
  3. metadata +12 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8bd387d89684408a56073510d1a9b3d14b119b2a
4
- data.tar.gz: 39514eaff5903fb90e2a853e8fd1896678d36179
2
+ SHA256:
3
+ metadata.gz: 9524fb500abe5d5f81a49ca6b4c8e45fdcaaca94b18624fb9c1c31ac658f46df
4
+ data.tar.gz: 5002be646071088dc91dd12c2e6a7ddcb4ee9bc1e9e57789c94acfe1f9ea029c
5
5
  SHA512:
6
- metadata.gz: a5961ad5fefe3009e6ebec716ea1ca81de282db1ee4acf68267d9a838446c13c3860c61712dc94773f5e878917def5f5c2e2140702e1e23d79582a95e776614c
7
- data.tar.gz: 1c4469bd61211da0e0f85a1603024a7c0c644de3ac68cddf38e5c72202cae165c8f4311f4536538da9c376879168547bb726616244bd00e22e0466cac7f88ed4
6
+ metadata.gz: 155b4f0726f474e1bff0704cd5cefc009407e796e31df31cd456b33105ffb125534071c0056c8d2c9ae233ccba9db4e158b5ea585796958509e976cda66bbb30
7
+ data.tar.gz: 6d85eeddbeebb22610bba310f304f987ec20116e33f0ea390dcb8a2172ecb1d4ed32d84ac12a6f4219ad0e0ba9c37876c26ec45360c02e05bc81eaadac6c573f
@@ -1,19 +1,15 @@
1
1
  require 'active_record'
2
2
 
3
- case ActiveRecord::VERSION::MAJOR
4
- when 4
5
- ActiveRecord::Associations::Builder::Association.valid_options << :inherit
6
- when 5
7
- # We can't add options into `valid_options` anymore.
8
- # Here are the possible solutions:
9
- # * monkey patch Assocition::VALID_OPTIONS
10
- # * prepend the `valid_options` method
11
- # * create an Extension class and add it via ActiveRecord::Associations::Builder::Association.extensions
12
- #
13
- # I went with the first one out of simplicity.
14
- ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :inherit
3
+ module ActiveRecordInheritBuildAssocPrepend
4
+ INHERIT_OPTIONS = %i[inherit inherit_allowed_list].freeze
5
+
6
+ def valid_options(options)
7
+ super + INHERIT_OPTIONS
8
+ end
15
9
  end
16
10
 
11
+ ActiveRecord::Associations::Builder::Association.singleton_class.prepend(ActiveRecordInheritBuildAssocPrepend)
12
+
17
13
  module ActiveRecordInheritAssocPrepend
18
14
  def target_scope
19
15
  if inherited_attributes = attribute_inheritance_hash
@@ -27,9 +23,11 @@ module ActiveRecordInheritAssocPrepend
27
23
 
28
24
  def attribute_inheritance_hash
29
25
  return nil unless reflection.options[:inherit]
26
+ inherit_allowed_list = reflection.options[:inherit_allowed_list]
30
27
 
31
28
  Array(reflection.options[:inherit]).each_with_object({}) do |association, hash|
32
29
  assoc_value = owner.send(association)
30
+ assoc_value = Array(assoc_value).concat(inherit_allowed_list) if inherit_allowed_list
33
31
  hash[association] = assoc_value
34
32
  hash["#{through_reflection.table_name}.#{association}"] = assoc_value if reflection.options.key?(:through)
35
33
  end
@@ -69,7 +67,10 @@ module ActiveRecordInheritPreloadAssocPrepend
69
67
 
70
68
  if inherit = reflection.options[:inherit]
71
69
  Array(inherit).each do |inherit_assoc|
72
- owner_values = owners.map(&inherit_assoc).compact.uniq
70
+ owner_values = owners.map(&inherit_assoc)
71
+ owner_values.compact!
72
+ owner_values.uniq!
73
+ owner_values.concat(reflection.options[:inherit_allowed_list]) if reflection.options[:inherit_allowed_list]
73
74
  prescope = prescope.where(inherit_assoc => owner_values)
74
75
  end
75
76
  end
@@ -80,7 +81,8 @@ module ActiveRecordInheritPreloadAssocPrepend
80
81
  def filter_associated_records_with_inherit!(owner, associated_records, inherit)
81
82
  associated_records.select! do |record|
82
83
  Array(inherit).all? do |association|
83
- record.send(association) == owner.send(association)
84
+ record_value = record.send(association)
85
+ record_value == owner.send(association) || reflection.options[:inherit_allowed_list]&.include?(record_value)
84
86
  end
85
87
  end
86
88
  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.8.0
4
+ version: 2.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Osheroff
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-17 00:00:00.000000000 Z
11
+ date: 2022-04-19 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: 4.2.0
19
+ version: 5.0.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5.3'
22
+ version: '6.2'
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: 4.2.0
29
+ version: 5.0.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5.3'
32
+ version: '6.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: minitest
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -100,20 +100,6 @@ dependencies:
100
100
  - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
- - !ruby/object:Gem::Dependency
104
- name: wwtd
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - ">="
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - ">="
115
- - !ruby/object:Gem::Version
116
- version: '0'
117
103
  - !ruby/object:Gem::Dependency
118
104
  name: sqlite3
119
105
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +128,7 @@ dependencies:
142
128
  - - ">="
143
129
  - !ruby/object:Gem::Version
144
130
  version: '0'
145
- description:
131
+ description:
146
132
  email:
147
133
  - ben@gimbo.net
148
134
  executables: []
@@ -154,7 +140,7 @@ homepage: https://github.com/zendesk/active_record_inherit_assoc
154
140
  licenses:
155
141
  - Apache License Version 2.0
156
142
  metadata: {}
157
- post_install_message:
143
+ post_install_message:
158
144
  rdoc_options: []
159
145
  require_paths:
160
146
  - lib
@@ -162,16 +148,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
148
  requirements:
163
149
  - - ">="
164
150
  - !ruby/object:Gem::Version
165
- version: '2.4'
151
+ version: '2.7'
166
152
  required_rubygems_version: !ruby/object:Gem::Requirement
167
153
  requirements:
168
154
  - - ">="
169
155
  - !ruby/object:Gem::Version
170
156
  version: '0'
171
157
  requirements: []
172
- rubyforge_project:
173
- rubygems_version: 2.6.14.4
174
- signing_key:
158
+ rubygems_version: 3.1.6
159
+ signing_key:
175
160
  specification_version: 4
176
161
  summary: Attribute inheritance for AR associations
177
162
  test_files: []