gabrielg-contexted_observers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +1 -0
- data/LICENSE +20 -0
- data/Manifest +17 -0
- data/README.markdown +22 -0
- data/Rakefile +8 -0
- data/contexted_observers.gemspec +36 -0
- data/lib/contexted_observers.rb +3 -0
- data/lib/thumblemonks/contexted_observers/context.rb +19 -0
- data/lib/thumblemonks/contexted_observers/modules.rb +3 -0
- data/lib/thumblemonks/contexted_observers/shortcuts.rb +15 -0
- data/rails/action_controller_extensions.rb +27 -0
- data/rails/active_record_observer_extensions.rb +4 -0
- data/rails/init.rb +2 -0
- data/test/action_controller_macro_test.rb +62 -0
- data/test/active_record_observer_test.rb +20 -0
- data/test/context_test.rb +47 -0
- data/test/test_helper.rb +7 -0
- metadata +94 -0
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
v0.1.0. Initial release.
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [Gabriel Gironda]
|
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/Manifest
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
contexted_observers.gemspec
|
3
|
+
lib/contexted_observers.rb
|
4
|
+
lib/thumblemonks/contexted_observers/context.rb
|
5
|
+
lib/thumblemonks/contexted_observers/modules.rb
|
6
|
+
lib/thumblemonks/contexted_observers/shortcuts.rb
|
7
|
+
LICENSE
|
8
|
+
Manifest
|
9
|
+
rails/action_controller_extensions.rb
|
10
|
+
rails/active_record_observer_extensions.rb
|
11
|
+
rails/init.rb
|
12
|
+
Rakefile
|
13
|
+
README.markdown
|
14
|
+
test/action_controller_macro_test.rb
|
15
|
+
test/active_record_observer_test.rb
|
16
|
+
test/context_test.rb
|
17
|
+
test/test_helper.rb
|
data/README.markdown
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Contexted Observers
|
2
|
+
|
3
|
+
I needed my Rails observers to have some context, and didn't want them to be aware of an entire controller or something equally jacked up. So here's something slightly less jacked up. At its basis, it provides a simple Context handler which has a stack of objects providing context. An example from the tests:
|
4
|
+
|
5
|
+
should "set the current context to be the last context hash" do
|
6
|
+
context, current_context = {:ashes => :ashes}, nil
|
7
|
+
ContextedObservers::Context.push_new(context) do
|
8
|
+
current_context = ContextedObservers::Context.current
|
9
|
+
end
|
10
|
+
assert_equal context, current_context
|
11
|
+
end
|
12
|
+
|
13
|
+
Current context becomes the hash {:ashes => :ashes}. Nesting another `push_new` will add another context to the stack and make it current. When combined with ActionController / ActiveRecord::Observer, it allows for things such as:
|
14
|
+
|
15
|
+
class BlockContextController < ActionController::Base
|
16
|
+
before_filter :load_community
|
17
|
+
contextualize :except => :bar do
|
18
|
+
{:community => @community}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
This provides a hash with the current community, in this example, as the current context. There's a shortcut method on ActiveRecord::Observer to access this context. This is because sometimes traversing the associations to get a piece of context isn't always possible / doesn't always make sense.
|
data/Rakefile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'echoe'
|
2
|
+
Echoe.new('contexted_observers') do |p|
|
3
|
+
p.author = "Gabriel Gironda"
|
4
|
+
p.summary = "A library that gives observers some context."
|
5
|
+
p.url = "http://github.com/gabrielg/contexted_observers"
|
6
|
+
p.install_message = "CHOOSY MOMS CHOOSE THUMBLE MONKS"
|
7
|
+
p.development_dependencies = ["thoughtbot-shoulda >= 2.0.6"]
|
8
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{contexted_observers}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Gabriel Gironda"]
|
9
|
+
s.date = %q{2008-12-24}
|
10
|
+
s.description = %q{A library that gives observers some context.}
|
11
|
+
s.email = %q{}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/contexted_observers.rb", "lib/thumblemonks/contexted_observers/context.rb", "lib/thumblemonks/contexted_observers/modules.rb", "lib/thumblemonks/contexted_observers/shortcuts.rb", "LICENSE", "README.markdown"]
|
13
|
+
s.files = ["CHANGELOG", "contexted_observers.gemspec", "lib/contexted_observers.rb", "lib/thumblemonks/contexted_observers/context.rb", "lib/thumblemonks/contexted_observers/modules.rb", "lib/thumblemonks/contexted_observers/shortcuts.rb", "LICENSE", "Manifest", "rails/action_controller_extensions.rb", "rails/active_record_observer_extensions.rb", "rails/init.rb", "Rakefile", "README.markdown", "test/action_controller_macro_test.rb", "test/active_record_observer_test.rb", "test/context_test.rb", "test/test_helper.rb"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/gabrielg/contexted_observers}
|
16
|
+
s.post_install_message = %q{CHOOSY MOMS CHOOSE THUMBLE MONKS}
|
17
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Contexted_observers", "--main", "README.markdown"]
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
s.rubyforge_project = %q{contexted_observers}
|
20
|
+
s.rubygems_version = %q{1.3.1}
|
21
|
+
s.summary = %q{A library that gives observers some context.}
|
22
|
+
s.test_files = ["test/action_controller_macro_test.rb", "test/active_record_observer_test.rb", "test/context_test.rb", "test/test_helper.rb"]
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 2
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
29
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0", "= 2.0.6"])
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0", "= 2.0.6"])
|
32
|
+
end
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0", "= 2.0.6"])
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Thumblemonks::ContextedObservers::Context
|
2
|
+
@@context_stack = []
|
3
|
+
|
4
|
+
def self.push_new(context = {})
|
5
|
+
@@context_stack.push(context.freeze)
|
6
|
+
yield
|
7
|
+
ensure
|
8
|
+
@@context_stack.pop
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.current
|
12
|
+
stack.last
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.stack
|
16
|
+
@@context_stack
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'action_controller'
|
3
|
+
|
4
|
+
module Thumblemonks
|
5
|
+
module ContextedObservers
|
6
|
+
module ActionControllerExtensions
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def contextualize(options = {}, &block)
|
15
|
+
around_filter(options) do |controller, action|
|
16
|
+
context_val = controller.instance_eval(&block)
|
17
|
+
Thumblemonks::ContextedObservers::Context.push_new(context_val) { action.call }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end # ClassMethods
|
22
|
+
|
23
|
+
end # ActionControllerExtensions
|
24
|
+
end # ContextedObservers
|
25
|
+
end # Thumblemonks
|
26
|
+
|
27
|
+
ActionController::Base.send(:include, Thumblemonks::ContextedObservers::ActionControllerExtensions)
|
data/rails/init.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_record'
|
3
|
+
require 'action_controller'
|
4
|
+
require 'action_controller/test_case'
|
5
|
+
require 'action_controller/test_process'
|
6
|
+
|
7
|
+
class BlockContextController < ActionController::Base
|
8
|
+
contextualize :except => :bar do
|
9
|
+
{:foo => :bar}
|
10
|
+
end
|
11
|
+
|
12
|
+
def foo
|
13
|
+
render :text => Thumblemonks::ContextedObservers::Context.current.inspect
|
14
|
+
end
|
15
|
+
|
16
|
+
def bar
|
17
|
+
render :text => Thumblemonks::ContextedObservers::Context.current.inspect
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
class BlockContextControllerTest < ActionController::TestCase
|
23
|
+
|
24
|
+
should "preload requested variable from block into the context" do
|
25
|
+
with_routing do |set|
|
26
|
+
set.draw do |map|
|
27
|
+
map.connect ':controller/:action/:id'
|
28
|
+
get :foo
|
29
|
+
end
|
30
|
+
end
|
31
|
+
assert_equal({:foo => :bar}.inspect, @response.body)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "pass on the filter options to around_filter" do
|
35
|
+
with_routing do |set|
|
36
|
+
set.draw do |map|
|
37
|
+
map.connect ':controller/:action/:id'
|
38
|
+
get :bar
|
39
|
+
end
|
40
|
+
end
|
41
|
+
assert_equal(nil.inspect, @response.body)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
# class ActiveRecordObserverTest < Test::Unit::TestCase
|
47
|
+
# include Thumblemonks
|
48
|
+
# class TestAR < ActiveRecord::Base; end
|
49
|
+
# class TestARObserver < ActiveRecord::Observer
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# context "active record observers" do
|
53
|
+
# setup { @instance = TestARObserver.instance }
|
54
|
+
#
|
55
|
+
# should "have an accessor for the current context" do
|
56
|
+
# context, last_context = {:you => :better}, nil
|
57
|
+
# ContextedObservers::Context.push_new(context) { last_context = @instance.context }
|
58
|
+
# assert_equal context, last_context
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# end # active record observers
|
62
|
+
# end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
class ActiveRecordObserverTest < Test::Unit::TestCase
|
5
|
+
include Thumblemonks
|
6
|
+
class TestAR < ActiveRecord::Base; end
|
7
|
+
class TestARObserver < ActiveRecord::Observer
|
8
|
+
end
|
9
|
+
|
10
|
+
context "active record observers" do
|
11
|
+
setup { @instance = TestARObserver.instance }
|
12
|
+
|
13
|
+
should "have an accessor for the current context" do
|
14
|
+
context, last_context = {:you => :better}, nil
|
15
|
+
ContextedObservers::Context.push_new(context) { last_context = @instance.context }
|
16
|
+
assert_equal context, last_context
|
17
|
+
end
|
18
|
+
|
19
|
+
end # active record observers
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ContextTest < Test::Unit::TestCase
|
4
|
+
include Thumblemonks
|
5
|
+
|
6
|
+
context "Context" do
|
7
|
+
|
8
|
+
should "have no contexts on the stack by default" do
|
9
|
+
assert ContextedObservers::Context.stack.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
should "add a new context to the stack for every new context" do
|
13
|
+
context_count = 0
|
14
|
+
ContextedObservers::Context.push_new do
|
15
|
+
ContextedObservers::Context.push_new { context_count = ContextedObservers::Context.stack.size }
|
16
|
+
end
|
17
|
+
assert_equal 2, context_count
|
18
|
+
end
|
19
|
+
|
20
|
+
should "set the current context to be the last context hash" do
|
21
|
+
context, current_context = {:ashes => :ashes}, nil
|
22
|
+
ContextedObservers::Context.push_new(context) do
|
23
|
+
current_context = ContextedObservers::Context.current
|
24
|
+
end
|
25
|
+
assert_equal context, current_context
|
26
|
+
end
|
27
|
+
|
28
|
+
should "pop the last context off the stack after the block executes" do
|
29
|
+
context, deepest_context, last_context = {:fun => :funky}, {:we_know => :major_toms}, nil
|
30
|
+
ContextedObservers::Context.push_new(context) do
|
31
|
+
ContextedObservers::Context.push_new(deepest_context) { last_context = ContextedObservers::Context.current }
|
32
|
+
assert_equal context, ContextedObservers::Context.current
|
33
|
+
end
|
34
|
+
assert_equal last_context, deepest_context
|
35
|
+
end
|
36
|
+
|
37
|
+
should "leave the context stack empty ocne all contexts have finished" do
|
38
|
+
ContextedObservers::Context.push_new({}) { }
|
39
|
+
assert ContextedObservers::Context.stack.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
should "freeze contexts" do
|
43
|
+
ContextedObservers::Context.push_new({}) { assert ContextedObservers::Context.current.frozen? }
|
44
|
+
end
|
45
|
+
|
46
|
+
end # Context contexts
|
47
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gabrielg-contexted_observers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Gironda
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-24 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
- - "="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 2.0.6
|
26
|
+
version:
|
27
|
+
description: A library that gives observers some context.
|
28
|
+
email: ""
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- CHANGELOG
|
35
|
+
- lib/contexted_observers.rb
|
36
|
+
- lib/thumblemonks/contexted_observers/context.rb
|
37
|
+
- lib/thumblemonks/contexted_observers/modules.rb
|
38
|
+
- lib/thumblemonks/contexted_observers/shortcuts.rb
|
39
|
+
- LICENSE
|
40
|
+
- README.markdown
|
41
|
+
files:
|
42
|
+
- CHANGELOG
|
43
|
+
- contexted_observers.gemspec
|
44
|
+
- lib/contexted_observers.rb
|
45
|
+
- lib/thumblemonks/contexted_observers/context.rb
|
46
|
+
- lib/thumblemonks/contexted_observers/modules.rb
|
47
|
+
- lib/thumblemonks/contexted_observers/shortcuts.rb
|
48
|
+
- LICENSE
|
49
|
+
- Manifest
|
50
|
+
- rails/action_controller_extensions.rb
|
51
|
+
- rails/active_record_observer_extensions.rb
|
52
|
+
- rails/init.rb
|
53
|
+
- Rakefile
|
54
|
+
- README.markdown
|
55
|
+
- test/action_controller_macro_test.rb
|
56
|
+
- test/active_record_observer_test.rb
|
57
|
+
- test/context_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://github.com/gabrielg/contexted_observers
|
61
|
+
post_install_message: CHOOSY MOMS CHOOSE THUMBLE MONKS
|
62
|
+
rdoc_options:
|
63
|
+
- --line-numbers
|
64
|
+
- --inline-source
|
65
|
+
- --title
|
66
|
+
- Contexted_observers
|
67
|
+
- --main
|
68
|
+
- README.markdown
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "1.2"
|
82
|
+
version:
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project: contexted_observers
|
86
|
+
rubygems_version: 1.2.0
|
87
|
+
signing_key:
|
88
|
+
specification_version: 2
|
89
|
+
summary: A library that gives observers some context.
|
90
|
+
test_files:
|
91
|
+
- test/action_controller_macro_test.rb
|
92
|
+
- test/active_record_observer_test.rb
|
93
|
+
- test/context_test.rb
|
94
|
+
- test/test_helper.rb
|