active_record_inherit_assoc 2.10.0
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 +7 -0
- data/lib/active_record_inherit_assoc.rb +125 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3114b730174fd26ee0f56c6e133fb5d93706d80770fa265d3c040eb1c29638ec
|
4
|
+
data.tar.gz: b3e5f39e1255c5f588b7b250920eadea41d50fac9285b19f387958d8367eb4b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3fae511b4f18e0908e2c155a841896f76b2bd10d30a699c4d104233ad92d1666af3e51d93429e6ec430ec02a1bdc1d90454373b391893f7734536186e364ff39
|
7
|
+
data.tar.gz: 1d270c6bc45fdbbf784ac4c7e465a303a06f2f4248f960f68b788fe85516c1848ddc7a4cdbe92ee5b85378f3b2434b8a5270832f9226df397e06314a6a7aeab7
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
case ActiveRecord::VERSION::MAJOR
|
4
|
+
when 4
|
5
|
+
ActiveRecord::Associations::Builder::Association.valid_options << :inherit
|
6
|
+
ActiveRecord::Associations::Builder::Association.valid_options << :inherit_allowed_list
|
7
|
+
when 5
|
8
|
+
# We can't add options into `valid_options` anymore.
|
9
|
+
# Here are the possible solutions:
|
10
|
+
# * monkey patch Assocition::VALID_OPTIONS
|
11
|
+
# * prepend the `valid_options` method
|
12
|
+
# * create an Extension class and add it via ActiveRecord::Associations::Builder::Association.extensions
|
13
|
+
#
|
14
|
+
# I went with the first one out of simplicity.
|
15
|
+
ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :inherit
|
16
|
+
ActiveRecord::Associations::Builder::Association::VALID_OPTIONS << :inherit_allowed_list
|
17
|
+
end
|
18
|
+
|
19
|
+
module ActiveRecordInheritAssocPrepend
|
20
|
+
def target_scope
|
21
|
+
if inherited_attributes = attribute_inheritance_hash
|
22
|
+
super.where(inherited_attributes)
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def attribute_inheritance_hash
|
31
|
+
return nil unless reflection.options[:inherit]
|
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
|
40
|
+
end
|
41
|
+
|
42
|
+
if ActiveRecord::VERSION::MAJOR >= 4
|
43
|
+
def skip_statement_cache?(*)
|
44
|
+
super || !!reflection.options[:inherit]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
ActiveRecord::Associations::Association.send(:prepend, ActiveRecordInheritAssocPrepend)
|
50
|
+
|
51
|
+
module ActiveRecordInheritPreloadAssocPrepend
|
52
|
+
if ActiveRecord::VERSION::STRING < '5.2.0'
|
53
|
+
def associated_records_by_owner(*)
|
54
|
+
super.tap do |result|
|
55
|
+
next unless inherit = reflection.options[:inherit]
|
56
|
+
result.each do |owner, associated_records|
|
57
|
+
filter_associated_records_with_inherit!(owner, associated_records, inherit)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
else
|
62
|
+
def associate_records_to_owner(owner, records)
|
63
|
+
if inherit = reflection.options[:inherit]
|
64
|
+
records = Array(records)
|
65
|
+
filter_associated_records_with_inherit!(owner, records, inherit)
|
66
|
+
end
|
67
|
+
super
|
68
|
+
end
|
69
|
+
end
|
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
|
+
|
87
|
+
def filter_associated_records_with_inherit!(owner, associated_records, inherit)
|
88
|
+
associated_records.select! do |record|
|
89
|
+
Array(inherit).all? do |association|
|
90
|
+
record_value = record.send(association)
|
91
|
+
record_value == owner.send(association) || reflection.options[:inherit_allowed_list]&.include?(record_value)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
ActiveRecord::Associations::Preloader::Association.send(:prepend, ActiveRecordInheritPreloadAssocPrepend)
|
98
|
+
|
99
|
+
class ActiveRecord::Base
|
100
|
+
# Makes the model inherit the specified attribute from a named association.
|
101
|
+
#
|
102
|
+
# parent_name - The Symbol name of the parent association.
|
103
|
+
# options - The Hash options to use:
|
104
|
+
# :attr - A Symbol or an Array of Symbol names of the attributes
|
105
|
+
# that should be inherited from the parent association.
|
106
|
+
#
|
107
|
+
# Examples
|
108
|
+
#
|
109
|
+
# class Post < ActiveRecord::Base
|
110
|
+
# belongs_to :category
|
111
|
+
# inherits_from :category, :attr => :account
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
def self.inherits_from(parent_name, options = {})
|
115
|
+
attrs = Array.wrap(options.fetch(:attr))
|
116
|
+
|
117
|
+
before_validation do |model|
|
118
|
+
parent = model.send(parent_name)
|
119
|
+
|
120
|
+
attrs.each do |attr|
|
121
|
+
model[attr] = parent[attr] if parent.present?
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_record_inherit_assoc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.10.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Osheroff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.2.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.3'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.2.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.3'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: minitest
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest-rg
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rails
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: bump
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
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
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: sqlite3
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: byebug
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
description:
|
146
|
+
email:
|
147
|
+
- ben@gimbo.net
|
148
|
+
executables: []
|
149
|
+
extensions: []
|
150
|
+
extra_rdoc_files: []
|
151
|
+
files:
|
152
|
+
- lib/active_record_inherit_assoc.rb
|
153
|
+
homepage: https://github.com/zendesk/active_record_inherit_assoc
|
154
|
+
licenses:
|
155
|
+
- Apache License Version 2.0
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '2.4'
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubygems_version: 3.0.3
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: Attribute inheritance for AR associations
|
176
|
+
test_files: []
|