ActionDispatch::Session::AbstractStore
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.
# File lib/active_record/session_store.rb, line 336 336: def destroy_session(env, session_id, options) 337: if sid = current_session_id(env) 338: Base.silence do 339: get_session_model(env, sid).destroy 340: env[SESSION_RECORD_KEY] = nil 341: end 342: end 343: 344: generate_sid unless options[:drop] 345: end
# File lib/active_record/session_store.rb, line 355 355: def find_session(id) 356: @@session_class.find_by_session_id(id) || 357: @@session_class.new(:session_id => id, :data => {}) 358: end
# File lib/active_record/session_store.rb, line 306 306: def get_session(env, sid) 307: Base.silence do 308: unless sid and session = @@session_class.find_by_session_id(sid) 309: # If the sid was nil or if there is no pre-existing session under the sid, 310: # force the generation of a new sid and associate a new session associated with the new sid 311: sid = generate_sid 312: session = @@session_class.new(:session_id => sid, :data => {}) 313: end 314: env[SESSION_RECORD_KEY] = session 315: [sid, session.data] 316: end 317: end
# File lib/active_record/session_store.rb, line 347 347: def get_session_model(env, sid) 348: if env[ENV_SESSION_OPTIONS_KEY][:id].nil? 349: env[SESSION_RECORD_KEY] = find_session(sid) 350: else 351: env[SESSION_RECORD_KEY] ||= find_session(sid) 352: end 353: end
# File lib/active_record/session_store.rb, line 319 319: def set_session(env, sid, session_data, options) 320: Base.silence do 321: record = get_session_model(env, sid) 322: record.data = session_data 323: return false unless record.save 324: 325: session_data = record.data 326: if session_data && session_data.respond_to?(:each_value) 327: session_data.each_value do |obj| 328: obj.clear_association_cache if obj.respond_to?(:clear_association_cache) 329: end 330: end 331: end 332: 333: sid 334: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.