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
This commit is contained in:
Jeremy Kemper 2007-01-23 04:19:16 +00:00
parent 7359dc0028
commit 06afb8c746
4 changed files with 21 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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