Skip to content
Browse files

Deprecation! @session and @flash will be removed after 1.2. Use the s…

…ession and flash methods instead. You'll get printed warnings during tests and logged warnings in dev mode when you access either instance variable directly.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4699 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 2bf8f23 commit 2399a223c35622178ea58db6629cfd26f2230d60 @jeremy jeremy committed Aug 7, 2006
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Deprecation! @session and @flash will be removed after 1.2. Use the session and flash methods instead. You'll get printed warnings during tests and logged warnings in dev mode when you access either instance variable directly. [Jeremy Kemper]
+
* Make Routing noisy when an anchor regexp is assigned to a segment. #5674 [francois.beausoleil@gmail.com]
* Added months and years to the resolution of DateHelper#distance_of_time_in_words, such that "60 days ago" becomes "2 months ago" #5611 [pjhyett@gmail.com]
View
4 actionpack/examples/blog_controller.cgi
@@ -14,7 +14,7 @@ class BlogController < ActionController::Base
render_template <<-"EOF"
<html><body>
- <%= @flash["alert"] %>
+ <%= flash["alert"] %>
<h1>Posts</h1>
<% @posts.each do |post| %>
<p><b><%= post.title %></b><br /><%= post.body %></p>
@@ -50,4 +50,4 @@ begin
BlogController.process_cgi(CGI.new) if $0 == __FILE__
rescue => e
CGI.new.out { "#{e.class}: #{e.message}" }
-end
+end
View
66 actionpack/lib/action_controller/base.rb
@@ -42,8 +42,8 @@ def initialize(message = nil)
end
end
class RedirectBackError < ActionControllerError #:nodoc:
- DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify @request.env["HTTP_REFERER"].'
-
+ DEFAULT_MESSAGE = 'No HTTP_REFERER was set in the request to this action, so redirect_to :back could not be called successfully. If this is a test, make sure to specify request.env["HTTP_REFERER"].'
+
def initialize(message = nil)
super(message || DEFAULT_MESSAGE)
end
@@ -59,7 +59,7 @@ def initialize(message = nil)
# def index
# @entries = Entry.find(:all)
# end
- #
+ #
# def sign
# Entry.create(params[:entry])
# redirect_to :action => "index"
@@ -300,8 +300,8 @@ class Base
# Holds a hash of objects in the session. Accessed like <tt>session[:person]</tt> to get the object tied to the "person"
# key. The session will hold any type of object as values, but the key should be a string or symbol.
- attr_accessor :session
-
+ attr_internal :session
+
# Holds a hash of header names and values. Accessed like <tt>headers["Cache-Control"]</tt> to get the value of the Cache-Control
# directive. Values should always be specified as strings.
attr_accessor :headers
@@ -400,6 +400,7 @@ 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
@@ -752,17 +753,17 @@ def render_template(template, status = nil, type = :rhtml, local_assigns = {}) #
def render_text(text = nil, status = nil) #:nodoc:
@performed_render = true
- @response.headers['Status'] = (status || DEFAULT_RENDER_STATUS_CODE).to_s
- @response.body = text
+ response.headers['Status'] = (status || DEFAULT_RENDER_STATUS_CODE).to_s
+ response.body = text
end
def render_javascript(javascript, status = nil) #:nodoc:
- @response.headers['Content-Type'] = 'text/javascript; charset=UTF-8'
+ response.headers['Content-Type'] = 'text/javascript; charset=UTF-8'
render_text(javascript, status)
end
def render_xml(xml, status = nil) #:nodoc:
- @response.headers['Content-Type'] = 'application/xml'
+ response.headers['Content-Type'] = 'application/xml'
render_text(xml, status)
end
@@ -791,7 +792,7 @@ def render_without_layout(template_name = default_template_name, status = nil) #
# Clears the rendered results, allowing for another render to be performed.
def erase_render_results #:nodoc:
- @response.body = nil
+ response.body = nil
@performed_render = false
end
@@ -893,20 +894,20 @@ def expires_in(seconds, options = {}) #:doc:
cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys)
cache_options.delete_if { |k,v| v.nil? or v == false }
cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"}
- @response.headers["Cache-Control"] = cache_control.join(', ')
+ response.headers["Cache-Control"] = cache_control.join(', ')
end
# Sets a HTTP 1.1 Cache-Control header of "no-cache" so no caching should occur by the browser or
# intermediate caches (like caching proxy servers).
def expires_now #:doc:
- @response.headers["Cache-Control"] = "no-cache"
+ response.headers["Cache-Control"] = "no-cache"
end
# Resets the session by clearing out all the objects stored within and initializing a new session object.
def reset_session #:doc:
- @request.reset_session
- @session = @request.session
- @response.session = @session
+ request.reset_session
+ @_session = request.session
+ response.session = @_session
end
private
@@ -929,29 +930,40 @@ def initialize_template_class(response)
response.redirected_to = nil
@performed_render = @performed_redirect = false
end
-
+
def assign_shortcuts(request, response)
@request, @params, @cookies = request, request.parameters, request.cookies
@response = response
@response.session = request.session
- @session = @response.session
+ @_session = @response.session
@template = @response.template
@assigns = @response.template.assigns
-
+
@headers = @response.headers
end
-
+
+
+ # TODO: assigns cookies headers params request response template
+ DEPRECATED_INSTANCE_VARIABLES = %w(flash session)
+
+ # 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)
+ end
+ end
+
def initialize_current_url
- @url = UrlRewriter.new(@request, @params.clone())
+ @url = UrlRewriter.new(request, params.clone)
end
def log_processing
if logger
logger.info "\n\nProcessing #{controller_class_name}\##{action_name} (for #{request_origin}) [#{request.method.to_s.upcase}]"
- logger.info " Session ID: #{@session.session_id}" if @session and @session.respond_to?(:session_id)
- logger.info " Parameters: #{respond_to?(:filter_parameters) ? filter_parameters(@params).inspect : @params.inspect}"
+ logger.info " Session ID: #{@_session.session_id}" if @_session and @_session.respond_to?(:session_id)
+ logger.info " Parameters: #{respond_to?(:filter_parameters) ? filter_parameters(params).inspect : params.inspect}"
end
end
@@ -1023,17 +1035,17 @@ def protected_instance_variables
def request_origin
# this *needs* to be cached!
# otherwise you'd get different results if calling it more than once
- @request_origin ||= "#{@request.remote_ip} at #{Time.now.to_s(:db)}"
+ @request_origin ||= "#{request.remote_ip} at #{Time.now.to_s(:db)}"
end
-
+
def complete_request_uri
- "#{@request.protocol}#{@request.host}#{@request.request_uri}"
+ "#{request.protocol}#{request.host}#{request.request_uri}"
end
def close_session
- @session.close unless @session.nil? || Hash === @session
+ @_session.close if @_session && @_session.respond_to?(:close)
end
-
+
def template_exists?(template_name = default_template_name)
@template.file_exists?(template_name)
end
View
7 actionpack/lib/action_controller/components.rb
@@ -111,16 +111,15 @@ def render_component_as_string(options) #:doc:
end
def flash_with_components(refresh = false) #:nodoc:
- if !defined?(@flash) || refresh
- @flash =
+ if !defined?(@_flash) || refresh
+ @_flash =
if defined?(@parent_controller)
@parent_controller.flash
else
flash_without_components
end
end
-
- @flash
+ @_flash
end
private
View
23 actionpack/lib/action_controller/flash.rb
@@ -17,7 +17,7 @@ module ActionController #:nodoc:
# end
#
# display.rhtml
- # <% if @flash[:notice] %><div class="notice"><%= @flash[:notice] %></div><% end %>
+ # <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
#
# This example just places a string in the flash, but you can put any object in there. And of course, you can put as many
# as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
@@ -141,13 +141,13 @@ def assign_shortcuts_with_flash(request, response) #:nodoc:
end
def process_cleanup_with_flash
- flash.sweep if @session
+ flash.sweep if @_session
process_cleanup_without_flash
end
def reset_session_with_flash
reset_session_without_flash
- remove_instance_variable(:@flash)
+ remove_instance_variable(:@_flash)
flash(:refresh)
end
@@ -156,20 +156,19 @@ def reset_session_with_flash
# <tt>flash["notice"] = "hello"</tt> to put a new one.
# Note that if sessions are disabled only flash.now will work.
def flash(refresh = false) #:doc:
- if !defined?(@flash) || refresh
- @flash =
- if @session.is_a?(Hash)
- # @session is a Hash, if sessions are disabled
- # we don't put the flash in the session in this case
+ if !defined?(@_flash) || refresh
+ @_flash =
+ if session.is_a?(Hash)
+ # don't put flash in session if disabled
FlashHash.new
else
- # otherwise, @session is a CGI::Session or a TestSession
+ # otherwise, session is a CGI::Session or a TestSession
# so make sure it gets retrieved from/saved to session storage after request processing
- @session["flash"] ||= FlashHash.new
+ session["flash"] ||= FlashHash.new
end
end
-
- @flash
+
+ @_flash
end
# deprecated. use <tt>flash.keep</tt> instead
View
4 actionpack/lib/action_controller/session_management.rb
@@ -128,8 +128,8 @@ def process_cleanup_with_session_management_support
# the database field. Only applies to ActiveRecordStore since there
# is not a standard way to iterate over session data.
def clear_persistent_model_associations #:doc:
- if defined?(@session) && @session.instance_variables.include?('@data')
- session_data = @session.instance_variable_get('@data')
+ if defined?(@_session) && @_session.instance_variables.include?('@data')
+ session_data = @_session.instance_variable_get('@data')
if session_data && session_data.respond_to?(:each_value)
session_data.each_value do |obj|
View
33 actionpack/test/controller/deprecated_instance_variables_test.rb
@@ -0,0 +1,33 @@
+require File.dirname(__FILE__) + '/../abstract_unit'
+
+class DeprecatedInstanceVariablesTest < Test::Unit::TestCase
+ class Target < ActionController::Base
+ 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"
+ class_eval "def internal_#{var}; render :text => @_#{var}.inspect end"
+ end
+
+ def rescue_action(e) raise e end
+ end
+
+ def setup
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ @controller = Target.new
+ end
+
+ ActionController::Base::DEPRECATED_INSTANCE_VARIABLES.each do |var|
+ class_eval <<-end_eval, __FILE__, __LINE__
+ def test_old_#{var}_is_deprecated
+ assert_deprecated('@#{var}') { get :old_#{var} }
+ end
+ def test_new_#{var}_isnt_deprecated
+ assert_not_deprecated { get :new_#{var} }
+ end
+ def test_internal_#{var}_isnt_deprecated
+ assert_not_deprecated { get :internal_#{var} }
+ end
+ end_eval
+ end
+end

0 comments on commit 2399a22

Please sign in to comment.
Something went wrong with that request. Please try again.