Browse files

fix yield not working from around filter of observers, fixes Github#329

  • Loading branch information...
1 parent 9c2c25c commit 20c35bca5adb515b3c4ec6c2b10cba1f6c0217aa Hemant Kumar committed May 7, 2011
Showing with 27 additions and 2 deletions.
  1. +2 −2 activerecord/lib/active_record/observer.rb
  2. +25 −0 activerecord/test/cases/lifecycle_test.rb
View
4 activerecord/lib/active_record/observer.rb
@@ -110,8 +110,8 @@ def define_callbacks(klass)
next unless respond_to?(callback)
callback_meth = :"_notify_#{observer_name}_for_#{callback}"
unless klass.respond_to?(callback_meth)
- klass.send(:define_method, callback_meth) do
- observer.send(callback, self)
+ klass.send(:define_method, callback_meth) do |&block|
+ observer.send(callback, self, &block)
end
klass.send(callback, callback_meth)
end
View
25 activerecord/test/cases/lifecycle_test.rb
@@ -107,6 +107,23 @@ def after_validation(model)
end
end
+
+class AroundTopic < Topic
+end
+
+class AroundTopicObserver < ActiveRecord::Observer
+ observe :around_topic
+ def topic_ids
+ @topic_ids ||= []
+ end
+
+ def around_save(topic)
+ topic_ids << topic.id
+ yield(topic)
+ topic_ids << topic.id
+ end
+end
+
class LifecycleTest < ActiveRecord::TestCase
fixtures :topics, :developers, :minimalistics
@@ -206,6 +223,14 @@ def test_invalid_observer
assert_equal developer, SalaryChecker.instance.last_saved
end
+ test "around filter from observer should accept block" do
+ observer = AroundTopicObserver.instance
+ topic = AroundTopic.new
+ topic.save
+ assert_nil observer.topic_ids.first
+ assert_not_nil observer.topic_ids.last
+ end
+
def test_observer_is_called_once
observer = DeveloperObserver.instance # activate
observer.calls.clear

0 comments on commit 20c35bc

Please sign in to comment.