moorage-enlightened_observers 1.2 → 1.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -10,9 +10,7 @@ h2. Usage
10
10
 
11
11
  <pre>
12
12
  class MyController < ActionController::Base
13
- include EnlightenedObservers
14
- enlighten_observer :my_observer, :second_observer, ..., { :data_hash_key => :controller_method_to_call, ...}
15
- ...
13
+ enlighten_observer :my_observer<i>[, :my_second_observer]</i>
16
14
  end
17
15
  </pre>
18
16
 
@@ -21,20 +19,19 @@ In your observers, also <code>include EnlightenedObservers::Enlightenment</code>
21
19
  <pre>
22
20
  class MyObserver < ActiveRecord::Observer
23
21
  include EnlightenedObservers::Enlightenment
24
- ...
22
+
23
+ <i> def after_create(my)
24
+ ...
25
+ end</i>
25
26
  end
26
27
  </pre>
27
28
 
28
29
  Of course, you still have to initialize any observer the normal way in <code>config/environment.rb</code>
29
30
 
30
31
  <pre>
31
- config.active_record.observers = :my_observer
32
+ config.active_record.observers = :my_observer<i>[, :my_second_observer]</i>
32
33
  </pre>
33
34
 
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
-
38
35
 
39
36
  h2. Installation
40
37
 
@@ -62,51 +59,12 @@ Optionally, to unpack it into your application, just run:
62
59
  rake gems:unpack GEM=moorage-enlightened_observers
63
60
  </pre>
64
61
 
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>
104
62
 
105
63
  h2. How it Works
106
64
 
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.
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.
108
66
 
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>
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>).
110
68
 
111
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.
112
70
 
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.2"
10
+ GEM_VERSION = "1.11"
11
11
  AUTHOR = "ThriveSmart, LLC"
12
12
  EMAIL = "developers@thrivesmart.com"
13
13
  HOMEPAGE = "http://www.github.com/moorage/enlightened_observers"
@@ -5,15 +5,13 @@ module EnlightenedObservers
5
5
 
6
6
  module ClassMethods
7
7
  def enlighten_observer(*observers)
8
- enlighten_data_spec = observers.last.is_a?(Hash) ? observers.pop : {}
8
+ configuration = 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
- 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
12
+ observer_instance.controller = controller
15
13
  action.call
16
- observer_instance.enlightenment = nil # Other controllers can call this too!
14
+ observer_instance.controller = controller # Other controllers can call this too!
17
15
  end
18
16
  end
19
17
  end
@@ -22,7 +20,7 @@ module EnlightenedObservers
22
20
  module Enlightenment
23
21
  def self.included(base)
24
22
  base.module_eval do
25
- attr_accessor :enlightenment
23
+ attr_accessor :controller
26
24
  end
27
25
  end
28
26
  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.2"
4
+ version: "1.11"
5
5
  platform: ruby
6
6
  authors:
7
7
  - ThriveSmart, LLC