Created
August 7, 2014 14:19
-
-
Save ozzyaaron/23ee55ee552cc1d8a884 to your computer and use it in GitHub Desktop.
What Am I Doing Wrong?
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
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? |
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
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 |
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
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