diff options
author | Mehmet Emin İNAÇ <mehmetemininac@gmail.com> | 2015-04-05 01:23:11 +0300 |
---|---|---|
committer | Mehmet Emin İNAÇ <mehmetemininac@gmail.com> | 2015-04-06 11:10:24 +0300 |
commit | 8c1180731f26e09a5163cfe96b7726c9e425207b (patch) | |
tree | 29113e0f7fd800406ef1ca792d296859af5a36e9 | |
parent | a05f3e5f96f7a8aa55483d91becdbe49b81833fd (diff) | |
download | rails-8c1180731f26e09a5163cfe96b7726c9e425207b.tar.gz rails-8c1180731f26e09a5163cfe96b7726c9e425207b.tar.bz2 rails-8c1180731f26e09a5163cfe96b7726c9e425207b.zip |
use singular table name if pluralize_table_names is setted as false while creating foreign key
test case for use singular table name if pluralize_table_names is setted as false while creating foreign key
refactor references foreign key addition tests
use singular table name while removing foreign key
merge foreign key singular table name methods
remove unnecessary drop table from test
3 files changed, 27 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index a768ee2d70..cb83d0022c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -138,7 +138,7 @@ module ActiveRecord end def foreign_table_name - name.to_s.pluralize + Base.pluralize_table_names ? name.to_s.pluralize : name end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 72e019066e..c1b4c936be 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -667,7 +667,10 @@ module ActiveRecord # remove_reference(:products, :user, index: true, foreign_key: true) # def remove_reference(table_name, ref_name, options = {}) - remove_foreign_key table_name, ref_name.to_s.pluralize if options[:foreign_key] + if options[:foreign_key] + reference_name = Base.pluralize_table_names ? ref_name.to_s.pluralize : ref_name + remove_foreign_key(table_name, reference_name) + end remove_column(table_name, "#{ref_name}_id") remove_column(table_name, "#{ref_name}_type") if options[:polymorphic] diff --git a/activerecord/test/cases/migration/references_foreign_key_test.rb b/activerecord/test/cases/migration/references_foreign_key_test.rb index 17ac72a109..87348d0f90 100644 --- a/activerecord/test/cases/migration/references_foreign_key_test.rb +++ b/activerecord/test/cases/migration/references_foreign_key_test.rb @@ -105,6 +105,28 @@ module ActiveRecord @connection.remove_reference :testings, :testing_parent, foreign_key: true end end + + test "foreign key methods respect pluralize_table_names" do + begin + original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names + ActiveRecord::Base.pluralize_table_names = false + @connection.create_table :testing + @connection.change_table :testing_parents do |t| + t.references :testing, foreign_key: true + end + + fk = @connection.foreign_keys("testing_parents").first + assert_equal "testing_parents", fk.from_table + assert_equal "testing", fk.to_table + + assert_difference "@connection.foreign_keys('testing_parents').size", -1 do + @connection.remove_reference :testing_parents, :testing, foreign_key: true + end + ensure + ActiveRecord::Base.pluralize_table_names = original_pluralize_table_names + @connection.drop_table "testing", if_exists: true + end + end end end end |