Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Deprecate ivars in view.

Deprecate use of @logger and @action_name instance variables inside
views. Please use instance methods logger and action_name instead.
  • Loading branch information...
commit 63edc022f12713b1ac170331a1b619fbcc064d5a 1 parent 2c96f50
@lifo lifo authored
View
14 actionpack/lib/action_view/base.rb
@@ -157,6 +157,8 @@ class Base
attr_reader :logger, :response, :headers
attr_internal :cookies, :flash, :headers, :params, :request, :response, :session
+ delegate :logger, :action_name, :to => :controller
+
attr_writer :template_format
# Specify trim mode for the ERB compiler. Defaults to '-'.
@@ -524,10 +526,18 @@ def delegate_compile(handler, template)
def template_handler_is_compilable?(handler)
handler.new(self).respond_to?(:compile)
end
-
+
# Assigns instance variables from the controller to the view.
def assign_variables_from_controller
- @assigns.each { |key, value| instance_variable_set("@#{key}", value) }
+ @assigns.each do |key, value|
+ if ['logger'].include?(key)
+ instance_variable_set("@#{key}", ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, key.to_sym))
+ elsif ['action_name'].include?(key)
+ instance_variable_set("@#{key}", ActiveSupport::Deprecation::DeprecatedInstanceVariable.new(value, key))
+ else
+ instance_variable_set("@#{key}", value)
+ end
+ end
end
View
51 actionpack/test/template/deprecate_ivars_test.rb
@@ -0,0 +1,51 @@
+require File.dirname(__FILE__) + '/../abstract_unit'
+
+class DeprecateIvars < ActionController::Base
+ def use_logger
+ render :inline => "<%= logger.class -%>"
+ end
+
+ def use_old_logger
+ render :inline => "<%= @logger.class -%>"
+ end
+
+ def use_action_name
+ render :inline => "<%= action_name -%>"
+ end
+
+ def use_old_action_name
+ render :inline => "<%= @action_name -%>"
+ end
+end
+
+class DeprecateIvarsTest < Test::Unit::TestCase
+ def setup
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ @controller = DeprecateIvars.new
+ @controller.logger = Logger.new(nil)
+
+ @request.host = "rubyonrails.com"
+ end
+
+ def test_logger
+ assert_not_deprecated { get :use_logger }
+ assert_equal "Logger", @response.body
+ end
+
+ def test_deprecated_logger
+ assert_deprecated { get :use_old_logger }
+ assert_equal "Logger", @response.body
+ end
+
+ def test_action_name
+ assert_not_deprecated { get :use_action_name }
+ assert_equal "use_action_name", @response.body
+ end
+
+ def test_deprecated_action_name
+ assert_deprecated { get :use_old_action_name }
+ assert_equal "use_old_action_name", @response.body
+ end
+end
View
15 activesupport/lib/active_support/deprecation.rb
@@ -1,4 +1,5 @@
require 'yaml'
+require 'delegate'
module ActiveSupport
module Deprecation #:nodoc:
@@ -175,6 +176,20 @@ def warn(callstack, called, args)
ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
end
end
+
+ class DeprecatedInstanceVariable < Delegator
+ def initialize(value, method)
+ super(value)
+ @method = method
+ @value = value
+ end
+
+ def __getobj__
+ ActiveSupport::Deprecation.warn("Instance variable @#{@method} is deprecated! Call instance method #{@method} instead.")
+ @value
+ end
+ end
+
end
end
View
12 activesupport/test/deprecation_test.rb
@@ -1,6 +1,7 @@
require File.dirname(__FILE__) + '/abstract_unit'
class Deprecatee
+
def initialize
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
@_request = 'there we go'
@@ -148,4 +149,15 @@ def message
assert_not_deprecated { error.message }
assert_nil @last_message
end
+
+end
+
+class DeprecatedIvarTest < Test::Unit::TestCase
+
+ def test_deprecated_ivar
+ @action = ActiveSupport::Deprecation::DeprecatedInstanceVariable.new("fubar", :foobar)
+
+ assert_deprecated(/Instance variable @foobar is deprecated! Call instance method foobar instead/) { assert_equal "fubar", @action }
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.