active_record_inherit_assoc 2.5.0 → 2.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/active_record_inherit_assoc.rb +38 -11
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b55c3f0e3b88e52836c8412b5f90299f6bfb5cd3850338328b58e3409afdeefd
|
4
|
+
data.tar.gz: 743abb357de27dd7954bbbabc1a72ef95ac9928b4762f8031ba55082bb7fed2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e911e8243061ca030b4293150cd433c1963f72154b849754d2460f02fa85e5b885a691ad3fe7df442de3cb5539fb70b016f100a7bbf0f578a51d9d3d38d42d26
|
7
|
+
data.tar.gz: 0ad8b533b645a0f71bc7e9978afb571ccdaad7fe889f6d1015b411f457268b62b86cab07d17acaa02cce904d332603606bb4cdb27bd2c521123773e7c4c9df6a
|
@@ -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_if
|
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_if
|
19
17
|
end
|
20
18
|
|
21
19
|
module ActiveRecordInheritAssocPrepend
|
@@ -31,11 +29,17 @@ module ActiveRecordInheritAssocPrepend
|
|
31
29
|
|
32
30
|
def attribute_inheritance_hash
|
33
31
|
return nil unless reflection.options[:inherit]
|
34
|
-
|
32
|
+
|
33
|
+
Array(reflection.options[:inherit]).each_with_object({}) do |association, hash|
|
34
|
+
assoc_value = owner.send(association)
|
35
|
+
next if reflection.options[:inherit_if] && !reflection.options[:inherit_if].call(owner)
|
36
|
+
hash[association] = assoc_value
|
37
|
+
hash["#{through_reflection.table_name}.#{association}"] = assoc_value if reflection.options.key?(:through)
|
38
|
+
end
|
35
39
|
end
|
36
40
|
|
37
41
|
if ActiveRecord::VERSION::MAJOR >= 4
|
38
|
-
def skip_statement_cache?
|
42
|
+
def skip_statement_cache?(*)
|
39
43
|
super || !!reflection.options[:inherit]
|
40
44
|
end
|
41
45
|
end
|
@@ -44,15 +48,38 @@ end
|
|
44
48
|
ActiveRecord::Associations::Association.send(:prepend, ActiveRecordInheritAssocPrepend)
|
45
49
|
|
46
50
|
module ActiveRecordInheritPreloadAssocPrepend
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
51
|
+
if ActiveRecord::VERSION::STRING < '5.2.0'
|
52
|
+
def associated_records_by_owner(*)
|
53
|
+
super.tap do |result|
|
54
|
+
next unless inherit = reflection.options[:inherit]
|
55
|
+
result.each do |owner, associated_records|
|
56
|
+
filter_associated_records_with_inherit!(owner, associated_records, inherit)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
else
|
61
|
+
def associate_records_to_owner(owner, records)
|
62
|
+
if inherit = reflection.options[:inherit]
|
63
|
+
records = Array(records)
|
64
|
+
filter_associated_records_with_inherit!(owner, records, inherit)
|
52
65
|
end
|
66
|
+
super
|
53
67
|
end
|
54
68
|
end
|
55
69
|
|
70
|
+
def scope
|
71
|
+
prescope = super
|
72
|
+
|
73
|
+
if inherit = reflection.options[:inherit]
|
74
|
+
Array(inherit).each do |inherit_assoc|
|
75
|
+
owner_values = owners.map(&inherit_assoc).compact.uniq
|
76
|
+
prescope = prescope.where(inherit_assoc => owner_values)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
prescope
|
81
|
+
end
|
82
|
+
|
56
83
|
def filter_associated_records_with_inherit!(owner, associated_records, inherit)
|
57
84
|
associated_records.select! do |record|
|
58
85
|
Array(inherit).all? do |association|
|
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.
|
4
|
+
version: 2.9.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:
|
11
|
+
date: 2020-07-15 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:
|
19
|
+
version: 4.2.0
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5.
|
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:
|
29
|
+
version: 4.2.0
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
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: '
|
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
|
-
|
173
|
-
rubygems_version: 2.6.11
|
172
|
+
rubygems_version: 3.0.3
|
174
173
|
signing_key:
|
175
174
|
specification_version: 4
|
176
175
|
summary: Attribute inheritance for AR associations
|