aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/inheritance.rb
diff options
context:
space:
mode:
authorbeerlington <pete@lette.us>2012-07-26 13:46:49 -0400
committerbeerlington <pete@lette.us>2012-07-26 13:46:49 -0400
commitd0aebd5357a5fcbb2f55963e4191e26458ad6d72 (patch)
treeea7ff5ed129c32e48b1b7423ec63f78247f9605b /activerecord/lib/active_record/inheritance.rb
parentb1d7283b2ac6763973e1a64475fc1c279cec8775 (diff)
downloadrails-d0aebd5357a5fcbb2f55963e4191e26458ad6d72.tar.gz
rails-d0aebd5357a5fcbb2f55963e4191e26458ad6d72.tar.bz2
rails-d0aebd5357a5fcbb2f55963e4191e26458ad6d72.zip
Refactor ActiveRecord::Inheritance.base_class logic
Moved logic from class_of_active_record_descendant(class) to the base_class method. This method was confusing because it required an argument, but that argument was 'self'. Moved base_class tests to inheritance_test.rb and added some test coverage for some untested cases.
Diffstat (limited to 'activerecord/lib/active_record/inheritance.rb')
-rw-r--r--activerecord/lib/active_record/inheritance.rb33
1 files changed, 15 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index 770083ac13..4a24024105 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -41,14 +41,26 @@ module ActiveRecord
@symbolized_sti_name ||= sti_name.present? ? sti_name.to_sym : symbolized_base_class
end
- # Returns the base AR subclass that this class descends from. If A
- # extends AR::Base, A.base_class will return A. If B descends from A
+ # Returns the class descending directly from ActiveRecord::Base (or
+ # that includes ActiveRecord::Model), or an abstract class, if any, in
+ # the inheritance hierarchy.
+ #
+ # If A extends AR::Base, A.base_class will return A. If B descends from A
# through some arbitrarily deep hierarchy, B.base_class will return A.
#
# If B < A and C < B and if A is an abstract_class then both B.base_class
# and C.base_class would return B as the answer since A is an abstract_class.
def base_class
- class_of_active_record_descendant(self)
+ unless self < Model::Tag
+ raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
+ end
+
+ sup = active_record_super
+ if sup.in?([Base, Model]) || sup.abstract_class?
+ self
+ else
+ sup.base_class
+ end
end
# Set this to true if this is an abstract class (see <tt>abstract_class?</tt>).
@@ -96,21 +108,6 @@ module ActiveRecord
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)
- unless klass < Model::Tag
- raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
- end
-
- sup = klass.active_record_super
- if [Base, Model].include?(klass) || [Base, Model].include?(sup) || sup.abstract_class?
- klass
- else
- class_of_active_record_descendant(sup)
- end
- end
-
# Returns the class type of the record using the current module as a prefix. So descendants of
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
def compute_type(type_name)