Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created May 20, 2010 20:12
Show Gist options
  • Select an option

  • Save brianjlandau/408024 to your computer and use it in GitHub Desktop.

Select an option

Save brianjlandau/408024 to your computer and use it in GitHub Desktop.
# 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