Permalink
Browse files

Merge pull request #424 from gnufied/master

Fixes around_filter from observer, github issue#329
  • Loading branch information...
2 parents 886818d + 20c35bc commit 8bbf47ad5f7092eb5c0b2f8eb7a60b03047fdfce @josevalim josevalim committed May 6, 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
@@ -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
@@ -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 8bbf47a

Please sign in to comment.