@@ -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,6 +210,7 @@ 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
@@ -253,6 +246,24 @@ def compute_type(type_name)
253
246
end
254
247
end
255
248
249
+ def set_base_class # :nodoc:
250
+ @base_class = begin
251
+ if self == Base
252
+ self
253
+ else
254
+ unless self < Base
255
+ raise ActiveRecordError , "#{ name } doesn't belong in a hierarchy descending from ActiveRecord"
256
+ end
257
+
258
+ if superclass == Base || superclass . abstract_class?
259
+ self
260
+ else
261
+ superclass . base_class
262
+ end
263
+ end
264
+ end
265
+ end
266
+
256
267
private
257
268
# Called by +instantiate+ to decide which class to use for a new
258
269
# record instance. For single-table inheritance, we check the record
0 commit comments