Parent

Files

Class Index [+]

Quicksearch

ActiveRecord::SessionStore

Active Record Session Store

A session store backed by an Active Record class. A default class is provided, but any object duck-typing to an Active Record Session class with text session_id and data attributes is sufficient.

The default assumes a sessions tables with columns:

  +id+ (numeric primary key),
  +session_id+ (text, or longtext if your session data exceeds 65K), and
  +data+ (text or longtext; careful if your session data exceeds 65KB).

The session_id column should always be indexed for speedy lookups. Session data is marshaled to the data column in Base64 format. If the data you write is larger than the column’s size limit, ActionController::SessionOverflowError will be raised.

You may configure the table name, primary key, and data column. For example, at the end of config/application.rb:

  ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
  ActiveRecord::SessionStore::Session.primary_key = 'session_id'
  ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data'

Note that setting the primary key to the session_id frees you from having a separate id column if you don’t want it. However, you must set session.model.id = session.session_id by hand! A before filter on ApplicationController is a good place.

Since the default class is a simple Active Record, you get timestamps for free if you add created_at and updated_at datetime columns to the sessions table, making periodic session expiration a snap.

You may provide your own session class implementation, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting

  ActiveRecord::SessionStore.session_class = MySessionClass

You must implement these methods:

  self.find_by_session_id(session_id)
  initialize(hash_of_session_id_and_data, options_hash = {})
  attr_reader :session_id
  attr_accessor :data
  save
  destroy

The example SqlBypass class is a generic SQL session store. You may use it as a basis for high-performance database-specific stores.

Constants

SESSION_RECORD_KEY
ENV_SESSION_OPTIONS_KEY

Private Instance Methods

destroy_session(env, session_id, options) click to toggle source
     # File lib/active_record/session_store.rb, line 334
334:       def destroy_session(env, session_id, options)
335:         if sid = current_session_id(env)
336:           Base.silence do
337:             get_session_model(env, sid).destroy
338:             env[SESSION_RECORD_KEY] = nil
339:           end
340:         end
341: 
342:         generate_sid unless options[:drop]
343:       end
find_session(id) click to toggle source
     # File lib/active_record/session_store.rb, line 353
353:       def find_session(id)
354:         @@session_class.find_by_session_id(id) ||
355:           @@session_class.new(:session_id => id, :data => {})
356:       end
get_session(env, sid) click to toggle source
     # File lib/active_record/session_store.rb, line 304
304:       def get_session(env, sid)
305:         Base.silence do
306:           unless sid and session = @@session_class.find_by_session_id(sid)
307:             # If the sid was nil or if there is no pre-existing session under the sid,
308:             # force the generation of a new sid and associate a new session associated with the new sid
309:             sid = generate_sid
310:             session = @@session_class.new(:session_id => sid, :data => {})
311:           end
312:           env[SESSION_RECORD_KEY] = session
313:           [sid, session.data]
314:         end
315:       end
get_session_model(env, sid) click to toggle source
     # File lib/active_record/session_store.rb, line 345
345:       def get_session_model(env, sid)
346:         if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
347:           env[SESSION_RECORD_KEY] = find_session(sid)
348:         else
349:           env[SESSION_RECORD_KEY] ||= find_session(sid)
350:         end
351:       end
set_session(env, sid, session_data, options) click to toggle source
     # File lib/active_record/session_store.rb, line 317
317:       def set_session(env, sid, session_data, options)
318:         Base.silence do
319:           record = get_session_model(env, sid)
320:           record.data = session_data
321:           return false unless record.save
322: 
323:           session_data = record.data
324:           if session_data && session_data.respond_to?(:each_value)
325:             session_data.each_value do |obj|
326:               obj.clear_association_cache if obj.respond_to?(:clear_association_cache)
327:             end
328:           end
329:         end
330: 
331:         sid
332:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.