Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / lib / action_controller / session_management.rb
blobfabb6e7f604a46fc93d4cc0483c57444adec9f15
1 require 'action_controller/session/cookie_store'
2 require 'action_controller/session/drb_store'
3 require 'action_controller/session/mem_cache_store'
4 if Object.const_defined?(:ActiveRecord)
5   require 'action_controller/session/active_record_store'
6 end
8 module ActionController #:nodoc:
9   module SessionManagement #:nodoc:
10     def self.included(base)
11       base.class_eval do
12         extend ClassMethods
13         alias_method_chain :process, :session_management_support
14         alias_method_chain :process_cleanup, :session_management_support
15       end
16     end
18     module ClassMethods
19       # Set the session store to be used for keeping the session data between requests. By default, sessions are stored
20       # in browser cookies (:cookie_store), but you can also specify one of the other included stores
21       # (:active_record_store, :p_store, drb_store, :mem_cache_store, or :memory_store) or your own custom class.
22       def session_store=(store)
23         ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] =
24           store.is_a?(Symbol) ? CGI::Session.const_get(store == :drb_store ? "DRbStore" : store.to_s.camelize) : store
25       end
27       # Returns the session store class currently used.
28       def session_store
29         ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager]
30       end
32       # Returns the hash used to configure the session. Example use:
33       #
34       #   ActionController::Base.session_options[:session_secure] = true # session only available over HTTPS
35       def session_options
36         ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
37       end
38       
39       # Specify how sessions ought to be managed for a subset of the actions on
40       # the controller. Like filters, you can specify <tt>:only</tt> and
41       # <tt>:except</tt> clauses to restrict the subset, otherwise options
42       # apply to all actions on this controller.
43       #
44       # The session options are inheritable, as well, so if you specify them in
45       # a parent controller, they apply to controllers that extend the parent.
46       #
47       # Usage:
48       #
49       #   # turn off session management for all actions.
50       #   session :off
51       #
52       #   # turn off session management for all actions _except_ foo and bar.
53       #   session :off, :except => %w(foo bar)
54       #
55       #   # turn off session management for only the foo and bar actions.
56       #   session :off, :only => %w(foo bar)
57       #
58       #   # the session will only work over HTTPS, but only for the foo action
59       #   session :only => :foo, :session_secure => true
60       #
61       #   # the session will only be disabled for 'foo', and only if it is
62       #   # requested as a web service
63       #   session :off, :only => :foo,
64       #           :if => Proc.new { |req| req.parameters[:ws] }
65       #
66       #   # the session will be disabled for non html/ajax requests
67       #   session :off, 
68       #     :if => Proc.new { |req| !(req.format.html? || req.format.js?) }
69       #
70       # All session options described for ActionController::Base.process_cgi
71       # are valid arguments.
72       def session(*args)
73         options = args.extract_options!
75         options[:disabled] = true if !args.empty?
76         options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only]
77         options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except]
78         if options[:only] && options[:except]
79           raise ArgumentError, "only one of either :only or :except are allowed"
80         end
82         write_inheritable_array("session_options", [options])
83       end
85       # So we can declare session options in the Rails initializer.
86       alias_method :session=, :session
88       def cached_session_options #:nodoc:
89         @session_options ||= read_inheritable_attribute("session_options") || []
90       end
92       def session_options_for(request, action) #:nodoc:
93         if (session_options = cached_session_options).empty?
94           {}
95         else
96           options = {}
98           action = action.to_s
99           session_options.each do |opts|
100             next if opts[:if] && !opts[:if].call(request)
101             if opts[:only] && opts[:only].include?(action)
102               options.merge!(opts)
103             elsif opts[:except] && !opts[:except].include?(action)
104               options.merge!(opts)
105             elsif !opts[:only] && !opts[:except]
106               options.merge!(opts)
107             end
108           end
109           
110           if options.empty? then options
111           else
112             options.delete :only
113             options.delete :except
114             options.delete :if
115             options[:disabled] ? false : options
116           end
117         end
118       end
119     end
121     def process_with_session_management_support(request, response, method = :perform_action, *arguments) #:nodoc:
122       set_session_options(request)
123       process_without_session_management_support(request, response, method, *arguments)
124     end
126     private
127       def set_session_options(request)
128         request.session_options = self.class.session_options_for(request, request.parameters["action"] || "index")
129       end
130       
131       def process_cleanup_with_session_management_support
132         clear_persistent_model_associations
133         process_cleanup_without_session_management_support
134       end
136       # Clear cached associations in session data so they don't overflow
137       # the database field.  Only applies to ActiveRecordStore since there
138       # is not a standard way to iterate over session data.
139       def clear_persistent_model_associations #:doc:
140         if defined?(@_session) && @_session.respond_to?(:data)
141           session_data = @_session.data
143           if session_data && session_data.respond_to?(:each_value)
144             session_data.each_value do |obj|
145               obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
146             end
147           end
148         end
149       end
150   end