Class: CGI::Session::ActiveRecordStore

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/session/active_record_store.rb

Overview

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 may be used as the backing store.

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). Session data is marshaled to data. session_id should be indexed for speedy lookups.

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, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting

+CGI::Session::ActiveRecordStore.session_class = MySessionClass+

You must implement these methods:

self.find_by_session_id(session_id)
initialize(hash_of_session_id_and_data)
attr_reader :session_id
attr_accessor :data
save
destroy

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

If the data you are attempting to write to the data column is larger than the column’s size limit, ActionController::SessionOverflowError will be raised.

Defined Under Namespace

Classes: Session, SqlBypass

Constant Summary collapse

@@session_class =
Session

Instance Method Summary collapse

Constructor Details

#initialize(session, option = nil) ⇒ ActiveRecordStore

Find or instantiate a session given a CGI::Session.



270
271
272
273
274
275
276
277
278
# File 'lib/action_controller/session/active_record_store.rb', line 270

def initialize(session, option = nil)
  session_id = session.session_id
  unless @session = ActiveRecord::Base.silence { @@session_class.find_by_session_id(session_id) }
    unless session.new_session
      raise CGI::Session::NoSession, 'uninitialized session'
    end
    @session = @@session_class.new(:session_id => session_id, :data => {})
  end
end

Instance Method Details

#closeObject

Save and close the session store.



295
296
297
298
299
300
# File 'lib/action_controller/session/active_record_store.rb', line 295

def close
  if @session
    update
    @session = nil
  end
end

#deleteObject

Delete and close the session store.



303
304
305
306
307
308
# File 'lib/action_controller/session/active_record_store.rb', line 303

def delete
  if @session
    ActiveRecord::Base.silence { @session.destroy }
    @session = nil
  end
end

#restoreObject

Restore session state. The session model handles unmarshaling.



281
282
283
284
285
# File 'lib/action_controller/session/active_record_store.rb', line 281

def restore
  if @session
    @session.data
  end
end

#updateObject

Save session store.



288
289
290
291
292
# File 'lib/action_controller/session/active_record_store.rb', line 288

def update
  if @session
    ActiveRecord::Base.silence { @session.save }
  end
end