Skip to content

Commit d70a9c7

Browse files
authored
Merge pull request rails#54725 from a5-stable/implement-respond-to-missing-for-migration
Add respond_to_missing? to complement method_missing in ActiveRecord::Migration
2 parents 23956c7 + bd12382 commit d70a9c7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

activerecord/lib/active_record/migration.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,11 @@ def load_schema!
782782
system("bin/rails db:test:prepare")
783783
end
784784
end
785+
786+
def respond_to_missing?(method, include_private = false)
787+
return false if nearest_delegate == delegate
788+
nearest_delegate.respond_to?(method, include_private)
789+
end
785790
end
786791

787792
def disable_ddl_transaction # :nodoc:
@@ -1170,6 +1175,10 @@ def internal_option?(option_name)
11701175
def command_recorder
11711176
CommandRecorder.new(connection)
11721177
end
1178+
1179+
def respond_to_missing?(method, include_private = false)
1180+
execution_strategy.respond_to?(method, include_private) || super
1181+
end
11731182
end
11741183

11751184
# MigrationProxy is used to defer loading of the actual migration classes

activerecord/test/cases/migration_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,21 @@ def create_table; "hi mom!"; end
461461
assert_equal "hi mom!", migration.method_missing(:create_table)
462462
end
463463

464+
def test_respond_to_for_migration_method
465+
migration_class = Class.new(ActiveRecord::Migration::Current) {
466+
def connection
467+
Class.new {
468+
def create_table; end
469+
}.new
470+
end
471+
}
472+
473+
migration_class.class_eval { undef_method :create_table }
474+
# create_table is handled by method_missing, so respond_to? returns true.
475+
assert migration_class.new.respond_to?(:create_table)
476+
assert migration_class.respond_to?(:create_table)
477+
end
478+
464479
def test_add_table_with_decimals
465480
Person.lease_connection.drop_table :big_numbers rescue nil
466481

0 commit comments

Comments
 (0)