Class: ActiveRecord::SessionStore::SqlBypass

### Home Classes Methods

### In Files

- lib/active_record/session_store.rb

### Parent

Object

### Methods

- ::connection
- ::connection_pool
- ::data_column
- ::data_column_name
- ::find_by_session_id
- ::new
- ::session_id_column
- ::table_name
- #data
- #destroy
- #loaded?
- #save

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- ActiveRecord
- ActiveRecord::ActiveRecordError
- ActiveRecord::AdapterNotFound
- ActiveRecord::AdapterNotSpecified
- ActiveRecord::Aggregations
- ActiveRecord::Aggregations::ClassMethods
- ActiveRecord::AssociationTypeMismatch
- ActiveRecord::Associations
- ActiveRecord::Associations::ClassMethods
- ActiveRecord::AttributeAssignment
- ActiveRecord::AttributeAssignment::ClassMethods
- ActiveRecord::AttributeAssignmentError
- ActiveRecord::AttributeMethods
- ActiveRecord::AttributeMethods::BeforeTypeCast
- ActiveRecord::AttributeMethods::ClassMethods
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead
- ActiveRecord::AttributeMethods::DeprecatedUnderscoreRead::ClassMethods
- ActiveRecord::AttributeMethods::Dirty
- ActiveRecord::AttributeMethods::PrimaryKey
- ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods
- ActiveRecord::AttributeMethods::Query
- ActiveRecord::AttributeMethods::Read
- ActiveRecord::AttributeMethods::Read::ClassMethods
- ActiveRecord::AttributeMethods::Serialization
- ActiveRecord::AttributeMethods::Serialization::Attribute
- ActiveRecord::AttributeMethods::Serialization::ClassMethods
- ActiveRecord::AttributeMethods::TimeZoneConversion
- ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods
- ActiveRecord::AttributeMethods::Write
- ActiveRecord::AttributeMethods::Write::ClassMethods
- ActiveRecord::AutosaveAssociation
- ActiveRecord::AutosaveAssociation::ClassMethods
- ActiveRecord::Base
- ActiveRecord::Batches
- ActiveRecord::Calculations
- ActiveRecord::Callbacks
- ActiveRecord::Coders
- ActiveRecord::ConfigurationError
- ActiveRecord::ConnectionAdapters
- ActiveRecord::ConnectionAdapters::AbstractAdapter
- ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
- ActiveRecord::ConnectionAdapters::ConnectionHandler
- ActiveRecord::ConnectionAdapters::ConnectionManagement
- ActiveRecord::ConnectionAdapters::ConnectionPool
- ActiveRecord::ConnectionAdapters::DatabaseLimits
- ActiveRecord::ConnectionAdapters::DatabaseStatements
- ActiveRecord::ConnectionAdapters::Mysql2Adapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter
- ActiveRecord::ConnectionAdapters::MysqlAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::Utils
- ActiveRecord::ConnectionAdapters::PostgreSQLColumn
- ActiveRecord::ConnectionAdapters::QueryCache
- ActiveRecord::ConnectionAdapters::Quoting
- ActiveRecord::ConnectionAdapters::SQLiteAdapter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::ExplainPrettyPrinter
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::StatementPool
- ActiveRecord::ConnectionAdapters::SQLiteAdapter::Version
- ActiveRecord::ConnectionAdapters::SchemaCache
- ActiveRecord::ConnectionAdapters::SchemaStatements
- ActiveRecord::ConnectionAdapters::StatementPool
- ActiveRecord::ConnectionAdapters::Table
- ActiveRecord::ConnectionAdapters::TableDefinition
- ActiveRecord::ConnectionNotEstablished
- ActiveRecord::ConnectionTimeoutError
- ActiveRecord::CounterCache
- ActiveRecord::DangerousAttributeError
- ActiveRecord::Delegation
- ActiveRecord::DynamicFinderMatch
- ActiveRecord::DynamicMatchers
- ActiveRecord::DynamicScopeMatch
- ActiveRecord::Explain
- ActiveRecord::FinderMethods
- ActiveRecord::Fixtures
- ActiveRecord::Fixtures::File
- ActiveRecord::Generators
- ActiveRecord::Generators::Migration
- ActiveRecord::Generators::MigrationGenerator
- ActiveRecord::Generators::ModelGenerator
- ActiveRecord::Generators::ObserverGenerator
- ActiveRecord::Generators::SessionMigrationGenerator
- ActiveRecord::IdentityMap
- ActiveRecord::IdentityMap::Middleware
- ActiveRecord::Inheritance
- ActiveRecord::Inheritance::ClassMethods
- ActiveRecord::Integration
- ActiveRecord::InvalidForeignKey
- ActiveRecord::IrreversibleMigration
- ActiveRecord::Locking
- ActiveRecord::Locking::Optimistic
- ActiveRecord::Locking::Optimistic::ClassMethods
- ActiveRecord::Locking::Pessimistic
- ActiveRecord::LogSubscriber
- ActiveRecord::Migration
- ActiveRecord::Migration::CommandRecorder
- ActiveRecord::MigrationProxy
- ActiveRecord::ModelSchema
- ActiveRecord::ModelSchema::ClassMethods
- ActiveRecord::MultiparameterAssignmentErrors
- ActiveRecord::NestedAttributes
- ActiveRecord::NestedAttributes::ClassMethods
- ActiveRecord::NestedAttributes::TooManyRecords
- ActiveRecord::Observer
- ActiveRecord::Persistence
- ActiveRecord::Persistence::ClassMethods
- ActiveRecord::PreparedStatementInvalid
- ActiveRecord::QueryCache
- ActiveRecord::QueryCache::ClassMethods
- ActiveRecord::QueryMethods
- ActiveRecord::Querying
- ActiveRecord::Railtie
- ActiveRecord::Railties
- ActiveRecord::Railties::ControllerRuntime
- ActiveRecord::Railties::ControllerRuntime::ClassMethods
- ActiveRecord::ReadOnlyRecord
- ActiveRecord::ReadonlyAttributes
- ActiveRecord::ReadonlyAttributes::ClassMethods
- ActiveRecord::RecordInvalid
- ActiveRecord::RecordNotFound
- ActiveRecord::RecordNotSaved
- ActiveRecord::RecordNotUnique
- ActiveRecord::Reflection
- ActiveRecord::Reflection::ClassMethods
- ActiveRecord::Reflection::MacroReflection
- ActiveRecord::Result
- ActiveRecord::Rollback
- ActiveRecord::Sanitization
- ActiveRecord::Sanitization::ClassMethods
- ActiveRecord::Schema
- ActiveRecord::Scoping
- ActiveRecord::Scoping::ClassMethods
- ActiveRecord::Scoping::Default
- ActiveRecord::Scoping::Default::ClassMethods
- ActiveRecord::Scoping::Named
- ActiveRecord::Scoping::Named::ClassMethods
- ActiveRecord::Serialization
- ActiveRecord::SerializationTypeMismatch
- ActiveRecord::SessionStore
- ActiveRecord::SessionStore::Session
- ActiveRecord::SessionStore::SqlBypass
- ActiveRecord::SpawnMethods
- ActiveRecord::StaleObjectError
- ActiveRecord::StatementInvalid
- ActiveRecord::Store
- ActiveRecord::Store::ClassMethods
- ActiveRecord::TestFixtures
- ActiveRecord::TestFixtures::ClassMethods
- ActiveRecord::ThrowResult
- ActiveRecord::Timestamp
- ActiveRecord::Transactions
- ActiveRecord::Transactions::ClassMethods
- ActiveRecord::Translation
- ActiveRecord::UnknownAttributeError
- ActiveRecord::UnknownPrimaryKey
- ActiveRecord::Validations
- ActiveRecord::Validations::AssociatedValidator
- ActiveRecord::Validations::ClassMethods
- ActiveRecord::Validations::UniquenessValidator
- ActiveRecord::WrappedDatabaseException
- Mysql
- Mysql::Result
- Mysql::Stmt
- Mysql::Time
- ArJdbcMySQL
- ArJdbcMySQL::Error

