Skip to content

Commit

Permalink
Deprecate ivars in view.
Browse files Browse the repository at this point in the history
Deprecate use of @logger and @action_name instance variables inside
views. Please use instance methods logger and action_name instead.
  • Loading branch information
lifo committed May 6, 2008
1 parent 2c96f50 commit 63edc02
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
14 changes: 12 additions & 2 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -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 '-'.
Expand Down Expand Up @@ -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


Expand Down
51 changes: 51 additions & 0 deletions 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
15 changes: 15 additions & 0 deletions activesupport/lib/active_support/deprecation.rb
@@ -1,4 +1,5 @@
require 'yaml'
require 'delegate'

module ActiveSupport
module Deprecation #:nodoc:
Expand Down Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions 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'
Expand Down Expand Up @@ -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

0 comments on commit 63edc02

Please sign in to comment.