Merge pull request #1162 from guilleiguaran/singularize_individual_table_name

Singularize individual table name
This commit is contained in:
Jon Leighton 2011-05-21 01:30:00 -07:00
commit d62a39a22b
6 changed files with 16 additions and 9 deletions

@ -49,8 +49,8 @@ def aliased_name_for(table_name, aliased_name = nil)
end
end
def pluralize(table_name)
ActiveRecord::Base.pluralize_table_names ? table_name.to_s.pluralize : table_name.to_s
def pluralize(table_name, base)
base.pluralize_table_names ? table_name.to_s.pluralize : table_name.to_s
end
private

@ -32,7 +32,7 @@ def table_name_for(reflection)
end
def table_alias_for(reflection, join = false)
name = alias_tracker.pluralize(reflection.name)
name = alias_tracker.pluralize(reflection.name, reflection.active_record)
name << "_#{alias_suffix}"
name << "_join" if join
name

@ -393,8 +393,8 @@ class Base
# Indicates whether table names should be the pluralized versions of the corresponding class names.
# If true, the default table name for a Product class will be +products+. If false, it would just be +product+.
# See table_name for the full rules on table/class naming. This is true, by default.
cattr_accessor :pluralize_table_names, :instance_writer => false
@@pluralize_table_names = true
class_attribute :pluralize_table_names, :instance_writer => false
self.pluralize_table_names = true
##
# :singleton-method:

@ -708,12 +708,9 @@ def test_has_many_through_goes_through_all_sti_classes
end
def test_has_many_with_pluralize_table_names_false
engine = Engine.create(:car_id => 1)
Aircraft.pluralize_table_names = false
engine = Engine.create!(:car_id => 1)
aircraft = Aircraft.create!(:name => "Airbus 380", :id => 1)
assert_equal aircraft.engines, [engine]
ensure
ActiveRecord::Base.pluralize_table_names = true
end
private

@ -367,6 +367,15 @@ def test_table_name_guesses_with_inherited_prefixes_and_suffixes
GUESSED_CLASSES.each(&:reset_table_name)
end
def test_singular_table_name_guesses_for_individual_table
CreditCard.pluralize_table_names = false
CreditCard.reset_table_name
assert_equal "credit_card", CreditCard.table_name
assert_equal "categories", Category.table_name
ensure
CreditCard.pluralize_table_names = true
CreditCard.reset_table_name
end
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
def test_update_all_with_order_and_limit

@ -1,3 +1,4 @@
class Aircraft < ActiveRecord::Base
self.pluralize_table_names = false
has_many :engines, :foreign_key => "car_id"
end