From 7b7939c02ef42980afb69400fc898c59efceab18 Mon Sep 17 00:00:00 2001 From: Zach Dennis Date: Fri, 3 Oct 2008 12:53:34 -0400 Subject: [PATCH] Updated #with_observers to take in class references, or an anonymous observer --- lib/no_peeping_toms.rb | 8 +++++- spec/no_peeping_toms_spec.rb | 52 +++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/no_peeping_toms.rb b/lib/no_peeping_toms.rb index 4e5ee78..ec7f78e 100644 --- a/lib/no_peeping_toms.rb +++ b/lib/no_peeping_toms.rb @@ -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 diff --git a/spec/no_peeping_toms_spec.rb b/spec/no_peeping_toms_spec.rb index 7b5f3e2..9890e38 100644 --- a/spec/no_peeping_toms_spec.rb +++ b/spec/no_peeping_toms_spec.rb @@ -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" @@ -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" @@ -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