Deprecation: check whether instance variables have been monkeyed with before assigning them to deprecation proxies. Raises a RuntimeError if so.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4717 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-08-07 21:58:31 +00:00
parent c0657a9084
commit c3cdd3b659
3 changed files with 29 additions and 4 deletions

@ -1,5 +1,7 @@
*SVN*
* Deprecation: check whether instance variables have been monkeyed with before assigning them to deprecation proxies. Raises a RuntimeError if so. [Jeremy Kemper]
* Add support for the param_name parameter to the auto_complete_field helper. #5026 [david.a.williams@gmail.com]
* Deprecation! @params, @session, @flash will be removed after 1.2. Use the corresponding instance methods instead. You'll get printed warnings during tests and logged warnings in dev mode when you access either instance variable directly. [Jeremy Kemper]

@ -400,7 +400,6 @@ def filter_parameter_logging(*filter_words, &block)
def process(request, response, method = :perform_action, *arguments) #:nodoc:
initialize_template_class(response)
assign_shortcuts(request, response)
assign_deprecated_shortcuts(request, response)
initialize_current_url
assign_names
forget_variables_added_to_assigns
@ -942,6 +941,8 @@ def assign_shortcuts(request, response)
@assigns = @response.template.assigns
@headers = @response.headers
assign_deprecated_shortcuts(request, response)
end
@ -950,8 +951,15 @@ def assign_shortcuts(request, response)
# Gone after 1.2.
def assign_deprecated_shortcuts(request, response)
DEPRECATED_INSTANCE_VARIABLES.each do |var|
instance_variable_set "@#{var}", ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, var)
DEPRECATED_INSTANCE_VARIABLES.each do |method|
var = "@#{method}"
if instance_variables.include?(var)
value = instance_variable_get(var)
unless ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy === value
raise "Deprecating #{var}, but it's already set to #{value.inspect}! Use the #{method}= writer method instead of setting #{var} directly."
end
end
instance_variable_set var, ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, method)
end
end
@ -1011,7 +1019,7 @@ def reset_variables_added_to_assigns
end
def add_instance_variables_to_assigns
@@protected_variables_cache ||= protected_instance_variables.inject({}) { |h, k| h[k] = true; h }
@@protected_variables_cache ||= Set.new(protected_instance_variables)
instance_variables.each do |var|
next if @@protected_variables_cache.include?(var)
@assigns[var[1..-1]] = instance_variable_get(var)

@ -2,6 +2,15 @@
class DeprecatedInstanceVariablesTest < Test::Unit::TestCase
class Target < ActionController::Base
def initialize(run = nil)
instance_eval(run) if run
super()
end
def noop
render :nothing => true
end
ActionController::Base::DEPRECATED_INSTANCE_VARIABLES.each do |var|
class_eval "def old_#{var}; render :text => @#{var}.inspect end"
class_eval "def new_#{var}; render :text => #{var}.inspect end"
@ -28,6 +37,12 @@ def test_new_#{var}_isnt_deprecated
def test_internal_#{var}_isnt_deprecated
assert_not_deprecated { get :internal_#{var} }
end
def test_#{var}_raises_if_already_set
assert_raise(RuntimeError) do
@controller = Target.new '@#{var} = Object.new'
get :noop
end
end
end_eval
end
end