diff --git a/Gemfile b/Gemfile index c6a541d9..31571791 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,6 @@ gem "locomotive_liquid", ">= 2.1.3" gem "bson_ext", ">= 1.0.8" gem "mongoid", :git => 'http://github.com/mongoid/mongoid.git' gem "mongoid_acts_as_tree", "= 0.1.5" -gem "mongo_session_store", "= 2.0.0.pre" gem "warden" gem "devise", "= 1.1.2" gem "haml", "= 3.0.18" diff --git a/Gemfile.lock b/Gemfile.lock index 82678983..c544614d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -149,8 +149,6 @@ GEM mimetype-fu (0.1.2) mongo (1.0.9) bson (>= 1.0.5) - mongo_session_store (2.0.0.pre) - actionpack (~> 3.0) mongoid_acts_as_tree (0.1.5) bson (>= 0.20.1) mongoid (<= 2.0.0) @@ -249,7 +247,6 @@ DEPENDENCIES locomotive_liquid (>= 2.1.3) mimetype-fu mocha! - mongo_session_store (= 2.0.0.pre) mongoid! mongoid_acts_as_tree (= 0.1.5) mongrel diff --git a/lib/locomotive.rb b/lib/locomotive.rb index c722e343..684e11be 100644 --- a/lib/locomotive.rb +++ b/lib/locomotive.rb @@ -16,7 +16,7 @@ require 'locomotive/routing' require 'locomotive/regexps' require 'locomotive/render' -require 'mongo_session_store/mongoid' +require 'locomotive/session_store' module Locomotive diff --git a/lib/locomotive/session_store.rb b/lib/locomotive/session_store.rb new file mode 100644 index 00000000..1f9d3546 --- /dev/null +++ b/lib/locomotive/session_store.rb @@ -0,0 +1,57 @@ +module ActionDispatch + module Session + class MongoidStore < AbstractStore + + class Session + include Mongoid::Document + include Mongoid::Timestamps + + field :data, :type => String, :default => [Marshal.dump({})].pack("m*") + index :updated_at + end + + # The class used for session storage. + cattr_accessor :session_class + self.session_class = Session + + SESSION_RECORD_KEY = 'rack.session.record'.freeze + + private + def generate_sid + BSON::ObjectId.new + end + + def get_session(env, sid) + sid ||= generate_sid + session = find_session(sid) + env[SESSION_RECORD_KEY] = session + [sid, unpack(session.data)] + end + + def set_session(env, sid, session_data) + record = env[SESSION_RECORD_KEY] ||= find_session(sid) + record.data = pack(session_data) + # Rack spec dictates that set_session should return true or false + # depending on whether or not the session was saved or not. + # However, ActionPack seems to want a session id instead. + record.save ? sid : false + end + + def find_session(id) + id = BSON::ObjectId.from_string(id.to_s) + @@session_class.first(:conditions => { :_id => id }) || + @@session_class.new(:id => id) + end + + def pack(data) + [Marshal.dump(data)].pack("m*") + end + + def unpack(packed) + return nil unless packed + Marshal.load(packed.unpack("m*").first) + end + + end + end +end \ No newline at end of file diff --git a/locomotive_cms.gemspec b/locomotive_cms.gemspec index bfbee25f..821a271b 100644 --- a/locomotive_cms.gemspec +++ b/locomotive_cms.gemspec @@ -22,7 +22,6 @@ Gem::Specification.new do |s| s.add_dependency "bson_ext", ">= 1.0.8" s.add_dependency "mongoid", ">= 2.0.0.beta.18" s.add_dependency "mongoid_acts_as_tree", "= 0.1.5" - s.add_dependency "mongo_session_store", "= 2.0.0.pre" s.add_dependency "warden" s.add_dependency "devise", "= 1.1.2" s.add_dependency "haml", "= 3.0.18"