aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rwxr-xr-xactionpack/examples/blog_controller.cgi4
-rwxr-xr-xactionpack/lib/action_controller/base.rb66
-rw-r--r--actionpack/lib/action_controller/components.rb7
-rw-r--r--actionpack/lib/action_controller/flash.rb23
-rw-r--r--actionpack/lib/action_controller/session_management.rb4
-rw-r--r--actionpack/test/controller/deprecated_instance_variables_test.rb33
7 files changed, 92 insertions, 47 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index c345755db1..f88dbd9d2e 100644
--- a/actionpack/CHANGELOG
+++ b/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]
diff --git a/actionpack/examples/blog_controller.cgi b/actionpack/examples/blog_controller.cgi
index e64fe85f0c..b0c14033a2 100755
--- a/actionpack/examples/blog_controller.cgi
+++ b/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 \ No newline at end of file
+end
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 339c0ceb11..03b89826ce 100755
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -42,8 +42,8 @@ module ActionController #:nodoc:
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 @@ module ActionController #:nodoc:
# def index
# @entries = Entry.find(:all)
# end
- #
+ #
# def sign
# Entry.create(params[:entry])
# redirect_to :action => "index"
@@ -300,8 +300,8 @@ module ActionController #:nodoc:
# 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 @@ module ActionController #:nodoc:
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 @@ module ActionController #:nodoc:
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 @@ module ActionController #:nodoc:
# 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 @@ module ActionController #:nodoc:
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 @@ module ActionController #:nodoc:
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 @@ module ActionController #:nodoc:
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
diff --git a/actionpack/lib/action_controller/components.rb b/actionpack/lib/action_controller/components.rb
index 4d5dc20ab1..c05b1eb068 100644
--- a/actionpack/lib/action_controller/components.rb
+++ b/actionpack/lib/action_controller/components.rb
@@ -111,16 +111,15 @@ module ActionController #:nodoc:
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
diff --git a/actionpack/lib/action_controller/flash.rb b/actionpack/lib/action_controller/flash.rb
index 623ce2f4ba..a166c1060c 100644
--- a/actionpack/lib/action_controller/flash.rb
+++ b/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 @@ module ActionController #: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 @@ module ActionController #:nodoc:
# <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
diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb
index d953b5b766..ad1b530d6f 100644
--- a/actionpack/lib/action_controller/session_management.rb
+++ b/actionpack/lib/action_controller/session_management.rb
@@ -128,8 +128,8 @@ module ActionController #:nodoc:
# 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|
diff --git a/actionpack/test/controller/deprecated_instance_variables_test.rb b/actionpack/test/controller/deprecated_instance_variables_test.rb
new file mode 100644
index 0000000000..81e7ba1324
--- /dev/null
+++ b/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