Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2031

Show
Ignore:
Timestamp:
08/17/05 21:05:31 (3 years ago)
Author:
minam
Message:

Add an :if option to session management, to allow programmatically enabling or disabling the session

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/session_management.rb

    r1886 r2031  
    3333      #   session :only => :foo, :session_secure => true 
    3434      # 
     35      #   # the session will only be disabled for 'foo', and only if it is 
     36      #   # requested as a web service 
     37      #   session :off, :only => :foo, 
     38      #           :if => Proc.new { |req| req.parameters[:ws] } 
     39      # 
    3540      # All session options described for ActionController::Base.process_cgi 
    3641      # are valid arguments. 
     
    4853      end 
    4954 
    50       def session_options_for(action) #:nodoc: 
     55      def session_options_for(request, action) #:nodoc: 
    5156        options = {} 
    5257 
    5358        action = action.to_s 
    5459        (read_inheritable_attribute("session_options") || []).each do |opts| 
     60          next if opts[:if] && !opts[:if].call(request) 
    5561          if opts[:only] && opts[:only].include?(action) 
    5662            options.merge!(opts) 
     
    6470        options.delete :only 
    6571        options.delete :except 
     72        options.delete :if 
    6673 
    6774        options[:disabled] ? false : options 
     
    7178    def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc: 
    7279      action = request.parameters["action"] || "index" 
    73       request.session_options = self.class.session_options_for(action) 
     80      request.session_options = self.class.session_options_for(request, action) 
    7481      process_without_session_management_support(request, response, method, *arguments) 
    7582    end 
  • trunk/actionpack/test/controller/session_management_test.rb

    r1888 r2031  
    1717    session :off, :only => :show 
    1818    session :session_secure => true, :except => :show 
     19    session :off, :only => :conditional, 
     20            :if => Proc.new { |r| r.parameters[:ws] } 
    1921 
    2022    def show 
     
    2426    def tell 
    2527      render_text "done" 
     28    end 
     29 
     30    def conditional 
     31      render_text ">>>#{params[:ws]}<<<" 
    2632    end 
    2733  end 
     
    6874    assert_equal false, @request.session_options 
    6975  end 
     76 
     77  def test_session_off_with_if 
     78    @controller = TestController.new 
     79    get :conditional 
     80    assert_instance_of Hash, @request.session_options 
     81    get :conditional, :ws => "ws" 
     82    assert_equal false, @request.session_options 
     83  end 
    7084end