Factorize methods that are easily reversible [#8267]
This commit is contained in:
parent
313232463d
commit
7204d3c63e
@ -86,40 +86,41 @@ def #{method}(*args, &block) # def create_table(*args, &block)
|
||||
|
||||
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 @@ def invert_remove_index(args)
|
||||
[: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.
|
||||
|
@ -127,12 +127,12 @@ def test_invert_rename_table
|
||||
|
||||
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 @@ def test_invert_rename_index
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user