Skip to content

Commit

Permalink
Merge pull request rails#424 from gnufied/master
Browse files Browse the repository at this point in the history
Fixes around_filter from observer, github issue#329
  • Loading branch information
josevalim committed May 6, 2011
2 parents 886818d + 20c35bc commit 8bbf47a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/observer.rb
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/lifecycle_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8bbf47a

Please sign in to comment.