Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4770

Show
Ignore:
Timestamp:
08/16/06 09:46:43 (2 years ago)
Author:
bitsweat
Message:

Nested classes are given table names prefixed by the singular form of the parent's table name.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4767 r4770  
    11*SVN* 
     2 
     3* Nested classes are given table names prefixed by the singular form of the parent's table name. [Jeremy Kemper] 
     4    Example: Invoice::Lineitem is given table name invoice_lineitems 
    25 
    36* Migrations: uniquely name multicolumn indexes so you don't have to. [Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/base.rb

    r4743 r4770  
    589589      # in Active Support, which knows almost all common English inflections (report a bug if your inflection isn't covered). 
    590590      # 
    591       # Additionally, the class-level table_name_prefix is prepended to the table_name and the table_name_suffix is appended. 
    592       # So if you have "myapp_" as a prefix, the table name guess for an Account class becomes "myapp_accounts". 
    593       # 
    594       # You can also overwrite this class method to allow for unguessable links, such as a Mouse class with a link to a 
    595       # "mice" table. Example: 
     591      # Nested classes are given table names prefixed by the singular form of 
     592      # the parent's table name. Example: 
     593      #   file                  class               table_name 
     594      #   invoice.rb            Invoice             invoices 
     595      #   invoice/lineitem.rb   Invoice::Lineitem   invoice_lineitems 
     596      # 
     597      # Additionally, the class-level table_name_prefix is prepended and the 
     598      # table_name_suffix is appended.  So if you have "myapp_" as a prefix, 
     599      # the table name guess for an Invoice class becomes "myapp_invoices". 
     600      # Invoice::Lineitem becomes "myapp_invoice_lineitems". 
     601      # 
     602      # You can also overwrite this class method to allow for unguessable 
     603      # links, such as a Mouse class with a link to a "mice" table. Example: 
    596604      # 
    597605      #   class Mouse < ActiveRecord::Base 
    598       #      set_table_name "mice" 
     606      #     set_table_name "mice" 
    599607      #   end 
    600608      def table_name 
     
    603611 
    604612      def reset_table_name #:nodoc: 
    605         name = "#{table_name_prefix}#{undecorated_table_name(base_class.name)}#{table_name_suffix}" 
     613        # If this is a nested class, prefix with singular parent table name. 
     614        if parent < ActiveRecord::Base && !parent.abstract_class? 
     615          contained = parent.table_name 
     616          contained = contained.singularize if parent.pluralize_table_names 
     617          contained << '_' 
     618        end 
     619        name = "#{table_name_prefix}#{contained}#{undecorated_table_name(base_class.name)}#{table_name_suffix}" 
    606620        set_table_name(name) 
    607621        name 
  • trunk/activerecord/test/base_test.rb

    r4670 r4770  
    1414class Category < ActiveRecord::Base; end 
    1515class Smarts < ActiveRecord::Base; end 
    16 class CreditCard < ActiveRecord::Base; end 
     16class CreditCard < ActiveRecord::Base 
     17  class PinNumber < ActiveRecord::Base; end 
     18end 
    1719class MasterCreditCard < ActiveRecord::Base; end 
    1820class Post < ActiveRecord::Base; end 
     
    373375    assert_equal "smarts", Smarts.table_name 
    374376    assert_equal "credit_cards", CreditCard.table_name 
     377    assert_equal "credit_card_pin_numbers", CreditCard::PinNumber.table_name 
    375378    assert_equal "master_credit_cards", MasterCreditCard.table_name 
    376379 
    377380    ActiveRecord::Base.pluralize_table_names = false 
    378     [Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name} 
     381    [Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name} 
    379382    assert_equal "category", Category.table_name 
    380383    assert_equal "smarts", Smarts.table_name 
    381384    assert_equal "credit_card", CreditCard.table_name 
     385    assert_equal "credit_card_pin_number", CreditCard::PinNumber.table_name 
    382386    assert_equal "master_credit_card", MasterCreditCard.table_name 
    383387    ActiveRecord::Base.pluralize_table_names = true 
    384     [Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name} 
     388    [Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name} 
    385389 
    386390    ActiveRecord::Base.table_name_prefix = "test_" 
     
    411415    assert_equal "category", Category.table_name 
    412416    ActiveRecord::Base.pluralize_table_names = true 
    413     [Category, Smarts, CreditCard, MasterCreditCard].each{|c| c.reset_table_name} 
     417    [Category, Smarts, CreditCard, CreditCard::PinNumber, MasterCreditCard].each{|c| c.reset_table_name} 
    414418  end 
    415419