Class: ActiveRecord::SessionStore::SqlBypass
- Inherits:
-
Object
- Object
- ActiveRecord::SessionStore::SqlBypass
- Extended by:
- ClassMethods
- Defined in:
- lib/active_record/session_store.rb
Overview
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
ActiveSupport::Base64.encode64(Marshal.dump(data))
and unmarshaling data is
Marshal.load(ActiveSupport::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.
Constant Summary collapse
- @@table_name =
'sessions'
- @@session_id_column =
'session_id'
- @@data_column =
'data'
Instance Attribute Summary collapse
-
#data ⇒ Object
Lazy-unmarshal session state.
-
#new_record ⇒ Object
(also: #new_record?)
readonly
Returns the value of attribute new_record.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Class Method Summary collapse
- .connection ⇒ Object
-
.find_by_session_id(session_id) ⇒ Object
Look up a session by id and unmarshal its data if found.
Instance Method Summary collapse
-
#connection ⇒ Object
:singleton-method: Use the ActiveRecord::Base.connection by default.
-
#data_column ⇒ Object
:singleton-method: The data field defaults to ‘data’.
- #destroy ⇒ Object
-
#initialize(attributes) ⇒ SqlBypass
constructor
Look for normal and marshaled data, self.find_by_session_id’s way of telling us to postpone unmarshaling until the data is requested.
- #loaded? ⇒ Boolean
- #save ⇒ Object
-
#session_id_column ⇒ Object
:singleton-method: The session id field defaults to ‘session_id’.
-
#table_name ⇒ Object
:singleton-method: The table name defaults to ‘sessions’.
Methods included from ClassMethods
create_table!, drop_table!, marshal, unmarshal
Constructor Details
#initialize(attributes) ⇒ SqlBypass
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.
227 228 229 230 231 232 |
# File 'lib/active_record/session_store.rb', line 227 def initialize(attributes) @session_id = attributes[:session_id] @data = attributes[:data] @marshaled_data = attributes[:marshaled_data] @new_record = @marshaled_data.nil? end |
Instance Attribute Details
#data ⇒ Object
Lazy-unmarshal session state.
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/active_record/session_store.rb', line 235 def data unless @data if @marshaled_data @data, @marshaled_data = self.class.unmarshal(@marshaled_data) || {}, nil else @data = {} end end @data end |
#new_record ⇒ Object (readonly) Also known as: new_record?
Returns the value of attribute new_record.
219 220 221 |
# File 'lib/active_record/session_store.rb', line 219 def new_record @new_record end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
219 220 221 |
# File 'lib/active_record/session_store.rb', line 219 def session_id @session_id end |
Class Method Details
.connection ⇒ Object
207 208 209 |
# File 'lib/active_record/session_store.rb', line 207 def connection @@connection ||= ActiveRecord::Base.connection end |
.find_by_session_id(session_id) ⇒ Object
Look up a session by id and unmarshal its data if found.
212 213 214 215 216 |
# File 'lib/active_record/session_store.rb', line 212 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 |
Instance Method Details
#connection ⇒ Object
:singleton-method: Use the ActiveRecord::Base.connection by default.
183 |
# File 'lib/active_record/session_store.rb', line 183 cattr_accessor :connection |
#data_column ⇒ Object
:singleton-method: The data field defaults to ‘data’.
200 |
# File 'lib/active_record/session_store.rb', line 200 cattr_accessor :data_column |
#destroy ⇒ Object
274 275 276 277 278 279 280 281 282 |
# File 'lib/active_record/session_store.rb', line 274 def destroy return if @new_record connect = connection connect.delete <<-end_sql, 'Destroy session' DELETE FROM #{table_name} WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)} end_sql end |
#loaded? ⇒ Boolean
246 247 248 |
# File 'lib/active_record/session_store.rb', line 246 def loaded? @data end |
#save ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/active_record/session_store.rb', line 250 def save return false unless loaded? marshaled_data = self.class.marshal(data) connect = connection if @new_record @new_record = false connect.update <<-end_sql, 'Create session' 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)} ) end_sql else connect.update <<-end_sql, 'Update session' 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)} end_sql end end |
#session_id_column ⇒ Object
:singleton-method: The session id field defaults to ‘session_id’.
194 |
# File 'lib/active_record/session_store.rb', line 194 cattr_accessor :session_id_column |
#table_name ⇒ Object
:singleton-method: The table name defaults to ‘sessions’.
188 |
# File 'lib/active_record/session_store.rb', line 188 cattr_accessor :table_name |