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 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
- You can choose to run some code with observers turned on. This is useful when spec'ing an observer. For example, if you write the following observer:
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
@@ -19,6 +19,7 @@ Jeweler::Tasks.new do |s|
19
19
  s.add_dependency "activerecord", ['>= 1.1']
20
20
  s.files -= ['.gitignore', 'init.rb']
21
21
  s.test_files = Dir['spec/**/*']
22
+ s.test_files -= ['spec/debug.log']
22
23
  end
23
24
 
24
25
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -1,16 +1,19 @@
1
1
  module NoPeepingToms
2
2
  def self.included(base)
3
- base.send :include, NoPeepingToms::InstanceMethods
4
- base.extend NoPeepingToms::ClassMethods
5
- base.alias_method_chain :update, :neighborhood_watch
6
- base.cattr_accessor :allow_peeping_toms, :peeping_toms
7
- base.allow_peeping_toms = false
8
- base.peeping_toms = [] # toms that are allowed to peep
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.allow_peeping_toms || self.class.peeping_toms.include?(self)
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
 
@@ -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.1"
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
- def before_update(person)
8
- $observer_called_names.push person.name
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
- def before_update(person)
15
- $calls_to_another_observer += 1
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
- it "should not register a name change" do
27
- @person.update_attribute :name, "Name change"
28
- $observer_called_names.pop.should be_blank
29
- $calls_to_another_observer.should == 0
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 "should register a name change with the person observer turned on by name" do
33
- ActiveRecord::Observer.with_observers("NoPeepingTomsSpec::PersonObserver") do
34
- @person.update_attribute :name, "Name change"
35
- $observer_called_names.pop.should == "Name change"
36
- end
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
- @person.update_attribute :name, "Man Without a Name"
39
- $observer_called_names.pop.should be_blank
40
-
41
- $calls_to_another_observer.should == 0
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
- @person.update_attribute :name, "Man Without a Name"
51
- $observer_called_names.pop.should be_blank
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 register a name change with an anonymous observer" do
57
- observer = Class.new(ActiveRecord::Observer) do
58
- observe NoPeepingTomsSpec::Person
59
- def before_update(person)
60
- $observer_called_names.push person.name
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
- @person.update_attribute :name, "Man Without a Name"
69
- $observer_called_names.pop.should be_blank
70
-
71
- $calls_to_another_observer.should == 0
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
- it "should handle multiple observers" do
76
- ActiveRecord::Observer.with_observers("NoPeepingTomsSpec::PersonObserver", "NoPeepingTomsSpec::AnotherObserver") do
77
- @person.update_attribute :name, "Name change"
78
- $observer_called_names.pop.should == "Name change"
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
- @person.update_attribute :name, "Man Without a Name"
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 handle multiple anonymous observers" do
88
- observer1 = Class.new(ActiveRecord::Observer) do
89
- observe NoPeepingTomsSpec::Person
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
- def before_update(person) ; $observer_called_names.push "#{person.name} 2" ; end
75
+ define_method(:before_create) {|person| called = true }
95
76
  end
96
-
97
- ActiveRecord::Observer.with_observers(observer1, observer2) do
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
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../../spec/spec_helper')
2
2
 
3
3
  plugin_spec_dir = File.dirname(__FILE__)
4
4
  ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: no_peeping_toms
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Maddox