From 6a3b3fba5a173dc8cd308c30426247e93e5fc97e Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Fri, 20 May 2011 01:44:13 -0500 Subject: Allow pluralize_table_names for individual model --- activerecord/lib/active_record/base.rb | 4 ++-- activerecord/test/cases/base_test.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index cd16b8d3ca..1ece79adc0 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -393,8 +393,8 @@ module ActiveRecord #:nodoc: # 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: diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index bfb66f07da..87ce537e0e 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -367,6 +367,15 @@ class BasicsTest < ActiveRecord::TestCase 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 -- cgit v1.2.3 From 23e94a67f8e922a9ccde7f8b2e6abc56a925d6bf Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Fri, 20 May 2011 01:53:13 -0500 Subject: Refactoring test_has_many_with_pluralize_table_names_false test --- activerecord/test/cases/associations/join_model_test.rb | 5 +---- activerecord/test/models/aircraft.rb | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 8e23ab78be..b59ce4efeb 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -708,12 +708,9 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/models/aircraft.rb b/activerecord/test/models/aircraft.rb index 0c47aab539..1f35ef45da 100644 --- a/activerecord/test/models/aircraft.rb +++ b/activerecord/test/models/aircraft.rb @@ -1,3 +1,4 @@ class Aircraft < ActiveRecord::Base + self.pluralize_table_names = false has_many :engines, :foreign_key => "car_id" end -- cgit v1.2.3 From e0e8918003eab33c5446f17227c9035a6df75e5f Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Fri, 20 May 2011 22:53:02 -0500 Subject: AliasTracker.pluralize use pluralize_table_names of model --- activerecord/lib/active_record/associations/alias_tracker.rb | 4 ++-- activerecord/lib/active_record/associations/join_helper.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/alias_tracker.rb b/activerecord/lib/active_record/associations/alias_tracker.rb index 44e2ee141e..bd98cf2f46 100644 --- a/activerecord/lib/active_record/associations/alias_tracker.rb +++ b/activerecord/lib/active_record/associations/alias_tracker.rb @@ -49,8 +49,8 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/associations/join_helper.rb b/activerecord/lib/active_record/associations/join_helper.rb index eae546e76e..87e33891a5 100644 --- a/activerecord/lib/active_record/associations/join_helper.rb +++ b/activerecord/lib/active_record/associations/join_helper.rb @@ -32,7 +32,7 @@ module ActiveRecord 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 -- cgit v1.2.3