Permalink
Browse files

Add an :if option to session management, to allow programmatically en…

…abling or disabling the session

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2031 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 7ab71d3 commit c031a0163356502392f1e6b2839eb248909f9def @jamis jamis committed Aug 17, 2005
@@ -32,6 +32,11 @@ module ClassMethods
# # the session will only work over HTTPS, but only for the foo action
# session :only => :foo, :session_secure => true
#
+ # # the session will only be disabled for 'foo', and only if it is
+ # # requested as a web service
+ # session :off, :only => :foo,
+ # :if => Proc.new { |req| req.parameters[:ws] }
+ #
# All session options described for ActionController::Base.process_cgi
# are valid arguments.
def session(*args)
@@ -47,11 +52,12 @@ def session(*args)
write_inheritable_array("session_options", [options])
end
- def session_options_for(action) #:nodoc:
+ def session_options_for(request, action) #:nodoc:
options = {}
action = action.to_s
(read_inheritable_attribute("session_options") || []).each do |opts|
+ next if opts[:if] && !opts[:if].call(request)
if opts[:only] && opts[:only].include?(action)
options.merge!(opts)
elsif opts[:except] && !opts[:except].include?(action)
@@ -63,14 +69,15 @@ def session_options_for(action) #:nodoc:
options.delete :only
options.delete :except
+ options.delete :if
options[:disabled] ? false : options
end
end
def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc:
action = request.parameters["action"] || "index"
- request.session_options = self.class.session_options_for(action)
+ request.session_options = self.class.session_options_for(request, action)
process_without_session_management_support(request, response, method, *arguments)
end
end
@@ -16,6 +16,8 @@ def tell
class TestController < ActionController::Base
session :off, :only => :show
session :session_secure => true, :except => :show
+ session :off, :only => :conditional,
+ :if => Proc.new { |r| r.parameters[:ws] }
def show
render_text "done"
@@ -24,6 +26,10 @@ def show
def tell
render_text "done"
end
+
+ def conditional
+ render_text ">>>#{params[:ws]}<<<"
+ end
end
class SpecializedController < SessionOffController
@@ -67,4 +73,12 @@ def test_controller_specialization_overrides_settings
get :another
assert_equal false, @request.session_options
end
+
+ def test_session_off_with_if
+ @controller = TestController.new
+ get :conditional
+ assert_instance_of Hash, @request.session_options
+ get :conditional, :ws => "ws"
+ assert_equal false, @request.session_options
+ end
end

0 comments on commit c031a01

Please sign in to comment.