aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-05-21 01:30:00 -0700
committerJon Leighton <j@jonathanleighton.com>2011-05-21 01:30:00 -0700
commitd62a39a22b7ad56efa79089177aa06fda0d41f86 (patch)
tree1323af2dd5870c4d17863974550b6f9efca887e7
parent7288682be4f9384ad4083415342d8af876f77db8 (diff)
parente0e8918003eab33c5446f17227c9035a6df75e5f (diff)
downloadrails-d62a39a22b7ad56efa79089177aa06fda0d41f86.tar.gz
rails-d62a39a22b7ad56efa79089177aa06fda0d41f86.tar.bz2
rails-d62a39a22b7ad56efa79089177aa06fda0d41f86.zip
Merge pull request #1162 from guilleiguaran/singularize_individual_table_name
Singularize individual table name
-rw-r--r--activerecord/lib/active_record/associations/alias_tracker.rb4
-rw-r--r--activerecord/lib/active_record/associations/join_helper.rb2
-rw-r--r--activerecord/lib/active_record/base.rb4
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb5
-rw-r--r--activerecord/test/cases/base_test.rb9
-rw-r--r--activerecord/test/models/aircraft.rb1
6 files changed, 16 insertions, 9 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
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 30e2d1a82c..92e525a361 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/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/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
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