Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #25 from leereilly/fix-instancemethods-deprecation…
Browse files Browse the repository at this point in the history
…-warning

Remove InstanceMethods deprecation warning
  • Loading branch information
Pat Maddox committed Jul 27, 2012
2 parents a0978d5 + e5fbd73 commit 02421b4
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions lib/no_peeping_toms.rb
Expand Up @@ -45,38 +45,36 @@ def observer_enabled?(observer)
end end
end end


module InstanceMethods # Overrides ActiveRecord#define_callbacks so that observers are only called
# Overrides ActiveRecord#define_callbacks so that observers are only called # when enabled.
# when enabled. #
# # This is a bit yuck being a protected method, but appears to be the cleanest
# This is a bit yuck being a protected method, but appears to be the cleanest # way so far
# way so far def define_callbacks_with_enabled_check(klass)
def define_callbacks_with_enabled_check(klass) observer = self
observer = self observer_name = observer.class.name.underscore.gsub('/', '__')
observer_name = observer.class.name.underscore.gsub('/', '__')


ActiveRecord::Callbacks::CALLBACKS.each do |callback| ActiveRecord::Callbacks::CALLBACKS.each do |callback|
next unless respond_to?(callback) next unless respond_to?(callback)
callback_meth = :"_notify_#{observer_name}_for_#{callback}" callback_meth = :"_notify_#{observer_name}_for_#{callback}"
unless klass.respond_to?(callback_meth) unless klass.respond_to?(callback_meth)
klass.send(:define_method, callback_meth) do klass.send(:define_method, callback_meth) do
observer.send(callback, self) if observer.observer_enabled? observer.send(callback, self) if observer.observer_enabled?
end
klass.send(callback, callback_meth)
end end
klass.send(callback, callback_meth)
end end
end end
end


# Enables interception of custom observer notifications, i.e. # Enables interception of custom observer notifications, i.e.
# notify_observers(:custom_notification) # notify_observers(:custom_notification)
def update(*args, &block) def update(*args, &block)
super if observer_enabled? super if observer_enabled?
end end


# Determines whether this observer should be run # Determines whether this observer should be run
def observer_enabled? def observer_enabled?
self.class.observer_enabled?(self) self.class.observer_enabled?(self)
end
end end
end end


Expand Down

0 comments on commit 02421b4

Please sign in to comment.