diff options
author | Jamis Buck <jamis@37signals.com> | 2005-08-17 21:05:31 +0000 |
---|---|---|
committer | Jamis Buck <jamis@37signals.com> | 2005-08-17 21:05:31 +0000 |
commit | c031a0163356502392f1e6b2839eb248909f9def (patch) | |
tree | dc4b0f6e922fbe9b0489972d2960e7e1d5f9861e /actionpack | |
parent | 7ab71d389225ed9051de9252d82bcced997064cb (diff) | |
download | rails-c031a0163356502392f1e6b2839eb248909f9def.tar.gz rails-c031a0163356502392f1e6b2839eb248909f9def.tar.bz2 rails-c031a0163356502392f1e6b2839eb248909f9def.zip |
Add an :if option to session management, to allow programmatically enabling or disabling the session
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2031 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/session_management.rb | 11 | ||||
-rw-r--r-- | actionpack/test/controller/session_management_test.rb | 14 |
2 files changed, 23 insertions, 2 deletions
diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb index 8341575f64..e5c2258582 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session_management.rb @@ -32,6 +32,11 @@ module ActionController #:nodoc: # # 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 @@ module ActionController #:nodoc: 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,6 +69,7 @@ module ActionController #:nodoc: options.delete :only options.delete :except + options.delete :if options[:disabled] ? false : options end @@ -70,7 +77,7 @@ module ActionController #:nodoc: 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 diff --git a/actionpack/test/controller/session_management_test.rb b/actionpack/test/controller/session_management_test.rb index 707c4287f1..84cbd28387 100644 --- a/actionpack/test/controller/session_management_test.rb +++ b/actionpack/test/controller/session_management_test.rb @@ -16,6 +16,8 @@ class SessionManagementTest < Test::Unit::TestCase 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 @@ class SessionManagementTest < Test::Unit::TestCase def tell render_text "done" end + + def conditional + render_text ">>>#{params[:ws]}<<<" + end end class SpecializedController < SessionOffController @@ -67,4 +73,12 @@ class SessionManagementTest < Test::Unit::TestCase 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 |