Sha256: 24212ad260deb7b7f6398a1a532a12ee407e22fcc54ef57393a823a556ba9086

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

module ActionDispatch
  module Session
    class MongoidStore < AbstractStore
      
      class Session
        include Mongoid::Document
        include Mongoid::Timestamps
        
        store_in :sessions

        field :data, :type => String, :default => [Marshal.dump({})].pack("m*")
        field :session_id, :type => String, :allow_nil => false
        
        index :updated_at
        index :session_id
      end

      # The class used for session storage.
      cattr_accessor :session_class
      self.session_class = Session

      SESSION_RECORD_KEY = 'rack.session.record'.freeze

      private
            
        def get_session(env, sid)    
          session = find_session(sid)
          env[SESSION_RECORD_KEY] = session
          [sid, unpack(session.data)]
        end

        def set_session(env, sid, session_data)
          record = get_session_model(env, 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)   
          @@session_class.where(:session_id => id).first || @@session_class.new(:session_id => id)
        end
                
        def destroy(env)
          if sid = current_session_id(env)
            find_session(sid).destroy
          end
        end
        
        def get_session_model(env, sid)
          if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
            env[SESSION_RECORD_KEY] = find_session(sid)
          else
            env[SESSION_RECORD_KEY] ||= find_session(sid)
          end
        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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongoid_session_store-1.0.0 lib/mongoid_session_store/mongoid_store.rb
mongoid_session_store-1.0.0.beta2 lib/mongoid_session_store/mongoid_store.rb
mongoid_session_store-1.0.0.beta1 lib/mongoid_session_store/mongoid_store.rb
mongoid_session_store-0.0.2 lib/mongoid_session_store/mongoid_store.rb
mongoid_session_store-0.0.1 lib/mongoid_session_store/mongoid_store.rb