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
This commit is contained in:
Jamis Buck
2006-01-20 20:43:40 +00:00
parent e3898491f2
commit d2f47503f8
3 changed files with 24 additions and 5 deletions

View File

@@ -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]

View File

@@ -882,6 +882,13 @@ module ActiveRecord #:nodoc:
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 @@ module ActiveRecord #:nodoc:
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'"

View File

@@ -1112,7 +1112,12 @@ class BasicsTest < Test::Unit::TestCase
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.
#