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

Ticket #1763: sessman.diff

File sessman.diff, 6.3 kB (added by minam, 3 years ago)
  • test/controller/session_management_test.rb

    old new  
     1require File.dirname(__FILE__) + '/../abstract_unit' 
     2 
     3class SessionManagementTest < Test::Unit::TestCase 
     4  class SessionOffController < ActionController::Base 
     5    session :off 
     6 
     7    def show 
     8      render_text "done" 
     9    end 
     10 
     11    def tell 
     12      render_text "done" 
     13    end 
     14  end 
     15 
     16  class TestController < ActionController::Base 
     17    session :off, :only => :show 
     18    session :session_secure => true, :except => :show 
     19 
     20    def show 
     21      render_text "done" 
     22    end 
     23 
     24    def tell 
     25      render_text "done" 
     26    end 
     27  end 
     28 
     29  def setup 
     30    @request, @response = ActionController::TestRequest.new, 
     31      ActionController::TestResponse.new 
     32  end 
     33 
     34  def test_session_off_globally 
     35    @controller = SessionOffController.new 
     36    get :show 
     37    assert_equal false, @request.session_options 
     38    get :tell 
     39    assert_equal false, @request.session_options 
     40  end 
     41 
     42  def test_session_off_conditionally 
     43    @controller = TestController.new 
     44    get :show 
     45    assert_equal false, @request.session_options 
     46    get :tell 
     47    assert_instance_of Hash, @request.session_options 
     48    assert @request.session_options[:session_secure] 
     49  end 
     50end 
  • lib/action_controller/session_management.rb

    old new  
     1module ActionController #:nodoc: 
     2  module SessionManagement #:nodoc: 
     3    def self.append_features(base) 
     4      super 
     5      base.extend(ClassMethods) 
     6      base.class_eval do 
     7        alias_method :process_without_session_management_support, :process 
     8        alias_method :process, :process_with_session_management_support 
     9      end 
     10    end 
     11 
     12    module ClassMethods 
     13      # Specify how sessions ought to be managed for a subset of the actions on 
     14      # the controller. Like filters, you can specify <tt>:only</tt> and 
     15      # <tt>:except</tt> clauses to restrict the subset, otherwise options 
     16      # apply to all actions on this controller. 
     17      # 
     18      # The session options are inheritable, as well, so if you specify them in 
     19      # a parent controller, they apply to controllers that extend the parent. 
     20      # 
     21      # Usage: 
     22      # 
     23      #   # turn off session management for all actions. 
     24      #   session :off 
     25      # 
     26      #   # turn off session management for all actions _except_ foo and bar. 
     27      #   session :off, :except => %w(foo bar) 
     28      # 
     29      #   # turn off session management for only the foo and bar actions. 
     30      #   session :off, :only => %w(foo bar) 
     31      # 
     32      #   # the session will only work over HTTPS, but only for the foo action 
     33      #   session :only => :foo, :session_secure => true 
     34      # 
     35      # All session options described for ActionController::Base.process_cgi 
     36      # are valid arguments. 
     37      def session(*args) 
     38        options = Hash === args.last ? args.pop : {} 
     39 
     40        options[:disabled] = true if !args.empty? 
     41        options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only] 
     42        options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except] 
     43        if options[:only] && options[:except] 
     44          raise ArgumentError, "only one of either :only or :except are allowed" 
     45        end 
     46 
     47        write_inheritable_array("session_options", [options]) 
     48      end 
     49 
     50      def session_options_for(action) #:nodoc: 
     51        options = {} 
     52 
     53        action = action.to_s 
     54        (read_inheritable_attribute("session_options") || []).each do |opts| 
     55          if opts[:only] && opts[:only].include?(action) 
     56            options.merge!(opts) 
     57          elsif opts[:except] && !opts[:except].include?(action) 
     58            options.merge!(opts) 
     59          elsif !opts[:only] && !opts[:except] 
     60            options.merge!(opts) 
     61          end 
     62        end 
     63 
     64        options.delete :only 
     65        options.delete :except 
     66 
     67        options[:disabled] ? false : options 
     68      end 
     69    end 
     70 
     71    def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc: 
     72      action = request.parameters["action"] || "index" 
     73      request.session_options = self.class.session_options_for(action) 
     74      process_without_session_management_support(request, response, method, *arguments) 
     75    end 
     76  end 
     77end 
  • lib/action_controller/test_process.rb

    old new  
    1414  end 
    1515 
    1616  class TestRequest < AbstractRequest #:nodoc: 
    17     attr_accessor :cookies 
     17    attr_accessor :cookies, :session_options 
    1818    attr_accessor :query_parameters, :request_parameters, :path, :session, :env 
    1919    attr_accessor :host 
    2020 
  • lib/action_controller/cgi_process.rb

    old new  
    3737  end 
    3838 
    3939  class CgiRequest < AbstractRequest #:nodoc: 
    40     attr_accessor :cgi 
     40    attr_accessor :cgi, :session_options 
    4141 
    4242    DEFAULT_SESSION_OPTIONS = { 
    4343      :database_manager => CGI::Session::PStore, 
     
    115115    end 
    116116     
    117117    def reset_session 
    118       @session.delete 
     118      @session.delete if CGI::Session === @session 
    119119      @session = (@session_options == false ? {} : new_session) 
    120120    end 
    121121 
  • lib/action_controller.rb

    old new  
    5050require 'action_controller/verification' 
    5151require 'action_controller/streaming' 
    5252require 'action_controller/auto_complete' 
     53require 'action_controller/session_management' 
    5354 
    5455require 'action_view' 
    5556ActionController::Base.template_class = ActionView::Base 
     
    7172  include ActionController::Verification 
    7273  include ActionController::Streaming 
    7374  include ActionController::AutoComplete 
     75  include ActionController::SessionManagement 
    7476end