Permalink
Browse files

define defalut value to session.data

  • Loading branch information...
1 parent 6329847 commit d993a94a434e5b2d557df478e90f2d55c0f945b1 @authorNari committed Jan 17, 2011
Showing with 32 additions and 12 deletions.
  1. +32 −12 lib/sequel-rails/session_store.rb
@@ -15,30 +15,50 @@ class Session < ::Sequel::Model
# property :updated_at, DateTime, :required => false, :index => true
class << self
-
+
def auto_migrate!
self.db.create_table :sessions do
primary_key :id
- column :session_id, String,
- :null => false,
- :unique => true,
+ column :session_id, String,
+ :null => false,
+ :unique => true,
:index => true
- column :data, :text,
- :null => false
-
- column :updated_at, DateTime,
- :null => true,
+ column :data, :text,
+ :null => false
+
+ column :updated_at, DateTime,
+ :null => true,
:index => true
end
end
-
+
+ def marshal(data)
+ ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
+ end
+
+ def unmarshal(data)
+ Marshal.load(ActiveSupport::Base64.decode64(data)) if data
+ end
end
def self.name
'session'
end
+ def data
+ self[:data] ||= self.class.unmarshal(self[:data]) || {}
+ end
+
+ def marshal_data!
+ return false unless self[:data]
+ self[:data] = self.class.marshal(data)
+ end
+
+ def before_save
+ marshal_data!
+ super
+ end
end
SESSION_RECORD_KEY = 'rack.session.record'.freeze
@@ -58,7 +78,7 @@ def get_session(env, sid)
def set_session(env, sid, session_data)
session = get_session_resource(env, sid)
session.data = session_data
- session.updated_at = Time.now if session.dirty?
+ session.updated_at = Time.now if session.modified?
session.save
end
@@ -72,7 +92,7 @@ def get_session_resource(env, sid)
def find_session(sid)
klass = self.class.session_class
-
+
klass.where(:session_id => sid).first || klass.new(:session_id => sid)
end

0 comments on commit d993a94

Please sign in to comment.