From bcbce90b1b0a6a9a50bd8236d968d88ae6977af7 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 25 Aug 2006 15:25:08 +0000 Subject: [PATCH] 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 --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 23 ++++++++++++++++------- activerecord/test/base_test.rb | 26 +++++++++++++++++++++----- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d326057d33..f6cf49f35a 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d98629e289..346f6549f1 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 8fe57c35b9..b917e43278 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -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