From 14101c7b40c2e04303cb497eb60996add2de645f Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 16 Aug 2006 09:46:43 +0000 Subject: [PATCH] Nested classes are given table names prefixed by the singular form of the parent's table name. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4770 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 3 +++ activerecord/lib/active_record/base.rb | 26 ++++++++++++++++++++------ activerecord/test/base_test.rb | 12 ++++++++---- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index acfae5c82c..99949ff510 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,8 @@ *SVN* +* Nested classes are given table names prefixed by the singular form of the parent's table name. [Jeremy Kemper] + Example: Invoice::Lineitem is given table name invoice_lineitems + * Migrations: uniquely name multicolumn indexes so you don't have to. [Jeremy Kemper] # people_active_last_name_index, people_active_deactivated_at_index add_index :people, [:active, :last_name] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 4a0ef4c203..d3a8abd016 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -588,21 +588,35 @@ module ActiveRecord #:nodoc: # to guess the table name from even when called on Reply. The rules used to do the guess are handled by the Inflector class # in Active Support, which knows almost all common English inflections (report a bug if your inflection isn't covered). # - # Additionally, the class-level table_name_prefix is prepended to the table_name and the table_name_suffix is appended. - # So if you have "myapp_" as a prefix, the table name guess for an Account class becomes "myapp_accounts". + # Nested classes are given table names prefixed by the singular form of + # the parent's table name. Example: + # file class table_name + # invoice.rb Invoice invoices + # invoice/lineitem.rb Invoice::Lineitem invoice_lineitems # - # You can also overwrite this class method to allow for unguessable links, such as a Mouse class with a link to a - # "mice" table. Example: + # Additionally, the class-level table_name_prefix is prepended and the + # table_name_suffix is appended. So if you have "myapp_" as a prefix, + # the table name guess for an Invoice class becomes "myapp_invoices". + # Invoice::Lineitem becomes "myapp_invoice_lineitems". + # + # You can also overwrite this class method to allow for unguessable + # links, such as a Mouse class with a link to a "mice" table. Example: # # class Mouse < ActiveRecord::Base - # set_table_name "mice" + # set_table_name "mice" # end def table_name reset_table_name end def reset_table_name #:nodoc: - name = "#{table_name_prefix}#{undecorated_table_name(base_class.name)}#{table_name_suffix}" + # 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}" set_table_name(name) name end diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 9518e0aca8..f52253dd8b 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -13,7 +13,9 @@ require 'fixtures/keyboard' class Category < ActiveRecord::Base; end class Smarts < ActiveRecord::Base; end -class CreditCard < ActiveRecord::Base; end +class CreditCard < ActiveRecord::Base + class PinNumber < ActiveRecord::Base; end +end class MasterCreditCard < ActiveRecord::Base; end class Post < ActiveRecord::Base; end class Computer < ActiveRecord::Base; end @@ -372,16 +374,18 @@ class BasicsTest < Test::Unit::TestCase 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 "master_credit_cards", MasterCreditCard.table_name ActiveRecord::Base.pluralize_table_names = false - [Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name} + [Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.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 "master_credit_card", MasterCreditCard.table_name ActiveRecord::Base.pluralize_table_names = true - [Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name} + [Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name} ActiveRecord::Base.table_name_prefix = "test_" Category.reset_table_name @@ -410,7 +414,7 @@ class BasicsTest < Test::Unit::TestCase Category.reset_table_name assert_equal "category", Category.table_name ActiveRecord::Base.pluralize_table_names = true - [Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name} + [Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name} end def test_destroy_all