Permalink
Browse files

Move enable/disable_observers onto ActiveRecord::Observer

  • Loading branch information...
1 parent bbc2197 commit 226cfb10f167a6d38f5dbf09602645786d12aa54 @patmaddox committed Jan 6, 2010
Showing with 33 additions and 21 deletions.
  1. +7 −0 History.rdoc
  2. +10 −2 README
  3. +12 −15 lib/no_peeping_toms.rb
  4. +4 −4 spec/no_peeping_toms_spec.rb
View
7 History.rdoc
@@ -1,3 +1,10 @@
+=== Version 1.1.0 / 2010-01-06
+
+* Explicit enable/disable API (Corey Haines & Drew Olson)
+ ActiveRecord::Observer.enable_observers
+ ActiveRecord::Observer.disable_observers
+ NOTE: Observers are enabled by default. You now need to explicitly disable them in your test helper
+
=== Version 1.0.1 / 2010-01-06
* official gem release
View
12 README
@@ -7,11 +7,11 @@ This plugin disables observers in your specs, so that model specs can run in com
To disable observers, place the following code in your test.rb, or spec_helper.rb, or wherever:
- NoPeepingToms.disable_observers
+ ActiveRecord::Observer.disable_observers
You can easily reenable observers:
- NoPeepingToms.enable_observers
+ ActiveRecord::Observer.enable_observers
You can choose to run some code with specific observers turned on. This is useful when spec'ing an observer. For example, if you write the following observer:
@@ -46,5 +46,13 @@ You can spec the Person class in complete isolation.
end
end
+Cool people who have contributed to NoPeepingToms:
+ * Brandon Keepers
+ * Corey Haines
+ * Jeff Siegel
+ * Drew Olson
+ * Zach Dennis
+
+If you think your name should be here, shoot me an email and I'll add it ASAP.
Copyright (c) 2007-2010 Pat Maddox, released under the MIT license
View
27 lib/no_peeping_toms.rb
@@ -1,30 +1,19 @@
module NoPeepingToms
- def self.disable_observers
- @default_observers_enabled = false
- end
-
- def self.enable_observers
- @default_observers_enabled = true
- end
- enable_observers
-
- def self.default_observers_enabled?
- @default_observers_enabled
- end
-
def self.included(base)
unless base.included_modules.include?(NoPeepingToms::InstanceMethods)
base.send :include, NoPeepingToms::InstanceMethods
base.extend NoPeepingToms::ClassMethods
base.alias_method_chain :update, :neighborhood_watch
- base.cattr_accessor :peeping_toms
+ base.cattr_accessor :peeping_toms, :default_observers_enabled
base.peeping_toms = [] # toms that are allowed to peep
end
+
+ base.enable_observers
end
module InstanceMethods
def update_with_neighborhood_watch(*args)
- if NoPeepingToms.default_observers_enabled? || self.class.peeping_toms.include?(self)
+ if self.class.default_observers_enabled || self.class.peeping_toms.include?(self)
update_without_neighborhood_watch(*args)
end
end
@@ -39,6 +28,14 @@ def with_observers(*observer_syms)
ensure
self.peeping_toms.clear
end
+
+ def disable_observers
+ self.default_observers_enabled = false
+ end
+
+ def enable_observers
+ self.default_observers_enabled = true
+ end
end
end
View
8 spec/no_peeping_toms_spec.rb
@@ -23,18 +23,18 @@ def before_create(person)
describe NoPeepingToms, "configuration" do
it "enables observers by default" do
load 'no_peeping_toms.rb'
- NoPeepingToms.default_observers_enabled?.should be_true
+ ActiveRecord::Observer.default_observers_enabled.should be_true
end
it "runs default observers when default observers are enabled" do
- NoPeepingToms.enable_observers
+ ActiveRecord::Observer.enable_observers
PersonObserver.called = false
Person.create!
PersonObserver.called.should be_true
end
it "does not run default observers when default observers are disabled" do
- NoPeepingToms.disable_observers
+ ActiveRecord::Observer.disable_observers
PersonObserver.called = false
Person.create!
PersonObserver.called.should be_false
@@ -43,7 +43,7 @@ def before_create(person)
describe ActiveRecord::Observer, 'with_observers' do
before(:each) do
- NoPeepingToms.disable_observers
+ ActiveRecord::Observer.disable_observers
end
it "should enable an observer via stringified class name" do

0 comments on commit 226cfb1

Please sign in to comment.