diff options
author | claudiob <claudiob@gmail.com> | 2014-12-08 06:53:24 -0800 |
---|---|---|
committer | claudiob <claudiob@gmail.com> | 2015-01-02 15:31:56 -0800 |
commit | 91b8129320522f664801f122daea4a7628db90a7 (patch) | |
tree | e567a67d852bec7519d9249f70c2983664c75459 /activemodel/test/cases | |
parent | f767981286b4c7dcb96e061a6f3edcc334008ea8 (diff) | |
download | rails-91b8129320522f664801f122daea4a7628db90a7.tar.gz rails-91b8129320522f664801f122daea4a7628db90a7.tar.bz2 rails-91b8129320522f664801f122daea4a7628db90a7.zip |
Deprecate `false` as the way to halt AM callbacks
Before this commit, returning `false` in an ActiveModel `before_` callback
such as `before_create` would halt the callback chain.
After this commit, the behavior is deprecated: will still work until
the next release of Rails but will also display a deprecation warning.
The preferred way to halt a callback chain is to explicitly `throw(:abort)`.
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/callbacks_test.rb | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index 2ac681b8d8..85455c112c 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -33,11 +33,13 @@ class CallbacksTest < ActiveModel::TestCase def initialize(options = {}) @callbacks = [] @valid = options[:valid] - @before_create_returns = options[:before_create_returns] + @before_create_returns = options.fetch(:before_create_returns, true) + @before_create_throws = options[:before_create_throws] end def before_create @callbacks << :before_create + throw(@before_create_throws) if @before_create_throws @before_create_returns end @@ -62,10 +64,18 @@ class CallbacksTest < ActiveModel::TestCase assert_equal model.callbacks.last, :final_callback end - test "the callback chain is halted when a before callback returns false" do + test "the callback chain is halted when a before callback returns false (deprecated)" do model = ModelCallbacks.new(before_create_returns: false) + assert_deprecated do + model.create + assert_equal model.callbacks.last, :before_create + end + end + + test "the callback chain is halted when a callback throws :abort" do + model = ModelCallbacks.new(before_create_throws: :abort) model.create - assert_equal model.callbacks.last, :before_create + assert_equal model.callbacks, [:before_create] end test "after callbacks are not executed if the block returns false" do |