Permalink
Browse files

Add AR::Base.base_class for querying the ancestor AR::Base subclass […

…Jamis Buck]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3439 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent e389849 commit d2f47503f8890fe42310ada8ca9408ac5f268265 @jamis jamis committed Jan 20, 2006
Showing with 24 additions and 5 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +16 −4 activerecord/lib/active_record/base.rb
  3. +6 −1 activerecord/test/base_test.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Add AR::Base.base_class for querying the ancestor AR::Base subclass [Jamis Buck]
+
* Allow configuration of the column used for optimistic locking [wilsonb@gmail.com]
* Don't hardcode 'id' in acts as list. [ror@philippeapril.com]
@@ -882,6 +882,13 @@ def threaded_connections=(value)
self.allow_concurrency = value
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
+ # through some arbitrarily deep hierarchy, B.base_class will return A.
+ def base_class
+ class_of_active_record_descendant(self)
+ end
+
private
# Finder methods must instantiate through this method to work with the single-table inheritance model
@@ -1105,17 +1112,22 @@ def compute_type(type_name)
end
end
- # Returns the name of the class descending directly from ActiveRecord in the inheritance hierarchy.
- def class_name_of_active_record_descendant(klass)
+ # Returns the class descending directly from ActiveRecord in the inheritance hierarchy.
+ def class_of_active_record_descendant(klass)
if klass.superclass == Base
- klass.name
+ klass
elsif klass.superclass.nil?
raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
else
- class_name_of_active_record_descendant(klass.superclass)
+ class_of_active_record_descendant(klass.superclass)
end
end
+ # Returns the name of the class descending directly from ActiveRecord in the inheritance hierarchy.
+ def class_name_of_active_record_descendant(klass)
+ class_of_active_record_descendant(klass).name
+ end
+
# Accepts an array or string. The string is returned untouched, but the array has each value
# sanitized and interpolated into the sql statement.
# ["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'"
@@ -1112,7 +1112,12 @@ def test_scoped_find_limit_offset
developers = Developer.find(:all, :order => 'id')
assert_equal Developer.count, developers.size
end
-
+
+ def test_base_class
+ assert_equal LoosePerson, LoosePerson.base_class
+ assert_equal LoosePerson, LooseDescendant.base_class
+ end
+
# FIXME: this test ought to run, but it needs to run sandboxed so that it
# doesn't b0rk the current test environment by undefing everything.
#

0 comments on commit d2f4750

Please sign in to comment.