moorage-enlightened_observers 1.0.20080915 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +52 -8
- data/Rakefile +1 -1
- data/lib/enlightened_observers.rb +6 -16
- metadata +1 -1
data/README.textile
CHANGED
@@ -2,27 +2,71 @@ h1. EnlightenedObservers
|
|
2
2
|
|
3
3
|
This plugin makes it quick and easy to share the controller information with observers, including session information. This is useful when observers need to report the current_user that made a certain action happen.
|
4
4
|
|
5
|
-
|
6
|
-
http://scie.nti.st/2007/2/1/let-activerecord-observers-see-controller-context
|
5
|
+
A sizable amount of the code and idea for this plugin comes from:
|
6
|
+
"Garry Dolley's Let ActiveRecord Observers see controller context":http://scie.nti.st/2007/2/1/let-activerecord-observers-see-controller-context
|
7
7
|
|
8
8
|
|
9
9
|
h2. Usage
|
10
10
|
|
11
|
-
<
|
11
|
+
<pre>
|
12
12
|
class MyController < ActionController::Base
|
13
|
-
|
13
|
+
enlighten_observer :my_observer<i>[, :my_second_observer]</i>
|
14
|
+
end
|
15
|
+
</pre>
|
16
|
+
|
17
|
+
In your observers, also <code>include EnlightenedObservers::Enlightenment</code>:
|
14
18
|
|
15
|
-
|
19
|
+
<pre>
|
20
|
+
class MyObserver < ActiveRecord::Observer
|
21
|
+
include EnlightenedObservers::Enlightenment
|
22
|
+
|
23
|
+
<i> def after_create(my)
|
24
|
+
...
|
25
|
+
end</i>
|
16
26
|
end
|
17
|
-
</
|
27
|
+
</pre>
|
18
28
|
|
19
|
-
|
20
|
-
|
29
|
+
Of course, you still have to initialize any observer the normal way in <code>config/environment.rb</code>
|
30
|
+
|
31
|
+
<pre>
|
32
|
+
config.active_record.observers = :my_observer<i>[, :my_second_observer]</i>
|
33
|
+
</pre>
|
21
34
|
|
22
35
|
|
23
36
|
h2. Installation
|
24
37
|
|
38
|
+
To enable the library your Rails 2.1 (or greater) project, use the gem configuration method in "<code>config/environment.rb</code>"
|
39
|
+
|
40
|
+
<pre>
|
41
|
+
Rails::Initializer.run do |config|
|
42
|
+
config.gem 'moorage-enlightened_observers', :lib => 'enlightened_observers', :source => 'http://gems.github.com'
|
43
|
+
end
|
44
|
+
</pre>
|
45
|
+
|
46
|
+
The <code>:lib</code> is important, because rails gets confused when the name of the gem is different from the library.
|
47
|
+
|
48
|
+
And of course, run
|
49
|
+
|
50
|
+
<pre>
|
51
|
+
rake gems:install
|
52
|
+
</pre>
|
53
|
+
|
54
|
+
To get them installed on your system.
|
55
|
+
|
56
|
+
Optionally, to unpack it into your application, just run:
|
57
|
+
|
58
|
+
<pre>
|
59
|
+
rake gems:unpack GEM=moorage-enlightened_observers
|
60
|
+
</pre>
|
61
|
+
|
62
|
+
|
63
|
+
h2. How it Works
|
64
|
+
|
65
|
+
By adding <code>include EnlightenedObservers::Enlightenment</code> to your Observer, an <code>attr_accessor</code> for <code>controllers</code> is added, which you can access any time from your Observer. Depending on how your oberserver has been called, this variable might have been set, with the controller that was used to access and change the model this observer has been observing.
|
66
|
+
|
67
|
+
By calling <code>enlighten_observer :my_observer<i>[, :my_second_observer]</i></code> in your controllers, you're adding an around_filter to the controller that sets and unsets the observers' controller accessor. Each time a controller action is called, this around filter finds the currently running instance of the observer (using code copied from module <code>ActiveModel::Observing::ClassMethods</code>).
|
25
68
|
|
69
|
+
NOTE: we could have had the <code>enlighten_observer</code> call actually do the <code>include EnlightenedObservers::Enlightenment</code> in the Observer - however, since your observer can be run even if the model was changed through something OTHER than a controller action, we chose to decouple the two calls.
|
26
70
|
|
27
71
|
|
28
72
|
=======
|
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ require 'date'
|
|
7
7
|
|
8
8
|
GEM = "enlightened_observers"
|
9
9
|
HUMANIZED_GEM = "EnlightenedObservers"
|
10
|
-
GEM_VERSION = "1.
|
10
|
+
GEM_VERSION = "1.1"
|
11
11
|
AUTHOR = "ThriveSmart, LLC"
|
12
12
|
EMAIL = "developers@thrivesmart.com"
|
13
13
|
HOMEPAGE = "http://www.github.com/moorage/enlightened_observers"
|
@@ -4,17 +4,15 @@ module EnlightenedObservers
|
|
4
4
|
end
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
def
|
8
|
-
super
|
9
|
-
|
7
|
+
def enlighten_observer(*observers)
|
10
8
|
configuration = observers.last.is_a?(Hash) ? observers.pop : {}
|
11
9
|
observers.each do |observer|
|
12
|
-
observer_instance =
|
13
|
-
|
14
|
-
|
10
|
+
observer_instance = observer.to_s.camelize.constantize.instance
|
11
|
+
around_filter do |controller, action|
|
12
|
+
observer_instance.controller = controller
|
13
|
+
action.call
|
14
|
+
observer_instance.controller = controller # Other controllers can call this too!
|
15
15
|
end
|
16
|
-
|
17
|
-
around_filter(observer_instance, :only => configuration[:only])
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
@@ -25,13 +23,5 @@ module EnlightenedObservers
|
|
25
23
|
attr_accessor :controller
|
26
24
|
end
|
27
25
|
end
|
28
|
-
|
29
|
-
def before(controller)
|
30
|
-
self.controller = controller
|
31
|
-
end
|
32
|
-
|
33
|
-
def after(controller)
|
34
|
-
self.controller = nil # Clean up for GC
|
35
|
-
end
|
36
26
|
end
|
37
27
|
end
|