Skip to content

Instantly share code, notes, and snippets.

@ozzyaaron
Created August 7, 2014 14:19
Show Gist options
  • Save ozzyaaron/23ee55ee552cc1d8a884 to your computer and use it in GitHub Desktop.
Save ozzyaaron/23ee55ee552cc1d8a884 to your computer and use it in GitHub Desktop.
What Am I Doing Wrong?
The below doesn't work. Even if Observed.observer_instances shows and instance of Observer when you save an Observed instance nothing is hit inside the observer.
If you register the observer in application bootup as per normal and specify "observe Observed" inside the Observer then of course it works.
What magic do I need to weave to allow a class to register itself with an observer rather than the observer register itself with the observed classes?
class Observed < ActiveRecord::Base
# include Auditing - would be repsonsible for providing
# def auditing( mode )
# Observer.register_class( self, mode )
# end
Observer.register_klass( self, :mode_for_later ) # Would like to mixing a class method auditing( mode ) so self is implied
end
class Observer < ActiveRecord::Observer
@mutex, @class_map = Mutex.new, {}
def self.register_class( klass, mode )
@mutex.synchronize do
klass.add_observer( self.instance )
@class_map[ klass ] = mode # I plan to use this later
end
end
def after_save
end
def update( *args )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment