aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-01-23 04:19:16 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-01-23 04:19:16 +0000
commit06afb8c7465e6b01833edfbae70547cf03c06480 (patch)
treee6c67cb67b2e84c80864771ba1aa22d546e70a55
parent7359dc0028b87f948d188f5fa6bb366a49181a81 (diff)
downloadrails-06afb8c7465e6b01833edfbae70547cf03c06480.tar.gz
rails-06afb8c7465e6b01833edfbae70547cf03c06480.tar.bz2
rails-06afb8c7465e6b01833edfbae70547cf03c06480.zip
Subclasses of an abstract class work with single-table inheritance. References #5704, closes #7284.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6013 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/base.rb9
-rwxr-xr-xactiverecord/test/fixtures/company.rb6
-rwxr-xr-xactiverecord/test/inheritance_test.rb7
4 files changed, 21 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index de3a777920..d3d0b3d510 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Subclasses of an abstract class work with single-table inheritance. #5704, #7284 [BertG, nick+rails@ag.arizona.edu]
+
* Make sure sqlite3 driver closes open connections on disconnect [Rob Rasmussen]
* [DOC] clear up some ambiguity with the way has_and_belongs_to_many creates the default join table name. #7072 [jeremymcanally]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index a783feafed..b8a497ffa3 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -817,8 +817,13 @@ module ActiveRecord #:nodoc:
attribute_key_name.humanize
end
- def descends_from_active_record? # :nodoc:
- superclass == Base || !columns_hash.include?(inheritance_column)
+ # True if this isn't a concrete subclass needing a STI type condition.
+ def descends_from_active_record?
+ if superclass.abstract_class?
+ superclass.descends_from_active_record?
+ else
+ superclass == Base || !columns_hash.include?(inheritance_column)
+ end
end
diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb
index c012a978a0..ff92b14fcb 100755
--- a/activerecord/test/fixtures/company.rb
+++ b/activerecord/test/fixtures/company.rb
@@ -1,4 +1,8 @@
-class Company < ActiveRecord::Base
+class AbstractCompany < ActiveRecord::Base
+ self.abstract_class = true
+end
+
+class Company < AbstractCompany
attr_protected :rating
set_sequence_name :companies_nonstd_seq
diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb
index 8b0d70cfa3..7ab1d91061 100755
--- a/activerecord/test/inheritance_test.rb
+++ b/activerecord/test/inheritance_test.rb
@@ -6,6 +6,13 @@ require 'fixtures/subscriber'
class InheritanceTest < Test::Unit::TestCase
fixtures :companies, :projects, :subscribers, :accounts
+ def test_company_descends_from_active_record
+ assert_raise(NoMethodError) { ActiveRecord::Base.descends_from_active_record? }
+ assert AbstractCompany.descends_from_active_record?, 'AbstractCompany should descend from ActiveRecord::Base'
+ assert Company.descends_from_active_record?, 'Company should descend from ActiveRecord::Base'
+ assert !Class.new(Company).descends_from_active_record?, 'Company subclass should not descend from ActiveRecord::Base'
+ end
+
def test_a_bad_type_column
#SQLServer need to turn Identity Insert On before manually inserting into the Identity column
if current_adapter?(:SQLServerAdapter, :SybaseAdapter)