Skip to content

Instantly share code, notes, and snippets.

@mariovisic
Forked from stevehodgkiss/native_columns_store.rb
Last active December 14, 2015 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariovisic/5027968 to your computer and use it in GitHub Desktop.
Save mariovisic/5027968 to your computer and use it in GitHub Desktop.
SSOServer::Application.config.session_store :active_record_store
ActiveRecord::SessionStore.session_class = UserSessionStore
require 'spec_helper'
describe UserSessionStore do
subject(:store) { UserSessionStore.new(session_id: '123') }
before do
store.data[:test] = 'test_data'
store.data[:sso_session_id] = '3'
store.save!
store.reload
end
it 'merges sso_session_id into #data' do
store.data[:sso_session_id].should eq '3'
end
it 'saves sso_session_id in separate column' do
store.sso_session_id.should eq '3'
end
it 'doesnt store sso_session_id in marshaled data column' do
marshaled_data = UserSessionStore.unmarshal(store.read_attribute(:data))
marshaled_data.count.should eq 1
marshaled_data.should have_key(:test)
end
end
class UserSessionStore < ActiveRecord::SessionStore::Session
def self.find_by_session_id(*args)
super
end
def data
@data ||= super.merge(:sso_session_id => sso_session_id)
end
private
def marshal_data!
return false unless loaded?
self.sso_session_id = data[:sso_session_id] if data[:sso_session_id].present?
write_attribute(@@data_column_name, self.class.marshal(data.except(:sso_session_id)))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment