diff options
Diffstat (limited to 'activerecord/lib/active_record/inheritance.rb')
-rw-r--r-- | activerecord/lib/active_record/inheritance.rb | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb index de9461982a..ec57151d40 100644 --- a/activerecord/lib/active_record/inheritance.rb +++ b/activerecord/lib/active_record/inheritance.rb @@ -13,10 +13,12 @@ module ActiveRecord module ClassMethods # True if this isn't a concrete subclass needing a STI type condition. def descends_from_active_record? - if superclass.abstract_class? - superclass.descends_from_active_record? + sup = active_record_super + + if sup.abstract_class? + sup.descends_from_active_record? else - superclass == Base || !columns_hash.include?(inheritance_column) + sup == Base || !columns_hash.include?(inheritance_column) end end @@ -79,17 +81,34 @@ module ActiveRecord instance end + # If this class includes ActiveRecord::Model then it won't have a + # superclass. So this provides a way to get to the 'root' (ActiveRecord::Base), + # through inheritance hierarchy, ending in Base, whether or not that is + # actually an ancestor of the class. + # + # Mainly for internal use. + def active_record_super #:nodoc: + if self == Base || superclass && superclass < Model::Tag + superclass + else + Base + end + end + protected # Returns the class descending directly from ActiveRecord::Base or an # abstract class, if any, in the inheritance hierarchy. def class_of_active_record_descendant(klass) - if klass == Base || klass.superclass == Base || klass.superclass.abstract_class? - klass - elsif klass.superclass.nil? + unless klass < Model::Tag raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord" + end + + sup = klass.active_record_super + if klass == Base || sup == Base || sup.abstract_class? + klass else - class_of_active_record_descendant(klass.superclass) + class_of_active_record_descendant(sup) end end |