aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2006-01-20 20:43:40 +0000
committerJamis Buck <jamis@37signals.com>2006-01-20 20:43:40 +0000
commitd2f47503f8890fe42310ada8ca9408ac5f268265 (patch)
treed1c7285079d79789fee8dc8edd4d9f860078f4b4
parente3898491f2960890d866bfe08e221a4cb52cfccc (diff)
downloadrails-d2f47503f8890fe42310ada8ca9408ac5f268265.tar.gz
rails-d2f47503f8890fe42310ada8ca9408ac5f268265.tar.bz2
rails-d2f47503f8890fe42310ada8ca9408ac5f268265.zip
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
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb20
-rwxr-xr-xactiverecord/test/base_test.rb7
3 files changed, 24 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 50d22feb1c..3696d08e62 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -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]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 00f98d51d3..bfd9daf803 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -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'"
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 5c70768436..42fefa9001 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -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.
#