store_base_sti_class 2.0.3 → 3.1.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.
@@ -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