mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Nested subclasses are not prefixed with the parent class' table_name since they should always use the base class' table_name. Closes #5911.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4819 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
c064802de9
commit
bcbce90b1b
3 changed files with 39 additions and 12 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Nested subclasses are not prefixed with the parent class' table_name since they should always use the base class' table_name. #5911 [Jonathan Viney]
|
||||
|
||||
* SQLServer: work around bug where some unambiguous date formats are not correctly identified if the session language is set to german. #5894 [Tom Ward, kruth@bfpi]
|
||||
|
||||
* SQLServer: fix eager association test. #5901 [Tom Ward]
|
||||
|
|
|
@ -610,13 +610,22 @@ module ActiveRecord #:nodoc:
|
|||
end
|
||||
|
||||
def reset_table_name #:nodoc:
|
||||
# If this is a nested class, prefix with singular parent table name.
|
||||
if parent < ActiveRecord::Base && !parent.abstract_class?
|
||||
contained = parent.table_name
|
||||
contained = contained.singularize if parent.pluralize_table_names
|
||||
contained << '_'
|
||||
end
|
||||
name = "#{table_name_prefix}#{contained}#{undecorated_table_name(base_class.name)}#{table_name_suffix}"
|
||||
base = base_class
|
||||
|
||||
name =
|
||||
# STI subclasses always use their superclass' table.
|
||||
unless self == base
|
||||
base.table_name
|
||||
else
|
||||
# Nested classes are prefixed with singular parent table name.
|
||||
if parent < ActiveRecord::Base && !parent.abstract_class?
|
||||
contained = parent.table_name
|
||||
contained = contained.singularize if parent.pluralize_table_names
|
||||
contained << '_'
|
||||
end
|
||||
name = "#{table_name_prefix}#{contained}#{undecorated_table_name(base.name)}#{table_name_suffix}"
|
||||
end
|
||||
|
||||
set_table_name(name)
|
||||
name
|
||||
end
|
||||
|
|
|
@ -14,7 +14,12 @@ require 'fixtures/keyboard'
|
|||
class Category < ActiveRecord::Base; end
|
||||
class Smarts < ActiveRecord::Base; end
|
||||
class CreditCard < ActiveRecord::Base
|
||||
class PinNumber < ActiveRecord::Base; end
|
||||
class PinNumber < ActiveRecord::Base
|
||||
class CvvCode < ActiveRecord::Base; end
|
||||
class SubCvvCode < CvvCode; end
|
||||
end
|
||||
class SubPinNumber < PinNumber; end
|
||||
class Brand < Category; end
|
||||
end
|
||||
class MasterCreditCard < ActiveRecord::Base; end
|
||||
class Post < ActiveRecord::Base; end
|
||||
|
@ -369,23 +374,33 @@ class BasicsTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_table_name_guesses
|
||||
classes = [Category, Smarts, CreditCard, CreditCard::PinNumber, CreditCard::PinNumber::CvvCode, CreditCard::SubPinNumber, CreditCard::Brand, MasterCreditCard]
|
||||
|
||||
assert_equal "topics", Topic.table_name
|
||||
|
||||
|
||||
assert_equal "categories", Category.table_name
|
||||
assert_equal "smarts", Smarts.table_name
|
||||
assert_equal "credit_cards", CreditCard.table_name
|
||||
assert_equal "credit_card_pin_numbers", CreditCard::PinNumber.table_name
|
||||
assert_equal "credit_card_pin_number_cvv_codes", CreditCard::PinNumber::CvvCode.table_name
|
||||
assert_equal "credit_card_pin_numbers", CreditCard::SubPinNumber.table_name
|
||||
assert_equal "categories", CreditCard::Brand.table_name
|
||||
assert_equal "master_credit_cards", MasterCreditCard.table_name
|
||||
|
||||
ActiveRecord::Base.pluralize_table_names = false
|
||||
[Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name}
|
||||
classes.each(&:reset_table_name)
|
||||
|
||||
assert_equal "category", Category.table_name
|
||||
assert_equal "smarts", Smarts.table_name
|
||||
assert_equal "credit_card", CreditCard.table_name
|
||||
assert_equal "credit_card_pin_number", CreditCard::PinNumber.table_name
|
||||
assert_equal "credit_card_pin_number_cvv_code", CreditCard::PinNumber::CvvCode.table_name
|
||||
assert_equal "credit_card_pin_number", CreditCard::SubPinNumber.table_name
|
||||
assert_equal "category", CreditCard::Brand.table_name
|
||||
assert_equal "master_credit_card", MasterCreditCard.table_name
|
||||
|
||||
ActiveRecord::Base.pluralize_table_names = true
|
||||
[Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name}
|
||||
classes.each(&:reset_table_name)
|
||||
|
||||
ActiveRecord::Base.table_name_prefix = "test_"
|
||||
Category.reset_table_name
|
||||
|
@ -413,8 +428,9 @@ class BasicsTest < Test::Unit::TestCase
|
|||
ActiveRecord::Base.table_name_suffix = ""
|
||||
Category.reset_table_name
|
||||
assert_equal "category", Category.table_name
|
||||
|
||||
ActiveRecord::Base.pluralize_table_names = true
|
||||
[Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name}
|
||||
classes.each(&:reset_table_name)
|
||||
end
|
||||
|
||||
def test_destroy_all
|
||||
|
|
Loading…
Reference in a new issue