From 08307383482b5dd4894589de2edf4bb62821d0fd Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 2 May 2012 14:45:12 -0700 Subject: imported options, switched to object composition --- .../middleware/session/abstract_store.rb | 69 ++++++++++++++++++---- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb index e82132b445..56c71b962a 100644 --- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb @@ -18,19 +18,6 @@ module ActionDispatch end end - module DestroyableSession - def destroy - clear - options = @env[Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY] if @env - options ||= {} - @by.send(:destroy_session, @env, options[:id], options) if @by - options[:id] = nil - @loaded = false - end - end - - ::Rack::Session::Abstract::SessionHash.send :include, DestroyableSession - module Compatibility def initialize(app, options = {}) options[:key] ||= '_session_id' @@ -81,12 +68,68 @@ module ActionDispatch include Compatibility include StaleSessionCheck + ENV_SESSION_KEY = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc: + ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc: + private + module DestroyableSession + def destroy + clear + options = @env[Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY] if @env + options ||= {} + @by.send(:destroy_session, @env, options[:id], options) if @by + options[:id] = nil + @loaded = false + end + end + + ::Rack::Session::Abstract::SessionHash.send :include, DestroyableSession + + def prepare_session(env) + session_was = env[ENV_SESSION_KEY] + env[ENV_SESSION_KEY] = Rack::Session::Abstract::SessionHash.new(self, env) + env[ENV_SESSION_OPTIONS_KEY] = Request::Session::Options.new(self, env, @default_options) + env[ENV_SESSION_KEY].merge! session_was if session_was + end + def set_cookie(env, session_id, cookie) request = ActionDispatch::Request.new(env) request.cookie_jar[key] = cookie end end end + + class Request + # SessionHash is responsible to lazily load the session from store. + class Session + class Options #:nodoc: + def initialize(by, env, default_options) + @by = by + @env = env + @session_id_loaded = false + @delegate = default_options + end + + def [](key) + load_session_id! if key == :id && session_id_not_loaded? + @delegate[key] + end + + def []=(k,v); @delegate[k] = v; end + def to_hash; @delegate.dup; end + def values_at(*args) @delegate.values_at(*args); end + + private + def session_id_not_loaded? + !(@session_id_loaded || @delegate.key?(:id)) + end + + def load_session_id! + @delegate[:id] = @by.send(:extract_session_id, @env) + @session_id_loaded = true + end + end + end + end end -- cgit v1.2.3