diff options
author | José Valim <jose.valim@gmail.com> | 2011-08-13 08:40:13 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-08-13 08:40:13 -0700 |
commit | 291072a64a2b191816f395b25aae4a50d2dcfe16 (patch) | |
tree | 9f4f4a80ce6058a5c298fd79ae8cde96fd45b967 | |
parent | 34689c40a03c9921b5c43ac1e120a9885edded73 (diff) | |
parent | d3c15a1d31d77e44b142c96cb55b654f3552a89d (diff) | |
download | rails-291072a64a2b191816f395b25aae4a50d2dcfe16.tar.gz rails-291072a64a2b191816f395b25aae4a50d2dcfe16.tar.bz2 rails-291072a64a2b191816f395b25aae4a50d2dcfe16.zip |
Merge pull request #2514 from myronmarston/fix_active_record_observer_disabling
Allow ActiveRecord observers to be disabled.
-rw-r--r-- | activemodel/lib/active_model/observing.rb | 4 | ||||
-rw-r--r-- | activemodel/test/cases/observing_test.rb | 12 | ||||
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/lifecycle_test.rb | 12 |
4 files changed, 27 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index d48f2e8a1f..7a910d18e7 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -226,10 +226,10 @@ module ActiveModel # Send observed_method(object) if the method exists and # the observer is enabled for the given object's class. - def update(observed_method, object) #:nodoc: + def update(observed_method, object, &block) #:nodoc: return unless respond_to?(observed_method) return if disabled_for?(object) - send(observed_method, object) + send(observed_method, object, &block) end # Special method sent by the observed class when it is inherited. diff --git a/activemodel/test/cases/observing_test.rb b/activemodel/test/cases/observing_test.rb index 99b1f407ae..f6ec24ae57 100644 --- a/activemodel/test/cases/observing_test.rb +++ b/activemodel/test/cases/observing_test.rb @@ -17,6 +17,10 @@ class FooObserver < ActiveModel::Observer def on_spec(record) stub.event_with(record) if stub end + + def around_save(record) + yield :in_around_save + end end class Foo @@ -133,4 +137,12 @@ class ObserverTest < ActiveModel::TestCase foo = Foo.new Foo.send(:notify_observers, :whatever, foo) end + + test "update passes a block on to the observer" do + yielded_value = nil + FooObserver.instance.update(:around_save, Foo.new) do |val| + yielded_value = val + end + assert_equal :in_around_save, yielded_value + end end diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 5a5351b517..fdf17c003c 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -111,7 +111,7 @@ module ActiveRecord callback_meth = :"_notify_#{observer_name}_for_#{callback}" unless klass.respond_to?(callback_meth) klass.send(:define_method, callback_meth) do |&block| - observer.send(callback, self, &block) + observer.update(callback, self, &block) end klass.send(callback, callback_meth) end diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 643e949087..75e5dfa49b 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -231,6 +231,18 @@ class LifecycleTest < ActiveRecord::TestCase assert_not_nil observer.topic_ids.last end + test "able to disable observers" do + observer = DeveloperObserver.instance # activate + observer.calls.clear + + ActiveRecord::Base.observers.disable DeveloperObserver do + Developer.create! :name => 'Ancestor', :salary => 100000 + SpecialDeveloper.create! :name => 'Descendent', :salary => 100000 + end + + assert_equal [], observer.calls + end + def test_observer_is_called_once observer = DeveloperObserver.instance # activate observer.calls.clear |