moorage-enlightened_observers 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -10,7 +10,9 @@ h2. Usage
10
10
 
11
11
  <pre>
12
12
  class MyController < ActionController::Base
13
- enlighten_observer :my_observer<i>[, :my_second_observer]</i>
13
+ include EnlightenedObservers
14
+ enlighten_observer :my_observer, :second_observer, ..., { :data_hash_key => :controller_method_to_call, ...}
15
+ ...
14
16
  end
15
17
  </pre>
16
18
 
@@ -19,19 +21,20 @@ In your observers, also <code>include EnlightenedObservers::Enlightenment</code>
19
21
  <pre>
20
22
  class MyObserver < ActiveRecord::Observer
21
23
  include EnlightenedObservers::Enlightenment
22
-
23
- <i> def after_create(my)
24
- ...
25
- end</i>
24
+ ...
26
25
  end
27
26
  </pre>
28
27
 
29
28
  Of course, you still have to initialize any observer the normal way in <code>config/environment.rb</code>
30
29
 
31
30
  <pre>
32
- config.active_record.observers = :my_observer<i>[, :my_second_observer]</i>
31
+ config.active_record.observers = :my_observer
33
32
  </pre>
34
33
 
34
+ Now, in your observers, you can access data saved by calling <code>enlightenment[:controller]</code> - but be sure to check if <code>enlightenment.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>enlightenment[:data_hash_key]</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
+
35
38
 
36
39
  h2. Installation
37
40
 
@@ -59,12 +62,51 @@ Optionally, to unpack it into your application, just run:
59
62
  rake gems:unpack GEM=moorage-enlightened_observers
60
63
  </pre>
61
64
 
65
+ h2. Example
66
+
67
+ A controller:
68
+ <pre>
69
+ class OrganizationsController < ApplicationController
70
+ include EnlightenedObservers
71
+ enlighten_observer :alert_event_observer, { :logged_in => :logged_in?, :current_user => :current_user }
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
+ f.actor = find_actor
90
+ f.title = "created #{alertable.name}"
91
+ f.save
92
+ end
93
+
94
+ protected
95
+
96
+ def find_actor
97
+ unless enlightenment.nil? || !enlightenment[:logged_in?]
98
+ return enlightenment[:current_user]
99
+ end
100
+ nil
101
+ end
102
+ end
103
+ </pre>
62
104
 
63
105
  h2. How it Works
64
106
 
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.
107
+ By adding <code>include EnlightenedObservers::Enlightenment</code> to your Observer, an <code>attr_accessor</code> for <code>enlightenment</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
108
 
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>).
109
+ 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' enlightenment 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>). The controller then saves the values from the controller into the enlightment hash, e.g. <code>enlightenment[:controller]</code>
68
110
 
69
111
  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.
70
112
 
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.1"
10
+ GEM_VERSION = "1.2"
11
11
  AUTHOR = "ThriveSmart, LLC"
12
12
  EMAIL = "developers@thrivesmart.com"
13
13
  HOMEPAGE = "http://www.github.com/moorage/enlightened_observers"
@@ -5,13 +5,15 @@ module EnlightenedObservers
5
5
 
6
6
  module ClassMethods
7
7
  def enlighten_observer(*observers)
8
- configuration = observers.last.is_a?(Hash) ? observers.pop : {}
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
- observer_instance.controller = controller
12
+ calculated_values = { :controller => self }
13
+ enlighten_data_spec.each_pair { |k,v| calculated_values[k] = self.send(v.to_sym) }
14
+ observer_instance.enlightenment = calculated_values
13
15
  action.call
14
- observer_instance.controller = controller # Other controllers can call this too!
16
+ observer_instance.enlightenment = nil # Other controllers can call this too!
15
17
  end
16
18
  end
17
19
  end
@@ -20,7 +22,7 @@ module EnlightenedObservers
20
22
  module Enlightenment
21
23
  def self.included(base)
22
24
  base.module_eval do
23
- attr_accessor :controller
25
+ attr_accessor :enlightenment
24
26
  end
25
27
  end
26
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moorage-enlightened_observers
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.1"
4
+ version: "1.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - ThriveSmart, LLC