Class: ActionDispatch::Session::AbstractStore

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/middleware/session/abstract_store.rb

Direct Known Subclasses

CookieStore, MemCacheStore

Defined Under Namespace

Classes: OptionsHash, SessionHash

Constant Summary collapse

ENV_SESSION_KEY =
'rack.session'.freeze
ENV_SESSION_OPTIONS_KEY =
'rack.session.options'.freeze
DEFAULT_OPTIONS =
{
  :key =>           '_session_id',
  :path =>          '/',
  :domain =>        nil,
  :expire_after =>  nil,
  :secure =>        false,
  :httponly =>      true,
  :cookie_only =>   true
}

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ AbstractStore

Returns a new instance of AbstractStore.



139
140
141
142
143
144
145
# File 'lib/action_dispatch/middleware/session/abstract_store.rb', line 139

def initialize(app, options = {})
  @app = app
  @default_options = DEFAULT_OPTIONS.merge(options)
  @key = @default_options.delete(:key).freeze
  @cookie_only = @default_options.delete(:cookie_only)
  ensure_session_key!
end

Instance Method Details

#call(env) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/action_dispatch/middleware/session/abstract_store.rb', line 147

def call(env)
  prepare!(env)
  response = @app.call(env)

  session_data = env[ENV_SESSION_KEY]
  options = env[ENV_SESSION_OPTIONS_KEY]

  if !session_data.is_a?(AbstractStore::SessionHash) || session_data.loaded? || options[:expire_after]
    session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.loaded?

    sid = options[:id] || generate_sid
    session_data = session_data.to_hash

    value = set_session(env, sid, session_data)
    return response unless value

    cookie = { :value => value }
    unless options[:expire_after].nil?
      cookie[:expires] = Time.now + options.delete(:expire_after)
    end

    request = ActionDispatch::Request.new(env)
    set_cookie(request, cookie.merge!(options))
  end

  response
end