no_peeping_toms 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +7 -0
- data/README +17 -1
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/no_peeping_toms.rb +18 -7
- data/no_peeping_toms.gemspec +1 -1
- data/spec/no_peeping_toms_spec.rb +49 -77
- data/spec/spec_helper.rb +1 -1
- metadata +1 -1
data/History.rdoc
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
=== Version 1.1.0 / 2010-01-06
|
2
|
+
|
3
|
+
* Explicit enable/disable API (Corey Haines & Drew Olson)
|
4
|
+
ActiveRecord::Observer.enable_observers
|
5
|
+
ActiveRecord::Observer.disable_observers
|
6
|
+
NOTE: Observers are enabled by default. You now need to explicitly disable them in your test helper
|
7
|
+
|
1
8
|
=== Version 1.0.1 / 2010-01-06
|
2
9
|
|
3
10
|
* official gem release
|
data/README
CHANGED
@@ -5,7 +5,15 @@ Originally a plugin, now gemified. 'gem install no_peeping_toms'
|
|
5
5
|
|
6
6
|
This plugin disables observers in your specs, so that model specs can run in complete isolation.
|
7
7
|
|
8
|
-
|
8
|
+
To disable observers, place the following code in your test.rb, or spec_helper.rb, or wherever:
|
9
|
+
|
10
|
+
ActiveRecord::Observer.disable_observers
|
11
|
+
|
12
|
+
You can easily reenable observers:
|
13
|
+
|
14
|
+
ActiveRecord::Observer.enable_observers
|
15
|
+
|
16
|
+
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:
|
9
17
|
|
10
18
|
class PersonObserver < ActiveRecord::Observer
|
11
19
|
def before_update(person)
|
@@ -38,5 +46,13 @@ You can spec the Person class in complete isolation.
|
|
38
46
|
end
|
39
47
|
end
|
40
48
|
|
49
|
+
Cool people who have contributed to NoPeepingToms:
|
50
|
+
* Brandon Keepers
|
51
|
+
* Corey Haines
|
52
|
+
* Jeff Siegel
|
53
|
+
* Drew Olson
|
54
|
+
* Zach Dennis
|
55
|
+
|
56
|
+
If you think your name should be here, shoot me an email and I'll add it ASAP.
|
41
57
|
|
42
58
|
Copyright (c) 2007-2010 Pat Maddox, released under the MIT license
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/no_peeping_toms.rb
CHANGED
@@ -1,16 +1,19 @@
|
|
1
1
|
module NoPeepingToms
|
2
2
|
def self.included(base)
|
3
|
-
base.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
unless base.included_modules.include?(NoPeepingToms::InstanceMethods)
|
4
|
+
base.send :include, NoPeepingToms::InstanceMethods
|
5
|
+
base.extend NoPeepingToms::ClassMethods
|
6
|
+
base.alias_method_chain :update, :neighborhood_watch
|
7
|
+
base.cattr_accessor :peeping_toms, :default_observers_enabled
|
8
|
+
base.peeping_toms = [] # toms that are allowed to peep
|
9
|
+
end
|
10
|
+
|
11
|
+
base.enable_observers
|
9
12
|
end
|
10
13
|
|
11
14
|
module InstanceMethods
|
12
15
|
def update_with_neighborhood_watch(*args)
|
13
|
-
if self.class.
|
16
|
+
if self.class.default_observers_enabled || self.class.peeping_toms.include?(self)
|
14
17
|
update_without_neighborhood_watch(*args)
|
15
18
|
end
|
16
19
|
end
|
@@ -25,6 +28,14 @@ module NoPeepingToms
|
|
25
28
|
ensure
|
26
29
|
self.peeping_toms.clear
|
27
30
|
end
|
31
|
+
|
32
|
+
def disable_observers
|
33
|
+
self.default_observers_enabled = false
|
34
|
+
end
|
35
|
+
|
36
|
+
def enable_observers
|
37
|
+
self.default_observers_enabled = true
|
38
|
+
end
|
28
39
|
end
|
29
40
|
end
|
30
41
|
|
data/no_peeping_toms.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{no_peeping_toms}
|
8
|
-
s.version = "1.0
|
8
|
+
s.version = "1.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Pat Maddox", "Brandon Keepers"]
|
@@ -4,106 +4,78 @@ module NoPeepingTomsSpec
|
|
4
4
|
class Person < ActiveRecord::Base; end
|
5
5
|
|
6
6
|
class PersonObserver < ActiveRecord::Observer
|
7
|
-
|
8
|
-
|
7
|
+
cattr_accessor :called
|
8
|
+
|
9
|
+
def before_create(person)
|
10
|
+
self.class.called = true
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
12
14
|
class AnotherObserver < ActiveRecord::Observer
|
13
15
|
observe Person
|
14
|
-
|
15
|
-
|
16
|
+
cattr_accessor :called
|
17
|
+
|
18
|
+
def before_create(person)
|
19
|
+
self.class.called = true
|
16
20
|
end
|
17
21
|
end
|
18
|
-
|
19
|
-
describe Person, " when changing a name" do
|
20
|
-
before(:each) do
|
21
|
-
$observer_called_names = []
|
22
|
-
$calls_to_another_observer = 0
|
23
|
-
@person = Person.create! :name => "Pat Maddox"
|
24
|
-
end
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
describe NoPeepingToms, "configuration" do
|
24
|
+
it "enables observers by default" do
|
25
|
+
load 'no_peeping_toms.rb'
|
26
|
+
ActiveRecord::Observer.default_observers_enabled.should be_true
|
30
27
|
end
|
31
28
|
|
32
|
-
it "
|
33
|
-
ActiveRecord::Observer.
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
it "runs default observers when default observers are enabled" do
|
30
|
+
ActiveRecord::Observer.enable_observers
|
31
|
+
PersonObserver.called = false
|
32
|
+
Person.create!
|
33
|
+
PersonObserver.called.should be_true
|
34
|
+
end
|
37
35
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
it "does not run default observers when default observers are disabled" do
|
37
|
+
ActiveRecord::Observer.disable_observers
|
38
|
+
PersonObserver.called = false
|
39
|
+
Person.create!
|
40
|
+
PersonObserver.called.should be_false
|
42
41
|
end
|
43
|
-
|
44
|
-
it "should register a name change with the person observer turned on by class reference" do
|
45
|
-
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) do
|
46
|
-
@person.update_attribute :name, "Name change"
|
47
|
-
$observer_called_names.pop.should == "Name change"
|
48
|
-
end
|
42
|
+
end
|
49
43
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
$calls_to_another_observer.should == 0
|
44
|
+
describe ActiveRecord::Observer, 'with_observers' do
|
45
|
+
before(:each) do
|
46
|
+
ActiveRecord::Observer.disable_observers
|
54
47
|
end
|
55
48
|
|
56
|
-
it "should
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
62
|
-
end
|
63
|
-
ActiveRecord::Observer.with_observers(observer) do
|
64
|
-
@person.update_attribute :name, "Name change"
|
65
|
-
$observer_called_names.pop.should == "Name change"
|
66
|
-
end
|
49
|
+
it "should enable an observer via stringified class name" do
|
50
|
+
PersonObserver.called = false
|
51
|
+
ActiveRecord::Observer.with_observers("NoPeepingTomsSpec::PersonObserver") { Person.create! }
|
52
|
+
PersonObserver.called.should be_true
|
53
|
+
end
|
67
54
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
55
|
+
it "should enable an observer via class" do
|
56
|
+
PersonObserver.called = false
|
57
|
+
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) { Person.create! }
|
58
|
+
PersonObserver.called.should be_true
|
72
59
|
end
|
73
60
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
61
|
+
it "should accept multiple observers" do
|
62
|
+
PersonObserver.called = false
|
63
|
+
AnotherObserver.called = false
|
64
|
+
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver, NoPeepingTomsSpec::AnotherObserver) do
|
65
|
+
Person.create!
|
79
66
|
end
|
80
|
-
|
81
|
-
|
82
|
-
$observer_called_names.pop.should be_blank
|
83
|
-
|
84
|
-
$calls_to_another_observer.should == 1
|
67
|
+
PersonObserver.called.should be_true
|
68
|
+
AnotherObserver.called.should be_true
|
85
69
|
end
|
86
70
|
|
87
|
-
it "should
|
88
|
-
|
89
|
-
|
90
|
-
def before_update(person) ; $observer_called_names.push "#{person.name} 1" ; end
|
91
|
-
end
|
92
|
-
observer2 = Class.new(ActiveRecord::Observer) do
|
71
|
+
it "should accept anonymous observers" do
|
72
|
+
called = false
|
73
|
+
observer = Class.new(ActiveRecord::Observer) do
|
93
74
|
observe NoPeepingTomsSpec::Person
|
94
|
-
|
75
|
+
define_method(:before_create) {|person| called = true }
|
95
76
|
end
|
96
|
-
|
97
|
-
|
98
|
-
@person.update_attribute :name, "Name change"
|
99
|
-
$observer_called_names.pop.should == "Name change 2"
|
100
|
-
$observer_called_names.pop.should == "Name change 1"
|
101
|
-
end
|
102
|
-
|
103
|
-
@person.update_attribute :name, "Man Without a Name"
|
104
|
-
$observer_called_names.pop.should be_blank
|
105
|
-
|
106
|
-
$calls_to_another_observer.should == 0
|
77
|
+
ActiveRecord::Observer.with_observers(observer) { Person.create! }
|
78
|
+
called.should be_true
|
107
79
|
end
|
108
80
|
|
109
81
|
it "should ensure peeping toms are reset after raised exception" do
|
data/spec/spec_helper.rb
CHANGED