Skip to content

Commit

Permalink
Updated #with_observers to take in class references, or an anonymous …
Browse files Browse the repository at this point in the history
…observer
  • Loading branch information
zdennis committed Oct 3, 2008
1 parent 732e921 commit 7b7939c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/no_peeping_toms.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
module NoPeepingToms
def with_observers(*observer_syms)
observer_names = [observer_syms].flatten
observers = observer_names.map { |o| o.to_s.classify.constantize.instance }
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
end
end

observers.each { |o| old_add_observer(o) }
yield
Expand Down
52 changes: 51 additions & 1 deletion spec/no_peeping_toms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def before_update(person)
$calls_to_another_observer.should == 0
end

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

it "should register a name change with the person observer turned on by class reference" do
Person.with_observers(NoPeepingTomsSpec::PersonObserver) do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change"
end

@person.update_attribute :name, "Man Without a Name"
$observer_called_names.pop.should be_blank

$calls_to_another_observer.should == 0
end

it "should register a name change with an anonymous observer" do
observer = Class.new(ActiveRecord::Observer) do
def before_update(person)
$observer_called_names.push person.name
end
end
Person.with_observers(observer) do
@person.update_attribute :name, "Name change"
$observer_called_names.pop.should == "Name change"
end

@person.update_attribute :name, "Man Without a Name"
$observer_called_names.pop.should be_blank

$calls_to_another_observer.should == 0
end


it "should handle multiple observers" do
Person.with_observers("NoPeepingTomsSpec::PersonObserver", "NoPeepingTomsSpec::AnotherObserver") do
@person.update_attribute :name, "Name change"
Expand All @@ -52,5 +82,25 @@ def before_update(person)

$calls_to_another_observer.should == 1
end

it "should handle multiple anonymous observers" do
observer1 = Class.new(ActiveRecord::Observer) do
def before_update(person) ; $observer_called_names.push "#{person.name} 1" ; end
end
observer2 = Class.new(ActiveRecord::Observer) do
def before_update(person) ; $observer_called_names.push "#{person.name} 2" ; end
end

Person.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"
end

@person.update_attribute :name, "Man Without a Name"
$observer_called_names.pop.should be_blank

$calls_to_another_observer.should == 0
end
end
end

0 comments on commit 7b7939c

Please sign in to comment.