aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-11-19 09:26:11 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2010-11-19 10:24:16 -0800
commit6519df4157a861c9c9d567ee57983ded0e967a10 (patch)
tree14de75a1542137d115b67b6ba81d69a65d8f8cbe /activerecord
parent6dbbfae5638a6c847fd63d52a72247e2bb15a320 (diff)
downloadrails-6519df4157a861c9c9d567ee57983ded0e967a10.tar.gz
rails-6519df4157a861c9c9d567ee57983ded0e967a10.tar.bz2
rails-6519df4157a861c9c9d567ee57983ded0e967a10.zip
command recorder will record commands sent to a delegate object
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/migration/command_recorder.rb16
-rw-r--r--activerecord/test/cases/migration/command_recorder_test.rb22
2 files changed, 35 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb
index 8a98a177d5..fa189b8009 100644
--- a/activerecord/lib/active_record/migration/command_recorder.rb
+++ b/activerecord/lib/active_record/migration/command_recorder.rb
@@ -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]
diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb
index 50d75e0400..ea2292dda5 100644
--- a/activerecord/test/cases/migration/command_recorder_test.rb
+++ b/activerecord/test/cases/migration/command_recorder_test.rb
@@ -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