diff --git a/init.rb b/init.rb index edb33f4..de8fcd6 100644 --- a/init.rb +++ b/init.rb @@ -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 diff --git a/lib/no_peeping_toms.rb b/lib/no_peeping_toms.rb index ec7f78e..6d02720 100644 --- a/lib/no_peeping_toms.rb +++ b/lib/no_peeping_toms.rb @@ -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 diff --git a/spec/db/database.yml b/spec/db/database.yml index 5746746..6dda097 100644 --- a/spec/db/database.yml +++ b/spec/db/database.yml @@ -1,3 +1,3 @@ sqlite3: :adapter: sqlite3 - :dbfile: vendor/plugins/no_peeping_toms/spec/db/no_peeping_toms.sqlite3.db + :dbfile: ":memory:" diff --git a/spec/no_peeping_toms_spec.rb b/spec/no_peeping_toms_spec.rb index 9890e38..69b787c 100644 --- a/spec/no_peeping_toms_spec.rb +++ b/spec/no_peeping_toms_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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"