From 68f0ef9056c7aa3d407e9c6d7c0fbe2e3e6f780a Mon Sep 17 00:00:00 2001 From: Aleksey Magusev Date: Mon, 2 Jul 2012 23:43:56 +0400 Subject: Refactor references schema definitions --- .../abstract/schema_definitions.rb | 30 +++++++++------------- .../test/cases/migration/change_table_test.rb | 28 +++++++++----------- 2 files changed, 24 insertions(+), 34 deletions(-) (limited to 'activerecord') 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 df78ba6c5a..0706e6ae53 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -282,7 +282,7 @@ module ActiveRecord index_options = options.delete(:index) args.each do |col| column("#{col}_id", :integer, options) - column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil? + column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic index(polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : nil) if index_options end end @@ -441,17 +441,13 @@ module ActiveRecord # Adds a reference. Optionally adds a +type+ column, if :polymorphic option is provided. # references and belongs_to are acceptable. # - # t.references(:goat) - # t.references(:goat, :polymorphic => true) - # t.belongs_to(:goat) + # t.references(:user) + # t.belongs_to(:supplier, polymorphic: true) + # def references(*args) options = args.extract_options! - polymorphic = options.delete(:polymorphic) - index_options = options.delete(:index) - args.each do |col| - @base.add_column(@table_name, "#{col}_id", :integer, options) - @base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil? - @base.add_index(@table_name, polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : nil) if index_options + args.each do |ref_name| + @base.add_reference(@table_name, ref_name, options) end end alias :belongs_to :references @@ -459,18 +455,16 @@ module ActiveRecord # Removes a reference. Optionally removes a +type+ column. # remove_references and remove_belongs_to are acceptable. # - # t.remove_references(:goat) - # t.remove_references(:goat, :polymorphic => true) - # t.remove_belongs_to(:goat) + # t.remove_references(:user) + # t.remove_belongs_to(:supplier, polymorphic: true) + # def remove_references(*args) options = args.extract_options! - polymorphic = options.delete(:polymorphic) - args.each do |col| - @base.remove_column(@table_name, "#{col}_id") - @base.remove_column(@table_name, "#{col}_type") unless polymorphic.nil? + args.each do |ref_name| + @base.remove_reference(@table_name, ref_name, options) end end - alias :remove_belongs_to :remove_references + alias :remove_belongs_to :remove_references # Adds a column or columns of a specified type # diff --git a/activerecord/test/cases/migration/change_table_test.rb b/activerecord/test/cases/migration/change_table_test.rb index 063209389f..4614be9650 100644 --- a/activerecord/test/cases/migration/change_table_test.rb +++ b/activerecord/test/cases/migration/change_table_test.rb @@ -30,61 +30,57 @@ module ActiveRecord def test_references_column_type_adds_id with_change_table do |t| - @connection.expect :add_column, nil, [:delete_me, 'customer_id', :integer, {}] + @connection.expect :add_reference, nil, [:delete_me, :customer, {}] t.references :customer end end def test_remove_references_column_type_removes_id with_change_table do |t| - @connection.expect :remove_column, nil, [:delete_me, 'customer_id'] + @connection.expect :remove_reference, nil, [:delete_me, :customer, {}] t.remove_references :customer end end def test_add_belongs_to_works_like_add_references with_change_table do |t| - @connection.expect :add_column, nil, [:delete_me, 'customer_id', :integer, {}] + @connection.expect :add_reference, nil, [:delete_me, :customer, {}] t.belongs_to :customer end end def test_remove_belongs_to_works_like_remove_references with_change_table do |t| - @connection.expect :remove_column, nil, [:delete_me, 'customer_id'] + @connection.expect :remove_reference, nil, [:delete_me, :customer, {}] t.remove_belongs_to :customer end end def test_references_column_type_with_polymorphic_adds_type with_change_table do |t| - @connection.expect :add_column, nil, [:delete_me, 'taggable_id', :integer, {}] - @connection.expect :add_column, nil, [:delete_me, 'taggable_type', :string, {}] - t.references :taggable, :polymorphic => true + @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true] + t.references :taggable, polymorphic: true end end def test_remove_references_column_type_with_polymorphic_removes_type with_change_table do |t| - @connection.expect :remove_column, nil, [:delete_me, 'taggable_id'] - @connection.expect :remove_column, nil, [:delete_me, 'taggable_type'] - t.remove_references :taggable, :polymorphic => true + @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true] + t.remove_references :taggable, polymorphic: true end end def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag with_change_table do |t| - @connection.expect :add_column, nil, [:delete_me, 'taggable_id', :integer, {:null => false}] - @connection.expect :add_column, nil, [:delete_me, 'taggable_type', :string, {:null => false}] - t.references :taggable, :polymorphic => true, :null => false + @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true, null: false] + t.references :taggable, polymorphic: true, null: false end end def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag with_change_table do |t| - @connection.expect :remove_column, nil, [:delete_me, 'taggable_id'] - @connection.expect :remove_column, nil, [:delete_me, 'taggable_type'] - t.remove_references :taggable, :polymorphic => true, :null => false + @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true, null: false] + t.remove_references :taggable, polymorphic: true, null: false end end -- cgit v1.2.3