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
This commit is contained in:
Jeremy Kemper 2006-08-16 09:46:43 +00:00
parent 80f1597942
commit 14101c7b40
3 changed files with 31 additions and 10 deletions

View File

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

View File

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

View File

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