mongoid3_session_store 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +35 -0
- data/lib/mongoid3_session_store/mongoid_store.rb +82 -0
- data/lib/mongoid3_session_store.rb +16 -0
- data/lib/tasks/mongoid3_session_store_tasks.rake +10 -0
- metadata +86 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= mongoid_session_store
|
2
|
+
|
3
|
+
Store rails 3.1+ sessions using Mongoid 3.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
This gem supports rails 3.1+ and mongoid 3.0
|
7
|
+
N.B. Mongoid 3 requires ruby 1.9.3. See http://mongoid.org/ for current requirements.
|
8
|
+
|
9
|
+
== Setup
|
10
|
+
|
11
|
+
In your Gemfile:
|
12
|
+
|
13
|
+
gem "mongoid", "~> 3.0.0"
|
14
|
+
gem "mongoid3_session_store", git: 'git@github.com:petercip/mongoid3_session_store.git'
|
15
|
+
|
16
|
+
In the session_store initializer (config/initializers/session_store.rb) you can comment out every line as
|
17
|
+
mongoid session store will be setup for use when added to the gemfile.
|
18
|
+
|
19
|
+
If you want to be more explicit you can comment the currently setup store and add:
|
20
|
+
|
21
|
+
Example::Application.config.session_store :mongoid_store
|
22
|
+
|
23
|
+
== Rake Tasks
|
24
|
+
Mongoid Session Store comes with a rake task to clear out the sessions it stores in mongoDB
|
25
|
+
|
26
|
+
rake db:mongoid:sessions:clear
|
27
|
+
|
28
|
+
== Note on Patches/Pull Requests
|
29
|
+
|
30
|
+
* Fork the project.
|
31
|
+
* Make your feature addition or bug fix.
|
32
|
+
* Add tests for it. This is important so I don't break it in a
|
33
|
+
future version unintentionally.
|
34
|
+
* Commit, do not mess with rakefile, version, or history.
|
35
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
36
|
+
* Send me a pull request. Bonus points for topic branches.
|
37
|
+
|
38
|
+
== Copyright
|
39
|
+
|
40
|
+
Copyright (c) 2010 Peter Cipollone. Based largely on mongoid_session_store, copyright (c) 2010 Ryan Fitzgerald. See MIT-LICENSE for details.
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
begin
|
9
|
+
require 'rdoc/task'
|
10
|
+
rescue LoadError
|
11
|
+
require 'rdoc/rdoc'
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
RDoc::Task = Rake::RDocTask
|
14
|
+
end
|
15
|
+
|
16
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'MongoidSessionStore'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README.rdoc')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
|
27
|
+
Rake::TestTask.new(:test) do |t|
|
28
|
+
t.libs << 'lib'
|
29
|
+
t.libs << 'test'
|
30
|
+
t.pattern = 'test/**/*_test.rb'
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
task :default => :test
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module ActionDispatch
|
2
|
+
module Session
|
3
|
+
class MongoidStore < AbstractStore
|
4
|
+
|
5
|
+
class Session
|
6
|
+
include Mongoid::Document
|
7
|
+
store_in :collection => 'sessions'
|
8
|
+
|
9
|
+
field :sid, :type => String
|
10
|
+
field :data, :type => String, :default => [Marshal.dump({})].pack("m*")
|
11
|
+
index({ :sid => 1 }, { :unique => true })
|
12
|
+
end
|
13
|
+
|
14
|
+
# The class used for session storage.
|
15
|
+
cattr_accessor :session_class
|
16
|
+
self.session_class = Session
|
17
|
+
|
18
|
+
SESSION_RECORD_KEY = 'rack.session.record'
|
19
|
+
ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY if ::Rails.version >= "3.1"
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def get_session(env, sid)
|
24
|
+
sid ||= generate_sid
|
25
|
+
session = find_session(sid)
|
26
|
+
env[SESSION_RECORD_KEY] = session
|
27
|
+
[sid, unpack(session.data)]
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_session(env, sid, session_data, options = nil)
|
31
|
+
record = get_session_model(env, sid)
|
32
|
+
record.data = pack(session_data)
|
33
|
+
|
34
|
+
# Rack spec dictates that set_session should return true or false
|
35
|
+
# depending on whether or not the session was saved or not.
|
36
|
+
# However, ActionPack seems to want a session id instead.
|
37
|
+
record.save ? sid : false
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_session(id)
|
41
|
+
@@session_class.find_or_create_by(:sid => id)
|
42
|
+
end
|
43
|
+
|
44
|
+
# def destroy(env)
|
45
|
+
# if sid = current_session_id(env)
|
46
|
+
# find_session(sid).destroy
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
|
50
|
+
def destroy(env)
|
51
|
+
destroy_session(env, current_session_id(env), {})
|
52
|
+
end
|
53
|
+
|
54
|
+
def destroy_session(env, session_id, options)
|
55
|
+
if sid = current_session_id(env)
|
56
|
+
get_session_model(env, sid).destroy
|
57
|
+
env[SESSION_RECORD_KEY] = nil
|
58
|
+
end
|
59
|
+
|
60
|
+
generate_sid unless options[:drop]
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_session_model(env, sid)
|
64
|
+
if env[ENV_SESSION_OPTIONS_KEY][:id].nil?
|
65
|
+
env[SESSION_RECORD_KEY] = find_session(sid)
|
66
|
+
else
|
67
|
+
env[SESSION_RECORD_KEY] ||= find_session(sid)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def pack(data)
|
72
|
+
[Marshal.dump(data)].pack("m*")
|
73
|
+
end
|
74
|
+
|
75
|
+
def unpack(packed)
|
76
|
+
return nil unless packed
|
77
|
+
Marshal.load(packed.unpack("m*").first)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module MongoidSessionStore
|
4
|
+
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
rake_tasks do
|
7
|
+
load "tasks/mongoid3_session_store_tasks.rake"
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer "setup mongoid session store" do |app|
|
11
|
+
require 'mongoid3_session_store/mongoid_store'
|
12
|
+
app.config.session_store :mongoid_store
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid3_session_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Cipollone
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mongoid
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.0'
|
46
|
+
description: Store rails 3.1+ sessions using Mongoid 3.
|
47
|
+
email:
|
48
|
+
- peter.cipollone@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- lib/mongoid3_session_store/mongoid_store.rb
|
54
|
+
- lib/mongoid3_session_store.rb
|
55
|
+
- lib/tasks/mongoid3_session_store_tasks.rake
|
56
|
+
- MIT-LICENSE
|
57
|
+
- Rakefile
|
58
|
+
- README.rdoc
|
59
|
+
homepage: http://github.com/petercip/mongoid3_session_store
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
hash: -3069011305836536140
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.24
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Store rails 3.1+ sessions using Mongoid 3.
|
86
|
+
test_files: []
|