mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Allow access to a callback event's return result from around callbacks
This commit is contained in:
committed by
José Valim
parent
1408b942d9
commit
df615f127e
@@ -225,7 +225,10 @@ module ActiveSupport
|
||||
# end
|
||||
[@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n")
|
||||
when :around
|
||||
"end"
|
||||
<<-RUBY_EVAL
|
||||
value
|
||||
end
|
||||
RUBY_EVAL
|
||||
end
|
||||
end
|
||||
|
||||
@@ -423,7 +426,7 @@ module ActiveSupport
|
||||
#
|
||||
# set_callback :save, :before, :before_meth
|
||||
# set_callback :save, :after, :after_meth, :if => :condition
|
||||
# set_callback :save, :around, lambda { |r| stuff; yield; stuff }
|
||||
# set_callback :save, :around, lambda { |r| stuff; result = yield; stuff }
|
||||
#
|
||||
# The second arguments indicates whether the callback is to be run +:before+,
|
||||
# +:after+, or +:around+ the event. If omitted, +:before+ is assumed. This
|
||||
@@ -442,6 +445,9 @@ module ActiveSupport
|
||||
#
|
||||
# Before and around callbacks are called in the order that they are set; after
|
||||
# callbacks are called in the reverse order.
|
||||
#
|
||||
# Around callbacks can access the return value from the event, if it
|
||||
# wasn't halted, from the +yield+ call.
|
||||
#
|
||||
# ===== Options
|
||||
#
|
||||
|
||||
@@ -299,6 +299,22 @@ module CallbacksTest
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class AroundPersonResult < MySuper
|
||||
attr_reader :result
|
||||
|
||||
set_callback :save, :around, :tweedle_dum
|
||||
|
||||
def tweedle_dum
|
||||
@result = yield
|
||||
end
|
||||
|
||||
def save
|
||||
run_callbacks :save do
|
||||
:running
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class HyphenatedCallbacks
|
||||
include ActiveSupport::Callbacks
|
||||
@@ -338,6 +354,14 @@ module CallbacksTest
|
||||
], around.history
|
||||
end
|
||||
end
|
||||
|
||||
class AroundCallbackResultTest < Test::Unit::TestCase
|
||||
def test_save_around
|
||||
around = AroundPersonResult.new
|
||||
around.save
|
||||
assert_equal :running, around.result
|
||||
end
|
||||
end
|
||||
|
||||
class SkipCallbacksTest < Test::Unit::TestCase
|
||||
def test_skip_person
|
||||
|
||||
Reference in New Issue
Block a user