aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2012-11-19 02:29:26 -0500
committerMarc-Andre Lafortune <github@marc-andre.ca>2012-12-21 13:54:52 -0500
commit7204d3c63e0c412c638b885fb552ec50e7c1520a (patch)
tree158e1570df5f70568377828a5230cdd96aee949d /activerecord
parent313232463d2a378cb3d14a34f5334cc4bd7a07b8 (diff)
downloadrails-7204d3c63e0c412c638b885fb552ec50e7c1520a.tar.gz
rails-7204d3c63e0c412c638b885fb552ec50e7c1520a.tar.bz2
rails-7204d3c63e0c412c638b885fb552ec50e7c1520a.zip
Factorize methods that are easily reversible [#8267]
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/migration/command_recorder.rb54
-rw-r--r--activerecord/test/cases/migration/command_recorder_test.rb16
2 files changed, 28 insertions, 42 deletions
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb
index d61de6bc47..4ea709fe69 100644
--- a/activerecord/lib/active_record/migration/command_recorder.rb
+++ b/activerecord/lib/active_record/migration/command_recorder.rb
@@ -86,40 +86,41 @@ module ActiveRecord
private
- def invert_transaction(args, &block)
- [:transaction, args, block]
+ module StraightReversions
+ private
+ { transaction: :transaction,
+ create_table: :drop_table,
+ create_join_table: :drop_join_table,
+ add_column: :remove_column,
+ add_timestamps: :remove_timestamps,
+ add_reference: :remove_reference,
+ }.each do |cmd, inv|
+ [[inv, cmd], [cmd, inv]].each do |method, inverse|
+ class_eval <<-EOV, __FILE__, __LINE__ + 1
+ def invert_#{method}(args, &block) # def invert_create_table(args, &block)
+ [:#{inverse}, args, block] # [:drop_table, args, block]
+ end # end
+ EOV
+ end
+ end
end
- def invert_create_table(args, &block)
- [:drop_table, args, block]
- end
+ include StraightReversions
def invert_drop_table(args, &block)
if args.size == 1 && block == nil
raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)."
end
- [:create_table, args, block]
- end
-
- def invert_create_join_table(args, &block)
- [:drop_join_table, args, block]
- end
-
- def invert_drop_join_table(args, &block)
- [:create_join_table, args, block]
+ super
end
def invert_rename_table(args)
[:rename_table, args.reverse]
end
- def invert_add_column(args)
- [:remove_column, args]
- end
-
def invert_remove_column(args)
raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2
- [:add_column, args]
+ super
end
def invert_rename_index(args)
@@ -143,22 +144,7 @@ module ActiveRecord
[:add_index, [table, options.delete(:column), options]]
end
- def invert_remove_timestamps(args)
- [:add_timestamps, args]
- end
-
- def invert_add_timestamps(args)
- [:remove_timestamps, args]
- end
-
- def invert_add_reference(args)
- [:remove_reference, args]
- end
alias :invert_add_belongs_to :invert_add_reference
-
- def invert_remove_reference(args)
- [:add_reference, args]
- end
alias :invert_remove_belongs_to :invert_remove_reference
# Forwards any missing method call to the \target.
diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb
index c12ffb0b4c..1c5530a132 100644
--- a/activerecord/test/cases/migration/command_recorder_test.rb
+++ b/activerecord/test/cases/migration/command_recorder_test.rb
@@ -127,12 +127,12 @@ module ActiveRecord
def test_invert_add_column
remove = @recorder.inverse_of :add_column, [:table, :column, :type, {}]
- assert_equal [:remove_column, [:table, :column, :type, {}]], remove
+ assert_equal [:remove_column, [:table, :column, :type, {}], nil], remove
end
def test_invert_remove_column
add = @recorder.inverse_of :remove_column, [:table, :column, :type, {}]
- assert_equal [:add_column, [:table, :column, :type, {}]], add
+ assert_equal [:add_column, [:table, :column, :type, {}], nil], add
end
def test_invert_remove_column_without_type
@@ -189,32 +189,32 @@ module ActiveRecord
def test_invert_add_timestamps
remove = @recorder.inverse_of :add_timestamps, [:table]
- assert_equal [:remove_timestamps, [:table]], remove
+ assert_equal [:remove_timestamps, [:table], nil], remove
end
def test_invert_remove_timestamps
add = @recorder.inverse_of :remove_timestamps, [:table]
- assert_equal [:add_timestamps, [:table]], add
+ assert_equal [:add_timestamps, [:table], nil], add
end
def test_invert_add_reference
remove = @recorder.inverse_of :add_reference, [:table, :taggable, { polymorphic: true }]
- assert_equal [:remove_reference, [:table, :taggable, { polymorphic: true }]], remove
+ assert_equal [:remove_reference, [:table, :taggable, { polymorphic: true }], nil], remove
end
def test_invert_add_belongs_to_alias
remove = @recorder.inverse_of :add_belongs_to, [:table, :user]
- assert_equal [:remove_reference, [:table, :user]], remove
+ assert_equal [:remove_reference, [:table, :user], nil], remove
end
def test_invert_remove_reference
add = @recorder.inverse_of :remove_reference, [:table, :taggable, { polymorphic: true }]
- assert_equal [:add_reference, [:table, :taggable, { polymorphic: true }]], add
+ assert_equal [:add_reference, [:table, :taggable, { polymorphic: true }], nil], add
end
def test_invert_remove_belongs_to_alias
add = @recorder.inverse_of :remove_belongs_to, [:table, :user]
- assert_equal [:add_reference, [:table, :user]], add
+ assert_equal [:add_reference, [:table, :user], nil], add
end
end
end