diff options
Diffstat (limited to 'activemodel/test/cases/callbacks_test.rb')
-rw-r--r-- | activemodel/test/cases/callbacks_test.rb | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/activemodel/test/cases/callbacks_test.rb b/activemodel/test/cases/callbacks_test.rb index 086e7266ff..2ac681b8d8 100644 --- a/activemodel/test/cases/callbacks_test.rb +++ b/activemodel/test/cases/callbacks_test.rb @@ -7,6 +7,7 @@ class CallbacksTest < ActiveModel::TestCase model.callbacks << :before_around_create yield model.callbacks << :after_around_create + false end end @@ -15,25 +16,29 @@ class CallbacksTest < ActiveModel::TestCase extend ActiveModel::Callbacks define_model_callbacks :create - define_model_callbacks :initialize, :only => :after - define_model_callbacks :multiple, :only => [:before, :around] - define_model_callbacks :empty, :only => [] + define_model_callbacks :initialize, only: :after + define_model_callbacks :multiple, only: [:before, :around] + define_model_callbacks :empty, only: [] before_create :before_create around_create CallbackValidator.new 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] @@ -107,7 +118,7 @@ class CallbacksTest < ActiveModel::TestCase test "after_create callbacks with both callbacks declared in one line" do assert_equal ["callback1", "callback2"], Violin1.new.create.history end - test "after_create callbacks with both callbacks declared in differnt lines" do + test "after_create callbacks with both callbacks declared in different lines" do assert_equal ["callback1", "callback2"], Violin2.new.create.history end |