moorage-enlightened_observers 1.12 → 1.14.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +42 -8
- data/Rakefile +1 -1
- data/lib/enlightened_observers.rb +2 -2
- metadata +1 -1
data/README.textile
CHANGED
@@ -11,7 +11,8 @@ h2. Usage
|
|
11
11
|
<pre>
|
12
12
|
class MyController < ActionController::Base
|
13
13
|
include EnlightenedObservers
|
14
|
-
enlighten_observer :my_observer
|
14
|
+
enlighten_observer :my_observer
|
15
|
+
...
|
15
16
|
end
|
16
17
|
</pre>
|
17
18
|
|
@@ -20,19 +21,20 @@ In your observers, also <code>include EnlightenedObservers::Enlightenment</code>
|
|
20
21
|
<pre>
|
21
22
|
class MyObserver < ActiveRecord::Observer
|
22
23
|
include EnlightenedObservers::Enlightenment
|
23
|
-
|
24
|
-
<em> def after_create(my)
|
25
|
-
...
|
26
|
-
end</em>
|
24
|
+
...
|
27
25
|
end
|
28
26
|
</pre>
|
29
27
|
|
30
28
|
Of course, you still have to initialize any observer the normal way in <code>config/environment.rb</code>
|
31
29
|
|
32
30
|
<pre>
|
33
|
-
config.active_record.observers = :my_observer
|
31
|
+
config.active_record.observers = :my_observer
|
34
32
|
</pre>
|
35
33
|
|
34
|
+
Now, in your observers, you can access data saved by calling <code>controller</code> - but be sure to check if <code>controller.nil?</code>
|
35
|
+
|
36
|
+
To get data stored that you specified in the hash at the end of the <code>enlighten_observer</code> declaration, simply access it as a hash: <code>controller</code>, and that will yield the computed value of <code>self.send(:controller_method_to_call)</code> where <code>self</code> is the controller instance.
|
37
|
+
|
36
38
|
|
37
39
|
h2. Installation
|
38
40
|
|
@@ -60,12 +62,44 @@ Optionally, to unpack it into your application, just run:
|
|
60
62
|
rake gems:unpack GEM=moorage-enlightened_observers
|
61
63
|
</pre>
|
62
64
|
|
65
|
+
h2. Example
|
66
|
+
|
67
|
+
A controller:
|
68
|
+
<pre>
|
69
|
+
class OrganizationsController < ApplicationController
|
70
|
+
include EnlightenedObservers
|
71
|
+
enlighten_observer :alert_event_observer
|
72
|
+
before_filter :authorization_required, :only => [:edit, :update, :destroy]
|
73
|
+
|
74
|
+
def edit
|
75
|
+
...
|
76
|
+
end
|
77
|
+
end
|
78
|
+
</pre>
|
79
|
+
|
80
|
+
An observer:
|
81
|
+
|
82
|
+
<pre>
|
83
|
+
class AlertEventObserver < ActiveRecord::Observer
|
84
|
+
include EnlightenedObservers::Enlightenment
|
85
|
+
observe :organization
|
86
|
+
|
87
|
+
def after_update(alertable)
|
88
|
+
f = Alert.new
|
89
|
+
unless controller.nil?
|
90
|
+
... # do somehting
|
91
|
+
end
|
92
|
+
f.title = "created #{alertable.name}"
|
93
|
+
f.save
|
94
|
+
end
|
95
|
+
end
|
96
|
+
</pre>
|
63
97
|
|
64
98
|
h2. How it Works
|
65
99
|
|
66
|
-
By adding <code>include EnlightenedObservers::Enlightenment</code> to your Observer, an <code>attr_accessor</code> for <code>
|
100
|
+
By adding <code>include EnlightenedObservers::Enlightenment</code> to your Observer, an <code>attr_accessor</code> for <code>controller</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.
|
67
101
|
|
68
|
-
By calling <code>enlighten_observer :my_observer
|
102
|
+
By calling <code>enlighten_observer :my_observer</code> in your controller, you're adding an around_filter to the controller that sets and unsets the observers' <code>controller</code> 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>).
|
69
103
|
|
70
104
|
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.
|
71
105
|
|
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.14.0"
|
11
11
|
AUTHOR = "ThriveSmart, LLC"
|
12
12
|
EMAIL = "developers@thrivesmart.com"
|
13
13
|
HOMEPAGE = "http://www.github.com/moorage/enlightened_observers"
|
@@ -5,13 +5,13 @@ module EnlightenedObservers
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def enlighten_observer(*observers)
|
8
|
-
|
8
|
+
enlighten_data_spec = observers.last.is_a?(Hash) ? observers.pop : {}
|
9
9
|
observers.each do |observer|
|
10
10
|
observer_instance = observer.to_s.camelize.constantize.instance
|
11
11
|
around_filter do |controller, action|
|
12
12
|
observer_instance.controller = controller
|
13
13
|
action.call
|
14
|
-
observer_instance.controller =
|
14
|
+
observer_instance.controller = nil # Other controllers can call this too!
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|