store_base_sti_class 3.0.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/lib/store_base_sti_class/version.rb +1 -1
- data/lib/store_base_sti_class.rb +152 -3
- data/store_base_sti_class.gemspec +14 -14
- metadata +10 -13
- data/lib/store_base_sti_class_for_6_0.rb +0 -153
- data/lib/store_base_sti_class_for_6_1.rb +0 -156
- data/lib/store_base_sti_class_for_7_0.rb +0 -156
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4475684e4eba21244f0aa189b876d00242379a3e4dac260b7a730eacbe7958b
|
4
|
+
data.tar.gz: 0a5ed0660a15ec4493dde2fc3f831813e804b5a18ece540921c9ac50d89e5df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a66ac2b24d20bef464fd9deb2b654eaf2932f6ae7893d4025ae35665e926d156a92dafe13cb424cb71414c65223c475fb19f713ef3e14d21a172581545c484e8
|
7
|
+
data.tar.gz: bd3f65931e6e64296b555f68e96907bb38c3fc283c1e29ad9324188ab0370767ea4ceba4cf17d37b9e28e0bf09786973bb77afc2787a19d9a0ce83d31f1bcf38
|
data/LICENSE.txt
CHANGED
data/lib/store_base_sti_class.rb
CHANGED
@@ -1,8 +1,157 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
require 'active_record'
|
4
2
|
|
5
|
-
require
|
3
|
+
require 'active_record/associations/join_dependency/join_part'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
class Base
|
7
|
+
class_attribute :store_base_sti_class
|
8
|
+
self.store_base_sti_class = true
|
9
|
+
end
|
10
|
+
|
11
|
+
module Inheritance
|
12
|
+
module ClassMethods
|
13
|
+
def polymorphic_name
|
14
|
+
ActiveRecord::Base.store_base_sti_class ? base_class.name : name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Associations
|
20
|
+
class Preloader
|
21
|
+
class ThroughAssociation < Association
|
22
|
+
private
|
23
|
+
|
24
|
+
def through_scope
|
25
|
+
scope = through_reflection.klass.unscoped
|
26
|
+
options = reflection.options
|
27
|
+
|
28
|
+
values = reflection_scope.values
|
29
|
+
if annotations = values[:annotate]
|
30
|
+
scope.annotate!(*annotations)
|
31
|
+
end
|
32
|
+
|
33
|
+
if options[:source_type]
|
34
|
+
# BEGIN PATCH
|
35
|
+
# original:
|
36
|
+
# scope.where! reflection.foreign_type => options[:source_type]
|
37
|
+
|
38
|
+
adjusted_foreign_type =
|
39
|
+
if ActiveRecord::Base.store_base_sti_class
|
40
|
+
options[:source_type]
|
41
|
+
else
|
42
|
+
([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
|
43
|
+
end
|
44
|
+
|
45
|
+
scope.where! reflection.foreign_type => adjusted_foreign_type
|
46
|
+
# END PATCH
|
47
|
+
|
48
|
+
elsif !reflection_scope.where_clause.empty?
|
49
|
+
scope.where_clause = reflection_scope.where_clause
|
50
|
+
|
51
|
+
if includes = values[:includes]
|
52
|
+
scope.includes!(source_reflection.name => includes)
|
53
|
+
else
|
54
|
+
scope.includes!(source_reflection.name)
|
55
|
+
end
|
56
|
+
|
57
|
+
if values[:references] && !values[:references].empty?
|
58
|
+
scope.references_values |= values[:references]
|
59
|
+
else
|
60
|
+
scope.references!(source_reflection.table_name)
|
61
|
+
end
|
62
|
+
|
63
|
+
if joins = values[:joins]
|
64
|
+
scope.joins!(source_reflection.name => joins)
|
65
|
+
end
|
66
|
+
|
67
|
+
if left_outer_joins = values[:left_outer_joins]
|
68
|
+
scope.left_outer_joins!(source_reflection.name => left_outer_joins)
|
69
|
+
end
|
70
|
+
|
71
|
+
if scope.eager_loading? && order_values = values[:order]
|
72
|
+
scope = scope.order(order_values)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
scope
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class AssociationScope
|
82
|
+
private
|
83
|
+
|
84
|
+
def next_chain_scope(scope, reflection, next_reflection)
|
85
|
+
primary_key = reflection.join_primary_key
|
86
|
+
foreign_key = reflection.join_foreign_key
|
87
|
+
|
88
|
+
table = reflection.aliased_table
|
89
|
+
foreign_table = next_reflection.aliased_table
|
90
|
+
constraint = table[primary_key].eq(foreign_table[foreign_key])
|
91
|
+
|
92
|
+
if reflection.type
|
93
|
+
# BEGIN PATCH
|
94
|
+
# original:
|
95
|
+
# value = transform_value(next_reflection.klass.polymorphic_name)
|
96
|
+
# scope = apply_scope(scope, table, reflection.type, value)
|
97
|
+
|
98
|
+
if ActiveRecord::Base.store_base_sti_class
|
99
|
+
value = transform_value(next_reflection.klass.polymorphic_name)
|
100
|
+
else
|
101
|
+
klass = next_reflection.klass
|
102
|
+
value = ([klass] + klass.descendants).map(&:name)
|
103
|
+
end
|
104
|
+
scope = apply_scope(scope, table, reflection.type, value)
|
105
|
+
# END PATCH
|
106
|
+
end
|
107
|
+
|
108
|
+
scope.joins!(join(foreign_table, constraint))
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
class HasManyThroughAssociation
|
114
|
+
private
|
115
|
+
|
116
|
+
def build_through_record(record)
|
117
|
+
@through_records[record.object_id] ||= begin
|
118
|
+
ensure_mutable
|
119
|
+
|
120
|
+
attributes = through_scope_attributes
|
121
|
+
attributes[source_reflection.name] = record
|
122
|
+
|
123
|
+
# START PATCH
|
124
|
+
if ActiveRecord::Base.store_base_sti_class
|
125
|
+
attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
|
126
|
+
end
|
127
|
+
# END PATCH
|
128
|
+
|
129
|
+
through_association.build(attributes)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
module Reflection
|
136
|
+
class PolymorphicReflection
|
137
|
+
def source_type_scope
|
138
|
+
type = @previous_reflection.foreign_type
|
139
|
+
source_type = @previous_reflection.options[:source_type]
|
140
|
+
|
141
|
+
# START PATCH
|
142
|
+
adjusted_source_type =
|
143
|
+
if ActiveRecord::Base.store_base_sti_class
|
144
|
+
source_type
|
145
|
+
else
|
146
|
+
([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
|
147
|
+
end
|
148
|
+
# END PATCH
|
149
|
+
|
150
|
+
lambda { |object| where(type => adjusted_source_type) }
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
6
155
|
|
7
156
|
module StoreBaseSTIClass
|
8
157
|
end
|
@@ -3,28 +3,28 @@
|
|
3
3
|
require_relative 'lib/store_base_sti_class/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name
|
7
|
-
spec.version
|
8
|
-
spec.platform
|
9
|
-
spec.author
|
10
|
-
spec.email
|
11
|
-
spec.description
|
6
|
+
spec.name = 'store_base_sti_class'
|
7
|
+
spec.version = StoreBaseSTIClass::VERSION
|
8
|
+
spec.platform = Gem::Platform::RUBY
|
9
|
+
spec.author = 'AppFolio'
|
10
|
+
spec.email = 'opensource@appfolio.com'
|
11
|
+
spec.description = <<~MSG
|
12
12
|
ActiveRecord has always stored the base class in polymorphic _type columns when using STI. This can have non-trivial
|
13
13
|
performance implications in certain cases. This gem adds the 'store_base_sti_class' configuration option which
|
14
14
|
controls whether ActiveRecord will store the base class or the actual class. Defaults to true for backwards
|
15
15
|
compatibility.'
|
16
16
|
MSG
|
17
|
-
spec.summary
|
18
|
-
Modifies ActiveRecord
|
17
|
+
spec.summary = <<~MSG
|
18
|
+
Modifies ActiveRecord 6.1.x - 7.1.x with the ability to store the actual class (instead of the base class) in
|
19
19
|
polymorhic _type columns when using STI.
|
20
20
|
MSG
|
21
|
-
spec.homepage
|
22
|
-
spec.license
|
23
|
-
spec.files
|
24
|
-
spec.require_paths
|
25
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.3')
|
21
|
+
spec.homepage = 'https://github.com/appfolio/store_base_sti_class'
|
22
|
+
spec.license = 'MIT'
|
23
|
+
spec.files = Dir['**/*'].select { |f| f[%r{^(lib/|LICENSE.txt|.*gemspec)}] }
|
24
|
+
spec.require_paths = ['lib']
|
26
25
|
|
26
|
+
spec.required_ruby_version = Gem::Requirement.new('< 3.4')
|
27
27
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
28
28
|
|
29
|
-
spec.add_dependency('activerecord', ['>= 6', '< 7.
|
29
|
+
spec.add_dependency('activerecord', ['>= 6.1', '< 7.2'])
|
30
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: store_base_sti_class
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AppFolio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-29 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: '6'
|
19
|
+
version: '6.1'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '7.
|
22
|
+
version: '7.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: '6'
|
29
|
+
version: '6.1'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '7.
|
32
|
+
version: '7.2'
|
33
33
|
description: |
|
34
34
|
ActiveRecord has always stored the base class in polymorphic _type columns when using STI. This can have non-trivial
|
35
35
|
performance implications in certain cases. This gem adds the 'store_base_sti_class' configuration option which
|
@@ -43,9 +43,6 @@ files:
|
|
43
43
|
- LICENSE.txt
|
44
44
|
- lib/store_base_sti_class.rb
|
45
45
|
- lib/store_base_sti_class/version.rb
|
46
|
-
- lib/store_base_sti_class_for_6_0.rb
|
47
|
-
- lib/store_base_sti_class_for_6_1.rb
|
48
|
-
- lib/store_base_sti_class_for_7_0.rb
|
49
46
|
- store_base_sti_class.gemspec
|
50
47
|
homepage: https://github.com/appfolio/store_base_sti_class
|
51
48
|
licenses:
|
@@ -58,18 +55,18 @@ require_paths:
|
|
58
55
|
- lib
|
59
56
|
required_ruby_version: !ruby/object:Gem::Requirement
|
60
57
|
requirements:
|
61
|
-
- - "
|
58
|
+
- - "<"
|
62
59
|
- !ruby/object:Gem::Version
|
63
|
-
version:
|
60
|
+
version: '3.4'
|
64
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
62
|
requirements:
|
66
63
|
- - ">="
|
67
64
|
- !ruby/object:Gem::Version
|
68
65
|
version: '0'
|
69
66
|
requirements: []
|
70
|
-
rubygems_version: 3.
|
67
|
+
rubygems_version: 3.5.3
|
71
68
|
signing_key:
|
72
69
|
specification_version: 4
|
73
|
-
summary: Modifies ActiveRecord
|
70
|
+
summary: Modifies ActiveRecord 6.1.x - 7.1.x with the ability to store the actual
|
74
71
|
class (instead of the base class) in polymorhic _type columns when using STI.
|
75
72
|
test_files: []
|
@@ -1,153 +0,0 @@
|
|
1
|
-
require 'active_record/associations/join_dependency/join_part'
|
2
|
-
|
3
|
-
if ActiveRecord::VERSION::STRING =~ /^6\.0/
|
4
|
-
module ActiveRecord
|
5
|
-
|
6
|
-
class Base
|
7
|
-
class_attribute :store_base_sti_class
|
8
|
-
self.store_base_sti_class = true
|
9
|
-
end
|
10
|
-
|
11
|
-
module Inheritance
|
12
|
-
module ClassMethods
|
13
|
-
def polymorphic_name
|
14
|
-
ActiveRecord::Base.store_base_sti_class ? base_class.name : name
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module Associations
|
20
|
-
class Preloader
|
21
|
-
class ThroughAssociation < Association
|
22
|
-
private
|
23
|
-
|
24
|
-
def through_scope
|
25
|
-
scope = through_reflection.klass.unscoped
|
26
|
-
options = reflection.options
|
27
|
-
|
28
|
-
if options[:source_type]
|
29
|
-
|
30
|
-
# BEGIN PATCH
|
31
|
-
# original:
|
32
|
-
# scope.where! reflection.foreign_type => options[:source_type]
|
33
|
-
|
34
|
-
adjusted_foreign_type =
|
35
|
-
if ActiveRecord::Base.store_base_sti_class
|
36
|
-
options[:source_type]
|
37
|
-
else
|
38
|
-
([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
|
39
|
-
end
|
40
|
-
|
41
|
-
scope.where! reflection.foreign_type => adjusted_foreign_type
|
42
|
-
# END PATCH
|
43
|
-
|
44
|
-
elsif !reflection_scope.where_clause.empty?
|
45
|
-
scope.where_clause = reflection_scope.where_clause
|
46
|
-
values = reflection_scope.values
|
47
|
-
|
48
|
-
if includes = values[:includes]
|
49
|
-
scope.includes!(source_reflection.name => includes)
|
50
|
-
else
|
51
|
-
scope.includes!(source_reflection.name)
|
52
|
-
end
|
53
|
-
|
54
|
-
if values[:references] && !values[:references].empty?
|
55
|
-
scope.references!(values[:references])
|
56
|
-
else
|
57
|
-
scope.references!(source_reflection.table_name)
|
58
|
-
end
|
59
|
-
|
60
|
-
if joins = values[:joins]
|
61
|
-
scope.joins!(source_reflection.name => joins)
|
62
|
-
end
|
63
|
-
|
64
|
-
if left_outer_joins = values[:left_outer_joins]
|
65
|
-
scope.left_outer_joins!(source_reflection.name => left_outer_joins)
|
66
|
-
end
|
67
|
-
|
68
|
-
if scope.eager_loading? && order_values = values[:order]
|
69
|
-
scope = scope.order(order_values)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
scope unless scope.empty_scope?
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
class AssociationScope
|
79
|
-
private
|
80
|
-
|
81
|
-
def next_chain_scope(scope, reflection, next_reflection)
|
82
|
-
join_keys = reflection.join_keys
|
83
|
-
key = join_keys.key
|
84
|
-
foreign_key = join_keys.foreign_key
|
85
|
-
|
86
|
-
table = reflection.aliased_table
|
87
|
-
foreign_table = next_reflection.aliased_table
|
88
|
-
constraint = table[key].eq(foreign_table[foreign_key])
|
89
|
-
|
90
|
-
if reflection.type
|
91
|
-
# BEGIN PATCH
|
92
|
-
# original:
|
93
|
-
# value = transform_value(next_reflection.klass.polymorphic_name)
|
94
|
-
# scope = apply_scope(scope, table, reflection.type, value)
|
95
|
-
if ActiveRecord::Base.store_base_sti_class
|
96
|
-
value = transform_value(next_reflection.klass.polymorphic_name)
|
97
|
-
else
|
98
|
-
klass = next_reflection.klass
|
99
|
-
value = ([klass] + klass.descendants).map(&:name)
|
100
|
-
end
|
101
|
-
scope = apply_scope(scope, table, reflection.type, value)
|
102
|
-
# END PATCH
|
103
|
-
end
|
104
|
-
|
105
|
-
scope.joins!(join(foreign_table, constraint))
|
106
|
-
end
|
107
|
-
|
108
|
-
end
|
109
|
-
|
110
|
-
class HasManyThroughAssociation
|
111
|
-
private
|
112
|
-
|
113
|
-
def build_through_record(record)
|
114
|
-
@through_records[record.object_id] ||= begin
|
115
|
-
ensure_mutable
|
116
|
-
|
117
|
-
attributes = through_scope_attributes
|
118
|
-
attributes[source_reflection.name] = record
|
119
|
-
|
120
|
-
# START PATCH
|
121
|
-
if ActiveRecord::Base.store_base_sti_class
|
122
|
-
attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
|
123
|
-
end
|
124
|
-
# END PATCH
|
125
|
-
|
126
|
-
through_association.build(attributes)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
module Reflection
|
133
|
-
class PolymorphicReflection
|
134
|
-
def source_type_scope
|
135
|
-
type = @previous_reflection.foreign_type
|
136
|
-
source_type = @previous_reflection.options[:source_type]
|
137
|
-
|
138
|
-
# START PATCH
|
139
|
-
adjusted_source_type =
|
140
|
-
if ActiveRecord::Base.store_base_sti_class
|
141
|
-
source_type
|
142
|
-
else
|
143
|
-
([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
|
144
|
-
end
|
145
|
-
# END PATCH
|
146
|
-
|
147
|
-
lambda { |object| where(type => adjusted_source_type) }
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
end
|
@@ -1,156 +0,0 @@
|
|
1
|
-
require 'active_record/associations/join_dependency/join_part'
|
2
|
-
|
3
|
-
if ActiveRecord::VERSION::STRING =~ /^6\.1/
|
4
|
-
module ActiveRecord
|
5
|
-
|
6
|
-
class Base
|
7
|
-
class_attribute :store_base_sti_class
|
8
|
-
self.store_base_sti_class = true
|
9
|
-
end
|
10
|
-
|
11
|
-
module Inheritance
|
12
|
-
module ClassMethods
|
13
|
-
def polymorphic_name
|
14
|
-
ActiveRecord::Base.store_base_sti_class ? base_class.name : name
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module Associations
|
20
|
-
class Preloader
|
21
|
-
class ThroughAssociation < Association
|
22
|
-
private
|
23
|
-
|
24
|
-
def through_scope
|
25
|
-
scope = through_reflection.klass.unscoped
|
26
|
-
options = reflection.options
|
27
|
-
|
28
|
-
values = reflection_scope.values
|
29
|
-
if annotations = values[:annotate]
|
30
|
-
scope.annotate!(*annotations)
|
31
|
-
end
|
32
|
-
|
33
|
-
if options[:source_type]
|
34
|
-
# BEGIN PATCH
|
35
|
-
# original:
|
36
|
-
# scope.where! reflection.foreign_type => options[:source_type]
|
37
|
-
|
38
|
-
adjusted_foreign_type =
|
39
|
-
if ActiveRecord::Base.store_base_sti_class
|
40
|
-
options[:source_type]
|
41
|
-
else
|
42
|
-
([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
|
43
|
-
end
|
44
|
-
|
45
|
-
scope.where! reflection.foreign_type => adjusted_foreign_type
|
46
|
-
# END PATCH
|
47
|
-
|
48
|
-
elsif !reflection_scope.where_clause.empty?
|
49
|
-
scope.where_clause = reflection_scope.where_clause
|
50
|
-
|
51
|
-
if includes = values[:includes]
|
52
|
-
scope.includes!(source_reflection.name => includes)
|
53
|
-
else
|
54
|
-
scope.includes!(source_reflection.name)
|
55
|
-
end
|
56
|
-
|
57
|
-
if values[:references] && !values[:references].empty?
|
58
|
-
scope.references_values |= values[:references]
|
59
|
-
else
|
60
|
-
scope.references!(source_reflection.table_name)
|
61
|
-
end
|
62
|
-
|
63
|
-
if joins = values[:joins]
|
64
|
-
scope.joins!(source_reflection.name => joins)
|
65
|
-
end
|
66
|
-
|
67
|
-
if left_outer_joins = values[:left_outer_joins]
|
68
|
-
scope.left_outer_joins!(source_reflection.name => left_outer_joins)
|
69
|
-
end
|
70
|
-
|
71
|
-
if scope.eager_loading? && order_values = values[:order]
|
72
|
-
scope = scope.order(order_values)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
scope
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
class AssociationScope
|
82
|
-
private
|
83
|
-
|
84
|
-
def next_chain_scope(scope, reflection, next_reflection)
|
85
|
-
primary_key = reflection.join_primary_key
|
86
|
-
foreign_key = reflection.join_foreign_key
|
87
|
-
|
88
|
-
table = reflection.aliased_table
|
89
|
-
foreign_table = next_reflection.aliased_table
|
90
|
-
constraint = table[primary_key].eq(foreign_table[foreign_key])
|
91
|
-
|
92
|
-
if reflection.type
|
93
|
-
# BEGIN PATCH
|
94
|
-
# original:
|
95
|
-
# value = transform_value(next_reflection.klass.polymorphic_name)
|
96
|
-
# scope = apply_scope(scope, table, reflection.type, value)
|
97
|
-
|
98
|
-
if ActiveRecord::Base.store_base_sti_class
|
99
|
-
value = transform_value(next_reflection.klass.polymorphic_name)
|
100
|
-
else
|
101
|
-
klass = next_reflection.klass
|
102
|
-
value = ([klass] + klass.descendants).map(&:name)
|
103
|
-
end
|
104
|
-
scope = apply_scope(scope, table, reflection.type, value)
|
105
|
-
# END PATCH
|
106
|
-
end
|
107
|
-
|
108
|
-
scope.joins!(join(foreign_table, constraint))
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
class HasManyThroughAssociation
|
114
|
-
private
|
115
|
-
|
116
|
-
def build_through_record(record)
|
117
|
-
@through_records[record.object_id] ||= begin
|
118
|
-
ensure_mutable
|
119
|
-
|
120
|
-
attributes = through_scope_attributes
|
121
|
-
attributes[source_reflection.name] = record
|
122
|
-
|
123
|
-
# START PATCH
|
124
|
-
if ActiveRecord::Base.store_base_sti_class
|
125
|
-
attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
|
126
|
-
end
|
127
|
-
# END PATCH
|
128
|
-
|
129
|
-
through_association.build(attributes)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
module Reflection
|
136
|
-
class PolymorphicReflection
|
137
|
-
def source_type_scope
|
138
|
-
type = @previous_reflection.foreign_type
|
139
|
-
source_type = @previous_reflection.options[:source_type]
|
140
|
-
|
141
|
-
# START PATCH
|
142
|
-
adjusted_source_type =
|
143
|
-
if ActiveRecord::Base.store_base_sti_class
|
144
|
-
source_type
|
145
|
-
else
|
146
|
-
([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
|
147
|
-
end
|
148
|
-
# END PATCH
|
149
|
-
|
150
|
-
lambda { |object| where(type => adjusted_source_type) }
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
end
|
@@ -1,156 +0,0 @@
|
|
1
|
-
require 'active_record/associations/join_dependency/join_part'
|
2
|
-
|
3
|
-
if ActiveRecord::VERSION::STRING =~ /\A7\.0/
|
4
|
-
module ActiveRecord
|
5
|
-
|
6
|
-
class Base
|
7
|
-
class_attribute :store_base_sti_class
|
8
|
-
self.store_base_sti_class = true
|
9
|
-
end
|
10
|
-
|
11
|
-
module Inheritance
|
12
|
-
module ClassMethods
|
13
|
-
def polymorphic_name
|
14
|
-
ActiveRecord::Base.store_base_sti_class ? base_class.name : name
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module Associations
|
20
|
-
class Preloader
|
21
|
-
class ThroughAssociation < Association
|
22
|
-
private
|
23
|
-
|
24
|
-
def through_scope
|
25
|
-
scope = through_reflection.klass.unscoped
|
26
|
-
options = reflection.options
|
27
|
-
|
28
|
-
values = reflection_scope.values
|
29
|
-
if annotations = values[:annotate]
|
30
|
-
scope.annotate!(*annotations)
|
31
|
-
end
|
32
|
-
|
33
|
-
if options[:source_type]
|
34
|
-
# BEGIN PATCH
|
35
|
-
# original:
|
36
|
-
# scope.where! reflection.foreign_type => options[:source_type]
|
37
|
-
|
38
|
-
adjusted_foreign_type =
|
39
|
-
if ActiveRecord::Base.store_base_sti_class
|
40
|
-
options[:source_type]
|
41
|
-
else
|
42
|
-
([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
|
43
|
-
end
|
44
|
-
|
45
|
-
scope.where! reflection.foreign_type => adjusted_foreign_type
|
46
|
-
# END PATCH
|
47
|
-
|
48
|
-
elsif !reflection_scope.where_clause.empty?
|
49
|
-
scope.where_clause = reflection_scope.where_clause
|
50
|
-
|
51
|
-
if includes = values[:includes]
|
52
|
-
scope.includes!(source_reflection.name => includes)
|
53
|
-
else
|
54
|
-
scope.includes!(source_reflection.name)
|
55
|
-
end
|
56
|
-
|
57
|
-
if values[:references] && !values[:references].empty?
|
58
|
-
scope.references_values |= values[:references]
|
59
|
-
else
|
60
|
-
scope.references!(source_reflection.table_name)
|
61
|
-
end
|
62
|
-
|
63
|
-
if joins = values[:joins]
|
64
|
-
scope.joins!(source_reflection.name => joins)
|
65
|
-
end
|
66
|
-
|
67
|
-
if left_outer_joins = values[:left_outer_joins]
|
68
|
-
scope.left_outer_joins!(source_reflection.name => left_outer_joins)
|
69
|
-
end
|
70
|
-
|
71
|
-
if scope.eager_loading? && order_values = values[:order]
|
72
|
-
scope = scope.order(order_values)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
scope
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
class AssociationScope
|
82
|
-
private
|
83
|
-
|
84
|
-
def next_chain_scope(scope, reflection, next_reflection)
|
85
|
-
primary_key = reflection.join_primary_key
|
86
|
-
foreign_key = reflection.join_foreign_key
|
87
|
-
|
88
|
-
table = reflection.aliased_table
|
89
|
-
foreign_table = next_reflection.aliased_table
|
90
|
-
constraint = table[primary_key].eq(foreign_table[foreign_key])
|
91
|
-
|
92
|
-
if reflection.type
|
93
|
-
# BEGIN PATCH
|
94
|
-
# original:
|
95
|
-
# value = transform_value(next_reflection.klass.polymorphic_name)
|
96
|
-
# scope = apply_scope(scope, table, reflection.type, value)
|
97
|
-
|
98
|
-
if ActiveRecord::Base.store_base_sti_class
|
99
|
-
value = transform_value(next_reflection.klass.polymorphic_name)
|
100
|
-
else
|
101
|
-
klass = next_reflection.klass
|
102
|
-
value = ([klass] + klass.descendants).map(&:name)
|
103
|
-
end
|
104
|
-
scope = apply_scope(scope, table, reflection.type, value)
|
105
|
-
# END PATCH
|
106
|
-
end
|
107
|
-
|
108
|
-
scope.joins!(join(foreign_table, constraint))
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
|
-
class HasManyThroughAssociation
|
114
|
-
private
|
115
|
-
|
116
|
-
def build_through_record(record)
|
117
|
-
@through_records[record.object_id] ||= begin
|
118
|
-
ensure_mutable
|
119
|
-
|
120
|
-
attributes = through_scope_attributes
|
121
|
-
attributes[source_reflection.name] = record
|
122
|
-
|
123
|
-
# START PATCH
|
124
|
-
if ActiveRecord::Base.store_base_sti_class
|
125
|
-
attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]
|
126
|
-
end
|
127
|
-
# END PATCH
|
128
|
-
|
129
|
-
through_association.build(attributes)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
module Reflection
|
136
|
-
class PolymorphicReflection
|
137
|
-
def source_type_scope
|
138
|
-
type = @previous_reflection.foreign_type
|
139
|
-
source_type = @previous_reflection.options[:source_type]
|
140
|
-
|
141
|
-
# START PATCH
|
142
|
-
adjusted_source_type =
|
143
|
-
if ActiveRecord::Base.store_base_sti_class
|
144
|
-
source_type
|
145
|
-
else
|
146
|
-
([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
|
147
|
-
end
|
148
|
-
# END PATCH
|
149
|
-
|
150
|
-
lambda { |object| where(type => adjusted_source_type) }
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
end
|