1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

command recorder will record commands sent to a delegate object

This commit is contained in:
Aaron Patterson 2010-11-19 09:26:11 -08:00
parent 6dbbfae563
commit 6519df4157
2 changed files with 35 additions and 3 deletions

View file

@ -3,10 +3,11 @@ module ActiveRecord
# ActiveRecord::Migration::CommandRecorder records commands done during
# a migration and knows how to reverse those commands.
class CommandRecorder
attr_reader :commands
attr_reader :commands, :delegate
def initialize
def initialize(delegate = nil)
@commands = []
@delegate = delegate
end
# record +command+. +command+ should be a method name and arguments.
@ -29,10 +30,19 @@ module ActiveRecord
@commands.reverse.map { |name, args|
method = :"invert_#{name}"
raise IrreversibleMigration unless respond_to?(method, true)
send(method, args)
__send__(method, args)
}
end
def respond_to?(*args) # :nodoc:
super || delegate.respond_to?(*args)
end
def send(method, *args) # :nodoc:
return super unless respond_to?(method)
record(method, args)
end
private
def invert_create_table(args)
[:drop_table, args]

View file

@ -7,6 +7,28 @@ module ActiveRecord
@recorder = CommandRecorder.new
end
def test_respond_to_delegates
recorder = CommandRecorder.new(Class.new {
def america; end
}.new)
assert recorder.respond_to?(:america)
end
def test_send_calls_super
assert_raises(NoMethodError) do
@recorder.send(:create_table, :horses)
end
end
def test_send_delegates_to_record
recorder = CommandRecorder.new(Class.new {
def create_table(name); end
}.new)
assert recorder.respond_to?(:create_table), 'respond_to? create_table'
recorder.send(:create_table, :horses)
assert_equal [[:create_table, [:horses]]], recorder.commands
end
def test_unknown_commands_raise_exception
@recorder.record :execute, ['some sql']
assert_raises(ActiveRecord::IrreversibleMigration) do