jbarnette-intercession 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +61 -0
- data/Rakefile +23 -0
- data/install.rb +11 -0
- data/lib/intercession/lifecycle.rb +17 -0
- data/rails/init.rb +2 -0
- data/session.template +22 -0
- metadata +58 -0
data/README.markdown
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Intercession
|
2
|
+
|
3
|
+
Treat your sessions like models, not hashes. Intercession mixes a module into
|
4
|
+
the session on each request, allowing you to nicely encapsulate (and test!)
|
5
|
+
lots of user and session-specific behavior.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Intercession works as a plugin or a gem. I've only used it as a plugin against
|
10
|
+
Rails 2.2, though, so YMMV. If you install it as a plugin, it'll create you a
|
11
|
+
stub session module in `app/models/transient/session.rb`.
|
12
|
+
|
13
|
+
As a plugin:
|
14
|
+
|
15
|
+
$ script/plugin install git://github.com/jbarnette/intercession.git
|
16
|
+
|
17
|
+
As a Gem:
|
18
|
+
|
19
|
+
$ [sudo] gem install jbarnette-intercession --source http://gems.github.com
|
20
|
+
|
21
|
+
## Example
|
22
|
+
|
23
|
+
Check out [this gist](http://gist.github.com/44506).
|
24
|
+
|
25
|
+
## Testing
|
26
|
+
|
27
|
+
Unit tests for your session model! Mix it in to a Hash and go to town:
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@session = Hash.new
|
31
|
+
@session.extend Transient::Session
|
32
|
+
end
|
33
|
+
|
34
|
+
test "getting user will find user from user_id key if available" do
|
35
|
+
assert_nil @session.user
|
36
|
+
@session[:user_id] = users(:default).id
|
37
|
+
assert_equal users(:default), @session.user
|
38
|
+
end
|
39
|
+
|
40
|
+
## License and Author
|
41
|
+
|
42
|
+
Copyright 2009 John Barnette (jbarnette@gmail.com)
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
45
|
+
a copy of this software and associated documentation files (the
|
46
|
+
"Software"), to deal in the Software without restriction, including
|
47
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
48
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
49
|
+
permit persons to whom the Software is furnished to do so, subject to
|
50
|
+
the following conditions:
|
51
|
+
|
52
|
+
The above copyright notice and this permission notice shall be
|
53
|
+
included in all copies or substantial portions of the Software.
|
54
|
+
|
55
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
56
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
57
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
58
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
59
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
60
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
61
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# require 'rake'
|
2
|
+
# require 'rake/testtask'
|
3
|
+
# require 'rake/rdoctask'
|
4
|
+
#
|
5
|
+
# desc 'Default: run unit tests.'
|
6
|
+
# task :default => :test
|
7
|
+
#
|
8
|
+
# desc 'Test the intercession plugin.'
|
9
|
+
# Rake::TestTask.new(:test) do |t|
|
10
|
+
# t.libs << 'lib'
|
11
|
+
# t.libs << 'test'
|
12
|
+
# t.pattern = 'test/**/*_test.rb'
|
13
|
+
# t.verbose = true
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# desc 'Generate documentation for the intercession plugin.'
|
17
|
+
# Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
# rdoc.rdoc_dir = 'rdoc'
|
19
|
+
# rdoc.title = 'Intercession'
|
20
|
+
# rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
# rdoc.rdoc_files.include('README')
|
22
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
# end
|
data/install.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
source = File.join File.dirname(__FILE__), "session.template"
|
2
|
+
relative = File.join "app", "models", "transient", "session.rb"
|
3
|
+
destination = File.join Rails.root, relative
|
4
|
+
|
5
|
+
if File.exist? destination
|
6
|
+
puts "#{relative} already exists, so I won't overwrite it."
|
7
|
+
else
|
8
|
+
FileUtils.mkdir_p File.dirname(destination)
|
9
|
+
FileUtils.cp source, destination
|
10
|
+
puts "Installed an empty session module in #{destination}. Go edit it!"
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Intercession
|
2
|
+
module Lifecycle
|
3
|
+
def self.included klass
|
4
|
+
klass.prepend_before_filter :intercede_before
|
5
|
+
klass.append_after_filter :intercede_after
|
6
|
+
end
|
7
|
+
|
8
|
+
def intercede_before
|
9
|
+
session.extend Transient::Session
|
10
|
+
session.after_initialize if session.respond_to? :after_initialize
|
11
|
+
end
|
12
|
+
|
13
|
+
def intercede_after
|
14
|
+
session.before_save if session.respond_to? :before_save
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/rails/init.rb
ADDED
data/session.template
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Transient
|
2
|
+
|
3
|
+
# This module gets mixed in to the Rails session, and lets you
|
4
|
+
# treat the session more like a real object. Note that while most things
|
5
|
+
# this module is mixed in to are Hashlike, they vary in capabilitites.
|
6
|
+
# Safest to assume that the index[] op is the only thing that's available.
|
7
|
+
|
8
|
+
module Session
|
9
|
+
|
10
|
+
# Called by a before_filter in the application controller. A good place
|
11
|
+
# to eagerly populate member models from IDs, if you're not feeling lazy.
|
12
|
+
|
13
|
+
def after_initialize
|
14
|
+
end
|
15
|
+
|
16
|
+
# Called by an after_filter in the application controller. A good
|
17
|
+
# opportunity to turn heavy lists of stuff into IDs, etc.
|
18
|
+
|
19
|
+
def before_save
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jbarnette-intercession
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Barnette
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-08 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: jbarnette@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- install.rb
|
26
|
+
- lib/intercession/lifecycle.rb
|
27
|
+
- rails/init.rb
|
28
|
+
- Rakefile
|
29
|
+
- README.markdown
|
30
|
+
- session.template
|
31
|
+
has_rdoc: false
|
32
|
+
homepage: http://github.com/jbarnette/intercession
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: Your session loves you. Treat her like a lady.
|
57
|
+
test_files: []
|
58
|
+
|