Permalink
Browse files

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
  • Loading branch information...
1 parent c064802 commit bcbce90b1b0a6a9a50bd8236d968d88ae6977af7 @jeremy jeremy committed Aug 25, 2006
Showing with 39 additions and 12 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +16 −7 activerecord/lib/active_record/base.rb
  3. +21 −5 activerecord/test/base_test.rb
View
@@ -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 @@ def table_name
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 @@
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 @@ def test_load_with_condition
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 @@ def test_table_name_guesses
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

0 comments on commit bcbce90

Please sign in to comment.