mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add AM test: after/around callback returning false
This stems from https://github.com/rails/rails/pull/17227#discussion_r21641358 It's simply a clarification of the current behavior by which if an `after_` or `around_` ActiveModel callback returns +false+, then the callback chain **is not halted**. The callback chain in ActiveModel is only halted when a `before_` callback returns `false`.
This commit is contained in:
parent
9a6f20e8f0
commit
b7bd7ffa63
1 changed files with 15 additions and 4 deletions
|
@ -7,6 +7,7 @@ class CallbacksTest < ActiveModel::TestCase
|
|||
model.callbacks << :before_around_create
|
||||
yield
|
||||
model.callbacks << :after_around_create
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -24,16 +25,20 @@ class CallbacksTest < ActiveModel::TestCase
|
|||
|
||||
after_create do |model|
|
||||
model.callbacks << :after_create
|
||||
false
|
||||
end
|
||||
|
||||
after_create "@callbacks << :final_callback"
|
||||
|
||||
def initialize(valid=true)
|
||||
@callbacks, @valid = [], valid
|
||||
def initialize(options = {})
|
||||
@callbacks = []
|
||||
@valid = options[:valid]
|
||||
@before_create_returns = options[:before_create_returns]
|
||||
end
|
||||
|
||||
def before_create
|
||||
@callbacks << :before_create
|
||||
@before_create_returns
|
||||
end
|
||||
|
||||
def create
|
||||
|
@ -51,14 +56,20 @@ class CallbacksTest < ActiveModel::TestCase
|
|||
:after_around_create, :after_create, :final_callback]
|
||||
end
|
||||
|
||||
test "after callbacks are always appended" do
|
||||
test "the callback chain is not halted when around or after callbacks return false" do
|
||||
model = ModelCallbacks.new
|
||||
model.create
|
||||
assert_equal model.callbacks.last, :final_callback
|
||||
end
|
||||
|
||||
test "the callback chain is halted when a before callback returns false" do
|
||||
model = ModelCallbacks.new(before_create_returns: false)
|
||||
model.create
|
||||
assert_equal model.callbacks.last, :before_create
|
||||
end
|
||||
|
||||
test "after callbacks are not executed if the block returns false" do
|
||||
model = ModelCallbacks.new(false)
|
||||
model = ModelCallbacks.new(valid: false)
|
||||
model.create
|
||||
assert_equal model.callbacks, [ :before_create, :before_around_create,
|
||||
:create, :after_around_create]
|
||||
|
|
Loading…
Reference in a new issue