Vendoring mongo_session_store so we have one less dependency

This commit is contained in:
Jacques Crocker 2010-09-28 02:19:55 -07:00
parent cc4c9ea4e9
commit 350aee82a8
5 changed files with 58 additions and 6 deletions

View File

@ -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"

View File

@ -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

View File

@ -16,7 +16,7 @@ require 'locomotive/routing'
require 'locomotive/regexps'
require 'locomotive/render'
require 'mongo_session_store/mongoid'
require 'locomotive/session_store'
module Locomotive

View File

@ -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

View File

@ -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"