@@ -43,6 +43,8 @@ module Inheritance
43
43
# Determines whether to store the full constant name including namespace when using STI.
44
44
# This is true, by default.
45
45
class_attribute :store_full_sti_class , instance_writer : false , default : true
46
+
47
+ set_base_class
46
48
end
47
49
48
50
module ClassMethods
@@ -98,17 +100,7 @@ def finder_needs_type_condition? #:nodoc:
98
100
#
99
101
# If B < A and C < B and if A is an abstract_class then both B.base_class
100
102
# and C.base_class would return B as the answer since A is an abstract_class.
101
- def base_class
102
- unless self < Base
103
- raise ActiveRecordError , "#{ name } doesn't belong in a hierarchy descending from ActiveRecord"
104
- end
105
-
106
- if superclass == Base || superclass . abstract_class?
107
- self
108
- else
109
- superclass . base_class
110
- end
111
- end
103
+ attr_reader :base_class
112
104
113
105
# Returns whether the class is a base class.
114
106
# See #base_class for more information.
@@ -218,10 +210,24 @@ def polymorphic_class_for(name)
218
210
end
219
211
220
212
def inherited ( subclass )
213
+ subclass . set_base_class
221
214
subclass . instance_variable_set ( :@_type_candidates_cache , Concurrent ::Map . new )
222
215
super
223
216
end
224
217
218
+ def dup # :nodoc:
219
+ # `initialize_dup` / `initialize_copy` don't work when defined
220
+ # in the `singleton_class`.
221
+ other = super
222
+ other . set_base_class
223
+ other
224
+ end
225
+
226
+ def initialize_clone ( other ) # :nodoc:
227
+ super
228
+ set_base_class
229
+ end
230
+
225
231
protected
226
232
# Returns the class type of the record using the current module as a prefix. So descendants of
227
233
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
@@ -253,6 +259,24 @@ def compute_type(type_name)
253
259
end
254
260
end
255
261
262
+ def set_base_class # :nodoc:
263
+ @base_class = begin
264
+ if self == Base
265
+ self
266
+ else
267
+ unless self < Base
268
+ raise ActiveRecordError , "#{ name } doesn't belong in a hierarchy descending from ActiveRecord"
269
+ end
270
+
271
+ if superclass == Base || superclass . abstract_class?
272
+ self
273
+ else
274
+ superclass . base_class
275
+ end
276
+ end
277
+ end
278
+ end
279
+
256
280
private
257
281
# Called by +instantiate+ to decide which class to use for a new
258
282
# record instance. For single-table inheritance, we check the record
0 commit comments