diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index 77c00f5a67..2e92cbfbf3 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -48,12 +48,11 @@ module ActiveSupport end module Assertions - def assert_deprecated(regexp = nil, &block) + def assert_deprecated(match = nil, &block) last = with_last_message_tracking_deprecation_behavior(&block) assert last, "Expected a deprecation warning within the block but received none" - if regexp - assert_match regexp, last, "Deprecation warning didn't match #{regexp}: #{last}" - end + match = Regexp.new(match) unless match.is_a?(Regexp) + assert_match match, last, "Deprecation warning didn't match #{match}: #{last}" end def assert_not_deprecated(&block) @@ -72,6 +71,26 @@ module ActiveSupport ActiveSupport::Deprecation.behavior = old_behavior end end + + # Stand-in for @request, @attributes, etc. + class DeprecatedInstanceVariableProxy + instance_methods.each { |m| undef_method m unless m =~ /^__/ } + + def initialize(instance, method, var = "@#{method}") + @instance, @method, @var = instance, method, var + deprecation_warning :initialize, caller + end + + private + def deprecation_warning(called, callstack) + ActiveSupport::Deprecation.warn("Using #{@var} directly is deprecated - call #{@method} instead.", callstack) + end + + def method_missing(called, *args, &block) + deprecation_warning called, caller + @instance.__send__(@method).__send__(called, *args, &block) + end + end end end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 5fb0c5b042..48506656a5 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -1,6 +1,13 @@ require File.dirname(__FILE__) + '/abstract_unit' class Deprecatee + def initialize + @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request) + @_request = 'there we go' + end + def request; @_request end + def old_request; @request end + def partially(foo = nil) ActiveSupport::Deprecation.warn 'calling with foo=nil is out' if foo.nil? end @@ -57,4 +64,13 @@ class DeprecationTest < Test::Unit::TestCase ActiveSupport::Deprecation.behavior = nil assert_deprecated(/foo=nil/) { @dtc.partially } end + + def test_deprecated_instance_variable_proxy + assert_not_deprecated { @dtc.request.size } + + assert_deprecated('Using @request directly is deprecated - call request instead.') do + assert_equal @dtc.request.size, @dtc.old_request.size + assert_equal @dtc.request.to_s, @dtc.old_request.to_s + end + end end