No matching classes.

# ActiveRecord::SessionStore::SqlBypass

A barebones session store which duck-types with the default session store but bypasses Active Record and issues SQL directly. This is an example session model class meant as a basis for your own classes.

The database connection, table name, and session id and data columns are configurable class attributes. Marshaling and unmarshaling are implemented as class methods that you may override. By default, marshaling data is

    ::Base64.encode64(Marshal.dump(data))

and unmarshaling data is

    Marshal.load(::Base64.decode64(data))

This marshaling behavior is intended to store the widest range of binary session data in a `text` column. For higher performance, store in a `blob` column instead and forgo the Base64 encoding.

### Attributes

connection[W]

Use the ActiveRecord::Base.connection by default.

connection_pool[W]

Use the ActiveRecord::Base.connection_pool by default.

data[W]
new_record[R]
new_record?[R]
session_id[R]

### Public Class Methods

connection() click to toggle source

    # File lib/active_record/session_store.rb, line 213
    def connection
      @connection ||= ActiveRecord::Base.connection
    end

connection_pool() click to toggle source

    # File lib/active_record/session_store.rb, line 217
    def connection_pool
      @connection_pool ||= ActiveRecord::Base.connection_pool
    end

data_column click to toggle source

The data field defaults to 'data'.

    # File lib/active_record/session_store.rb, line 201
    cattr_accessor :data_column

