Created
May 20, 2010 20:12
-
-
Save brianjlandau/408024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # From: http://scie.nti.st/2007/2/1/let-activerecord-observers-see-controller-context | |
| # By Garry Dolley | |
| # MIT / X11 licensed | |
| module EnlightenObservers | |
| def self.included(base) | |
| base.extend(ClassMethods) | |
| end | |
| module ClassMethods | |
| def observer(*observers) | |
| observers.each do |observer| | |
| observer_instance = observer.to_s.classify.constantize.instance | |
| class <<observer_instance | |
| include Enlightenment | |
| end | |
| around_filter(observer_instance) | |
| end | |
| end | |
| end | |
| module Enlightenment | |
| def self.included(base) | |
| base.class_eval do | |
| attr_accessor :controller | |
| end | |
| end | |
| def before(controller) | |
| self.controller = controller | |
| end | |
| def after(controller) | |
| self.controller = nil # Clean up for GC | |
| end | |
| private | |
| def method_missing(method, *args, &block) | |
| if controller.respond_to?(method) | |
| controller.send(method, *args) | |
| else | |
| super | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment