Permalink
Browse files

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
  • Loading branch information...
1 parent 80f1597 commit 14101c7b40c2e04303cb497eb60996add2de645f @jeremy jeremy committed Aug 16, 2006
Showing with 31 additions and 10 deletions.
  1. +3 −0 activerecord/CHANGELOG
  2. +20 −6 activerecord/lib/active_record/base.rb
  3. +8 −4 activerecord/test/base_test.rb
@@ -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]
@@ -588,21 +588,35 @@ def serialized_attributes
# 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
@@ -13,7 +13,9 @@
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 @@ def test_table_name_guesses
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 @@ def test_table_name_guesses
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

0 comments on commit 14101c7

Please sign in to comment.