no_peeping_toms 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,40 @@
1
+ no_peeping_toms
2
+ =============
3
+
4
+ This plugin disables observers in your specs, so that model specs can run in complete isolation.
5
+
6
+ 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:
7
+
8
+ class PersonObserver < ActiveRecord::Observer
9
+ def before_update(person)
10
+ old_person = Person.find person.id
11
+ if old_person.name != person.name
12
+ NameChange.create! :person => person, :old_name => old_person.name, :new_name => person.name
13
+ end
14
+ end
15
+ end
16
+
17
+ You can spec the Person class in complete isolation.
18
+
19
+ describe Person, " when changing a name" do
20
+ before(:each) do
21
+ @person = Person.create! :name => "Pat Maddox"
22
+ end
23
+
24
+ # By default, don't run any observers
25
+ it "should not register a name change" do
26
+ lambda { @person.update_attribute :name, "Don Juan Demarco" }.should_not change(NameChange, :count)
27
+ end
28
+
29
+ # Run only a portion of code with certain observers turned on
30
+ it "should register a name change with the person observer turned on" do
31
+ Person.with_observers(:person_observer) do
32
+ lambda { @person.update_attribute :name, "Don Juan Demarco" }.should change(NameChange, :count).by(1)
33
+ end
34
+
35
+ lambda { @person.update_attribute :name, "Man Without a Name" }.should_not change(NameChange, :count)
36
+ end
37
+ end
38
+
39
+
40
+ Copyright (c) 2007 Pat Maddox, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'jeweler'
3
+ rescue LoadError
4
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
5
+ exit 1
6
+ end
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+ require 'rcov/rcovtask'
10
+ require "load_multi_rails_rake_tasks"
11
+
12
+ Jeweler::Tasks.new do |s|
13
+ s.name = "no_peeping_toms"
14
+ s.summary = "Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer."
15
+ s.description = s.summary
16
+ s.email = "pat.maddox@gmail.com"
17
+ s.homepage = "http://github.com/pat-maddox"
18
+ s.authors = ["Pat Maddox", "Brandon Keepers"]
19
+ s.add_dependency "activerecord", ['>= 1.1']
20
+ s.test_files = Dir['spec/**/*']
21
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'no_peeping_toms' if "test" == RAILS_ENV
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,32 @@
1
+ module NoPeepingToms
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
9
+ end
10
+
11
+ module InstanceMethods
12
+ def update_with_neighborhood_watch(*args)
13
+ puts "calling tom's #update: #{*args.inspect} -- #{self.class.allow_peeping_toms}"
14
+ if self.class.allow_peeping_toms || self.class.peeping_toms.include?(self)
15
+ update_without_neighborhood_watch(*args)
16
+ end
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+ def with_observers(*observer_syms)
22
+ self.peeping_toms = Array(observer_syms).map do |o|
23
+ o.respond_to?(:instance) ? o.instance : o.to_s.classify.constantize.instance
24
+ end
25
+ yield
26
+ ensure
27
+ self.peeping_toms.clear
28
+ end
29
+ end
30
+ end
31
+
32
+ ActiveRecord::Observer.send :include, NoPeepingToms
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{no_peeping_toms}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Pat Maddox", "Brandon Keepers"]
9
+ s.date = %q{2009-08-17}
10
+ s.description = %q{Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer.}
11
+ s.email = %q{pat.maddox@gmail.com}
12
+ s.extra_rdoc_files = [
13
+ "README"
14
+ ]
15
+ s.files = [
16
+ "README",
17
+ "Rakefile",
18
+ "VERSION",
19
+ "init.rb",
20
+ "install.rb",
21
+ "lib/no_peeping_toms.rb",
22
+ "no_peeping_toms.gemspec",
23
+ "spec/db/database.yml",
24
+ "spec/db/schema.rb",
25
+ "spec/no_peeping_toms_spec.rb",
26
+ "spec/spec_helper.rb",
27
+ "tasks/no_peeping_toms_tasks.rake",
28
+ "uninstall.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/pat-maddox}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.3}
34
+ s.summary = %q{Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer.}
35
+ s.test_files = [
36
+ "spec/db",
37
+ "spec/db/database.yml",
38
+ "spec/db/schema.rb",
39
+ "spec/no_peeping_toms_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<activerecord>, [">= 1.1"])
49
+ else
50
+ s.add_dependency(%q<activerecord>, [">= 1.1"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<activerecord>, [">= 1.1"])
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ :adapter: sqlite3
3
+ :dbfile: ":memory:"
data/spec/db/schema.rb ADDED
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :people, :force => true do |t|
3
+ t.column :name, :string
4
+ end
5
+ end
@@ -0,0 +1,118 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ module NoPeepingTomsSpec
4
+ class Person < ActiveRecord::Base; end
5
+
6
+ class PersonObserver < ActiveRecord::Observer
7
+ def before_update(person)
8
+ $observer_called_names.push person.name
9
+ end
10
+ end
11
+
12
+ class AnotherObserver < ActiveRecord::Observer
13
+ observe Person
14
+ def before_update(person)
15
+ $calls_to_another_observer += 1
16
+ end
17
+ 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
+
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
30
+ end
31
+
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
37
+
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
42
+ 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
49
+
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
54
+ end
55
+
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
67
+
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
72
+ end
73
+
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"
79
+ 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
85
+ end
86
+
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
93
+ observe NoPeepingTomsSpec::Person
94
+ def before_update(person) ; $observer_called_names.push "#{person.name} 2" ; end
95
+ 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
107
+ end
108
+
109
+ it "should ensure peeping toms are reset after raised exception" do
110
+ lambda {
111
+ ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) do
112
+ raise ArgumentError, "Michael, I've made a huge mistake"
113
+ end
114
+ }.should raise_error(ArgumentError)
115
+ ActiveRecord::Observer.peeping_toms.should == []
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
2
+
3
+ plugin_spec_dir = File.dirname(__FILE__)
4
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
5
+
6
+ databases = YAML::load(IO.read(plugin_spec_dir + "/db/database.yml"))
7
+ ActiveRecord::Base.establish_connection(databases[ENV["DB"] || "sqlite3"])
8
+ load(File.join(plugin_spec_dir, "db", "schema.rb"))
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :no_peeping_toms do
3
+ # # Task goes here
4
+ # end
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: no_peeping_toms
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pat Maddox
8
+ - Brandon Keepers
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-08-17 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activerecord
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "1.1"
25
+ version:
26
+ description: Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer.
27
+ email: pat.maddox@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - README
34
+ files:
35
+ - README
36
+ - Rakefile
37
+ - VERSION
38
+ - init.rb
39
+ - install.rb
40
+ - lib/no_peeping_toms.rb
41
+ - no_peeping_toms.gemspec
42
+ - spec/db/database.yml
43
+ - spec/db/schema.rb
44
+ - spec/no_peeping_toms_spec.rb
45
+ - spec/spec_helper.rb
46
+ - tasks/no_peeping_toms_tasks.rake
47
+ - uninstall.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/pat-maddox
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.3
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer.
76
+ test_files:
77
+ - spec/db/database.yml
78
+ - spec/db/schema.rb
79
+ - spec/no_peeping_toms_spec.rb
80
+ - spec/spec_helper.rb