Skip to content
Browse files

imported options, switched to object composition

  • Loading branch information...
1 parent 976e7e4 commit 08307383482b5dd4894589de2edf4bb62821d0fd @tenderlove tenderlove committed May 2, 2012
Showing with 56 additions and 13 deletions.
  1. +56 −13 actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
View
69 actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -18,19 +18,6 @@ def initialize(const_error)
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 @@ class AbstractStore < Rack::Session::Abstract::ID
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

0 comments on commit 0830738

Please sign in to comment.
Something went wrong with that request. Please try again.