Also aliased as: data_column_name
data_column_name click to toggle source
Alias for: data_column
find_by_session_id(session_id) click to toggle source

Look up a session by id and unmarshal its data if found.

    # File lib/active_record/session_store.rb, line 222
    def find_by_session_id(session_id)
      if record = connection.select_one("SELECT * FROM #{@@table_name} WHERE #{@@session_id_column}=#{connection.quote(session_id)}")
        new(:session_id => session_id, :marshaled_data => record['data'])
      end
    end

new(attributes) click to toggle source

Look for normal and marshaled data, self.find_by_session_id's way of telling us to postpone unmarshaling until the data is requested. We need to handle a normal data attribute in case of a new record.

    # File lib/active_record/session_store.rb, line 239
    def initialize(attributes)
      @session_id     = attributes[:session_id]
      @data           = attributes[:data]
      @marshaled_data = attributes[:marshaled_data]
      @new_record     = @marshaled_data.nil?
    end

session_id_column click to toggle source

The session id field defaults to 'session_id'.

    # File lib/active_record/session_store.rb, line 195
    cattr_accessor :session_id_column

table_name click to toggle source

The table name defaults to 'sessions'.

    # File lib/active_record/session_store.rb, line 189
    cattr_accessor :table_name

### Public Instance Methods

data() click to toggle source

Lazy-unmarshal session state.

    # File lib/active_record/session_store.rb, line 247
    def data
      unless @data
        if @marshaled_data
          @data, @marshaled_data = self.class.unmarshal(@marshaled_data) || {}, nil
        else
          @data = {}
        end
      end
      @data
    end

destroy() click to toggle source

    # File lib/active_record/session_store.rb, line 286
    def destroy
      return if @new_record

      connect = connection
      connect.delete           DELETE FROM #{table_name}          WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)}, 'Destroy session'
    end

loaded?() click to toggle source

    # File lib/active_record/session_store.rb, line 258
    def loaded?
      @data
    end

save() click to toggle source

    # File lib/active_record/session_store.rb, line 262
    def save
      return false unless loaded?
      marshaled_data = self.class.marshal(data)
      connect        = connection

      if @new_record
        @new_record = false
        connect.update             INSERT INTO #{table_name} (              #{connect.quote_column_name(session_id_column)},              #{connect.quote_column_name(data_column)} )            VALUES (              #{connect.quote(session_id)},              #{connect.quote(marshaled_data)} ), 'Create session'
      else
        connect.update             UPDATE #{table_name}            SET #{connect.quote_column_name(data_column)}=#{connect.quote(marshaled_data)}            WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)}, 'Update session'
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.