diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-06 14:50:14 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-06 14:50:14 -0700 |
commit | 8bbf47ad5f7092eb5c0b2f8eb7a60b03047fdfce (patch) | |
tree | 61d8aedef27eee1749e92b73767bdccc1abfe082 | |
parent | 886818d2bab40585c0cea763002ffc16917dd0b3 (diff) | |
parent | 20c35bca5adb515b3c4ec6c2b10cba1f6c0217aa (diff) | |
download | rails-8bbf47ad5f7092eb5c0b2f8eb7a60b03047fdfce.tar.gz rails-8bbf47ad5f7092eb5c0b2f8eb7a60b03047fdfce.tar.bz2 rails-8bbf47ad5f7092eb5c0b2f8eb7a60b03047fdfce.zip |
Merge pull request #424 from gnufied/master
Fixes around_filter from observer, github issue#329
-rw-r--r-- | activerecord/lib/active_record/observer.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/lifecycle_test.rb | 25 |
2 files changed, 27 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/observer.rb b/activerecord/lib/active_record/observer.rb index 0893d7e337..c723436330 100644 --- a/activerecord/lib/active_record/observer.rb +++ b/activerecord/lib/active_record/observer.rb @@ -110,8 +110,8 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 6cd8494c9e..643e949087 100644 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -107,6 +107,23 @@ class ValidatedCommentObserver < ActiveRecord::Observer 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 @@ class LifecycleTest < ActiveRecord::TestCase 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 |