Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:patmaddox/no-peeping-toms

  • Loading branch information...
commit ef9d248ccc24780a9caf92c914345a585e93524d 2 parents eb2815b + 564a9fd
@patmaddox authored
View
1  .gitignore
@@ -3,3 +3,4 @@
Gemfile.lock
pkg/*
spec/debug.log
+.*.sw?
View
3  History.rdoc
@@ -1,3 +1,6 @@
+=== Version 2.0.2 / 2012-07-26
+* Fix deprecation warning
+
=== Version 2.0.1 / 2011-02-16
* Supports current Rails 3.x release. No longer supports Rails 2.
View
46 lib/no_peeping_toms.rb
@@ -45,32 +45,36 @@ def observer_enabled?(observer)
end
end
- module InstanceMethods
- # Overrides ActiveRecord#define_callbacks so that observers are only called
- # when enabled.
- #
- # This is a bit yuck being a protected method, but appears to be the cleanest
- # way so far
- def define_callbacks_with_enabled_check(klass)
- observer = self
- observer_name = observer.class.name.underscore.gsub('/', '__')
+ # Overrides ActiveRecord#define_callbacks so that observers are only called
+ # when enabled.
+ #
+ # This is a bit yuck being a protected method, but appears to be the cleanest
+ # way so far
+ def define_callbacks_with_enabled_check(klass)
+ observer = self
+ observer_name = observer.class.name.underscore.gsub('/', '__')
- ActiveRecord::Callbacks::CALLBACKS.each do |callback|
- next unless respond_to?(callback)
- callback_meth = :"_notify_#{observer_name}_for_#{callback}"
- unless klass.respond_to?(callback_meth)
- klass.send(:define_method, callback_meth) do
- observer.send(callback, self) if observer.observer_enabled?
- end
- klass.send(callback, callback_meth)
+ ActiveRecord::Callbacks::CALLBACKS.each do |callback|
+ next unless respond_to?(callback)
+ callback_meth = :"_notify_#{observer_name}_for_#{callback}"
+ unless klass.respond_to?(callback_meth)
+ klass.send(:define_method, callback_meth) do
+ observer.send(callback, self) if observer.observer_enabled?
end
+ klass.send(callback, callback_meth)
end
end
+ end
- # Determines whether this observer should be run
- def observer_enabled?
- self.class.observer_enabled?(self)
- end
+ # Enables interception of custom observer notifications, i.e.
+ # notify_observers(:custom_notification)
+ def update(*args, &block)
+ super if observer_enabled?
+ end
+
+ # Determines whether this observer should be run
+ def observer_enabled?
+ self.class.observer_enabled?(self)
end
end
View
36 spec/no_peeping_toms_spec.rb
@@ -3,7 +3,22 @@
module NoPeepingTomsSpec
class Person < ActiveRecord::Base; end
class SpecialPerson < Person; end
-
+ class HungryPerson < Person
+ def eat_dinner!
+ notify_observers(:ate_dinner)
+ end
+ end
+
+ class DinnerObserver < ActiveRecord::Observer
+ observe Person
+ cattr_accessor :called
+
+ def ate_dinner(person)
+ self.class.called ||= 0
+ self.class.called += 1
+ end
+ end
+
class PersonObserver < ActiveRecord::Observer
cattr_accessor :called
@@ -12,7 +27,7 @@ def before_create(person)
self.class.called += 1
end
end
-
+
class AnotherObserver < ActiveRecord::Observer
observe Person
cattr_accessor :called
@@ -26,6 +41,7 @@ def before_create(person)
# Register the observers with the host app
PersonObserver.instance
AnotherObserver.instance
+ DinnerObserver.instance
describe NoPeepingToms, "configuration" do
it "enables observers by default" do
@@ -82,6 +98,22 @@ def before_create(person)
AnotherObserver.called.should be_true
end
+ it "should not call observers with custom notifications if they are disabled" do
+ DinnerObserver.called = 0
+ lambda {
+ HungryPerson.new.eat_dinner!
+ }.should_not change(DinnerObserver, :called)
+ end
+
+ it "should call observers with custom notifications if they are enabled" do
+ DinnerObserver.called = 0
+ ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::DinnerObserver) do
+ lambda {
+ HungryPerson.new.eat_dinner!
+ }.should change(DinnerObserver, :called).by(1)
+ end
+ end
+
it "should ensure peeping toms are reset after raised exception" do
lambda {
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) do
Please sign in to comment.
Something went wrong with that request. Please try again.