Skip to content

Commit

Permalink
handle multiple observers
Browse files Browse the repository at this point in the history
git-svn-id: svn://evang.eli.st/public/plugins/no_peeping_toms@1465 82013aef-fb27-0410-99a1-c1893322ecab
  • Loading branch information
pat committed Nov 22, 2007
1 parent 79078ca commit 732e921
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/no_peeping_toms.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module NoPeepingToms
def with_observers(observer_syms = [])
def with_observers(*observer_syms)
observer_names = [observer_syms].flatten
observers = observer_names.map { |o| o.to_s.classify.constantize.instance }

Expand Down
33 changes: 28 additions & 5 deletions spec/no_peeping_toms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,52 @@ class Person < ActiveRecord::Base; end

class PersonObserver < ActiveRecord::Observer
def before_update(person)
$observer_calls.push person.name
$observer_called_names.push person.name
end
end

class AnotherObserver < ActiveRecord::Observer
observe Person
def before_update(person)
$calls_to_another_observer += 1
end
end

describe Person, " when changing a name" do
before(:each) do
$observer_calls = []
$observer_called_names = []
$calls_to_another_observer = 0
@person = Person.create! :name => "Pat Maddox"
end

it "should not register a name change" do
@person.update_attribute :name, "Name change"
$observer_calls.pop.should be_blank
$observer_called_names.pop.should be_blank
$calls_to_another_observer.should == 0
end

it "should register a name change with the person observer turned on" do
Person.with_observers("NoPeepingTomsSpec::PersonObserver") do
@person.update_attribute :name, "Name change"
$observer_calls.pop.should == "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"
$observer_called_names.pop.should == "Name change"
end

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

$calls_to_another_observer.should == 1
end
end
end

0 comments on commit 732e921

Please sign in to comment.