Skip to content

Commit

Permalink
Rewrite to allow peeping toms to be re-enabled, specifically for use …
Browse files Browse the repository at this point in the history
…in cucumber

To enable:
  ActiveRecord::Observer.allow_peeping_toms = true
  • Loading branch information
bkeepers committed Jan 14, 2009
1 parent 7b7939c commit 06ff4a4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
11 changes: 1 addition & 10 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
if "test" == RAILS_ENV

ActiveRecord::Base.class_eval do
class << self
alias_method :old_add_observer, :add_observer
def add_observer(o); end
end

extend NoPeepingToms
end

ActiveRecord::Observer.send :include, NoPeepingToms
end
34 changes: 23 additions & 11 deletions lib/no_peeping_toms.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
module NoPeepingToms
def with_observers(*observer_syms)
observer_names = [observer_syms].flatten
observers = observer_names.map do |o|
if o.respond_to?(:instance) && o.instance.is_a?(ActiveRecord::Observer)
o.instance
else
o.to_s.classify.constantize.instance
def self.included(base)
base.send :include, NoPeepingToms::InstanceMethods
base.extend NoPeepingToms::ClassMethods
base.alias_method_chain :update, :neighborhood_watch
base.cattr_accessor :allow_peeping_toms, :peeping_toms
base.allow_peeping_toms = false
base.peeping_toms = [] # toms that are allowed to peep
end

module InstanceMethods
def update_with_neighborhood_watch(*args)
if self.class.allow_peeping_toms || self.class.peeping_toms.include?(self)
update_without_neighborhood_watch(*args)
end
end
end

module ClassMethods
def with_observers(*observer_syms)
self.peeping_toms = Array(observer_syms).map do |o|
o.respond_to?(:instance) ? o.instance : o.to_s.classify.constantize.instance
end
yield
self.peeping_toms.clear
end

observers.each { |o| old_add_observer(o) }
yield
observers.each { |o| delete_observer(o) }
end
end
2 changes: 1 addition & 1 deletion spec/db/database.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sqlite3:
:adapter: sqlite3
:dbfile: vendor/plugins/no_peeping_toms/spec/db/no_peeping_toms.sqlite3.db
:dbfile: ":memory:"
10 changes: 5 additions & 5 deletions spec/no_peeping_toms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def before_update(person)
end

it "should register a name change with the person observer turned on by name" do
Person.with_observers("NoPeepingTomsSpec::PersonObserver") do
ActiveRecord::Observer.with_observers("NoPeepingTomsSpec::PersonObserver") do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change"
end
Expand All @@ -42,7 +42,7 @@ def before_update(person)
end

it "should register a name change with the person observer turned on by class reference" do
Person.with_observers(NoPeepingTomsSpec::PersonObserver) do
ActiveRecord::Observer.with_observers(NoPeepingTomsSpec::PersonObserver) do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change"
end
Expand All @@ -59,7 +59,7 @@ def before_update(person)
$observer_called_names.push person.name
end
end
Person.with_observers(observer) do
ActiveRecord::Observer.with_observers(observer) do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change"
end
Expand All @@ -72,7 +72,7 @@ def before_update(person)


it "should handle multiple observers" do
Person.with_observers("NoPeepingTomsSpec::PersonObserver", "NoPeepingTomsSpec::AnotherObserver") do
ActiveRecord::Observer.with_observers("NoPeepingTomsSpec::PersonObserver", "NoPeepingTomsSpec::AnotherObserver") do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change"
end
Expand All @@ -91,7 +91,7 @@ def before_update(person) ; $observer_called_names.push "#{person.name} 1" ; end
def before_update(person) ; $observer_called_names.push "#{person.name} 2" ; end
end

Person.with_observers(observer1, observer2) do
ActiveRecord::Observer.with_observers(observer1, observer2) do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change 2"
$observer_called_names.pop.should == "Name change 1"
Expand Down

0 comments on commit 06ff4a4

Please sign in to comment.