diff options
author | Jamis Buck <jamis@37signals.com> | 2006-01-20 20:43:40 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2006-01-20 20:43:40 +0000 |
commit | d2f47503f8890fe42310ada8ca9408ac5f268265 (patch) | |
tree | d1c7285079d79789fee8dc8edd4d9f860078f4b4 /activerecord/lib | |
parent | e3898491f2960890d866bfe08e221a4cb52cfccc (diff) | |
download | rails-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
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 20 |
1 files changed, 16 insertions, 4 deletions
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'" |