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.
A sizable amount of the code and idea for this plugin comes from:
Garry Dolley’s Let ActiveRecord Observers see controller context
class MyController < ActionController::Base include EnlightenedObservers enlighten_observer :my_observer ... end
In your observers, also include EnlightenedObservers::Enlightenment
:
class MyObserver < ActiveRecord::Observer include EnlightenedObservers::Enlightenment ... end
Of course, you still have to initialize any observer the normal way in config/environment.rb
config.active_record.observers = :my_observer
Now, in your observers, you can access data saved by calling controller
– but be sure to check if controller.nil?
To get data stored that you specified in the hash at the end of the enlighten_observer
declaration, simply access it as a hash: controller
, and that will yield the computed value of self.send(:controller_method_to_call)
where self
is the controller instance.
To enable the library your Rails 2.1 (or greater) project, use the gem configuration method in “config/environment.rb
”
Rails::Initializer.run do |config| config.gem 'moorage-enlightened_observers', :lib => 'enlightened_observers', :source => 'http://gems.github.com' end
The :lib
is important, because rails gets confused when the name of the gem is different from the library.
And of course, run
rake gems:install
To get them installed on your system.
Optionally, to unpack it into your application, just run:
rake gems:unpack GEM=moorage-enlightened_observers
A controller:
class OrganizationsController < ApplicationController
include EnlightenedObservers
enlighten_observer :alert_event_observer
before_filter :authorization_required, :only => [:edit, :update, :destroy] def edit … end end
An observer:
class AlertEventObserver < ActiveRecord::Observer include EnlightenedObservers::Enlightenment observe :organization def after_update(alertable) f = Alert.new unless controller.nil? ... # do somehting end f.title = "created #{alertable.name}" f.save end end
By adding include EnlightenedObservers::Enlightenment
to your Observer, an attr_accessor
for controller
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.
By calling enlighten_observer :my_observer
in your controller, 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 ActiveModel::Observing::ClassMethods
).
NOTE: we could have had the enlighten_observer
call actually do the include EnlightenedObservers::Enlightenment
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.
===
Copyright © 2008 ThriveSmart, LLC, based on code by Garry Dolley, released under the MIT license.