aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/lib/action_controller/session/active_record_store.rb
blob: 9e0db037d0db0a2618f12d65eabb84e5018cfb2d (plain) (tree)






























                                                                                                               



                                                                                                                            
























                                                                     
                                                                              







                                                                   
begin

require 'active_record'
require 'cgi'
require 'cgi/session'

# Contributed by Tim Bates
class CGI
  class Session
    # ActiveRecord database based session storage class.
    #
    # Implements session storage in a database using the ActiveRecord ORM library. Assumes that the database
    # has a table called +sessions+ with columns +id+ (numeric, primary key), +sessid+ and +data+ (text).
    # The session data is stored in the +data+ column in YAML format; the user is responsible for ensuring that
    # only data that can be YAMLized is stored in the session.
    class ActiveRecordStore
      # The ActiveRecord class which corresponds to the database table.
      class Session < ActiveRecord::Base
        serialize :data
        # Isn't this class definition beautiful?
      end

      # Create a new ActiveRecordStore instance. This constructor is used internally by CGI::Session.
      # The user does not generally need to call it directly.
      #
      # +session+ is the session for which this instance is being created.
      #
      # +option+ is currently ignored as no options are recognized.
      #
      # This session's ActiveRecord database row will be created if it does not exist, or opened if it does.
      def initialize(session, option=nil)
        ActiveRecord::Base.silence do
          @session = Session.find_by_sessid(session.session_id) || Session.new("sessid" => session.session_id, "data" => {})
          @data    = @session.data
        end
      end

      # Update and close the session's ActiveRecord object.
      def close
        return unless @session
        update
        @session = nil
      end

      # Close and destroy the session's ActiveRecord object.
      def delete
        return unless @session
        @session.destroy
        @session = nil
      end

      # Restore session state from the session's ActiveRecord object.
      def restore
        return unless @session
        @data = @session.data
      end

      # Save session state in the session's ActiveRecord object.
      def update
        return unless @session
        ActiveRecord::Base.silence { @session.update_attribute "data", @data }
      end
    end #ActiveRecordStore
  end #Session
end #CGI

rescue LoadError
  # Couldn't load Active Record, so don't make this store available
end