aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations/callbacks_test.rb
diff options
context:
space:
mode:
authorclaudiob <claudiob@gmail.com>2014-12-08 06:35:25 -0800
committerclaudiob <claudiob@gmail.com>2015-01-02 15:31:56 -0800
commitf767981286b4c7dcb96e061a6f3edcc334008ea8 (patch)
tree35afe22d27693178a1f0a08f20e36c7eeb5a9189 /activemodel/test/cases/validations/callbacks_test.rb
parentd217daf6a740de7e4925872abe632982cfaab89b (diff)
downloadrails-f767981286b4c7dcb96e061a6f3edcc334008ea8.tar.gz
rails-f767981286b4c7dcb96e061a6f3edcc334008ea8.tar.bz2
rails-f767981286b4c7dcb96e061a6f3edcc334008ea8.zip
Deprecate `false` as the way to halt AM validation callbacks
Before this commit, returning `false` in an ActiveModel validation callback such as `before_validation` 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/validations/callbacks_test.rb')
-rw-r--r--activemodel/test/cases/validations/callbacks_test.rb20
1 files changed, 17 insertions, 3 deletions
diff --git a/activemodel/test/cases/validations/callbacks_test.rb b/activemodel/test/cases/validations/callbacks_test.rb
index 5d6d48b824..4b0dd58efb 100644
--- a/activemodel/test/cases/validations/callbacks_test.rb
+++ b/activemodel/test/cases/validations/callbacks_test.rb
@@ -30,11 +30,16 @@ class DogWithTwoValidators < Dog
before_validation { self.history << 'before_validation_marker2' }
end
-class DogBeforeValidatorReturningFalse < Dog
+class DogDeprecatedBeforeValidatorReturningFalse < Dog
before_validation { false }
before_validation { self.history << 'before_validation_marker2' }
end
+class DogBeforeValidatorThrowingAbort < Dog
+ before_validation { throw :abort }
+ before_validation { self.history << 'before_validation_marker2' }
+end
+
class DogAfterValidatorReturningFalse < Dog
after_validation { false }
after_validation { self.history << 'after_validation_marker' }
@@ -86,13 +91,22 @@ class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase
assert_equal ['before_validation_marker1', 'before_validation_marker2'], d.history
end
- def test_further_callbacks_should_not_be_called_if_before_validation_returns_false
- d = DogBeforeValidatorReturningFalse.new
+ def test_further_callbacks_should_not_be_called_if_before_validation_throws_abort
+ d = DogBeforeValidatorThrowingAbort.new
output = d.valid?
assert_equal [], d.history
assert_equal false, output
end
+ def test_deprecated_further_callbacks_should_not_be_called_if_before_validation_returns_false
+ d = DogDeprecatedBeforeValidatorReturningFalse.new
+ assert_deprecated do
+ output = d.valid?
+ assert_equal [], d.history
+ assert_equal false, output
+ end
+ end
+
def test_further_callbacks_should_be_called_if_after_validation_returns_false
d = DogAfterValidatorReturningFalse.new
d.valid?