2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
2010-06-16 11:30:37 -04:00
|
|
|
|
|
|
|
class Dog
|
|
|
|
include ActiveModel::Validations
|
2010-06-19 12:18:45 -04:00
|
|
|
include ActiveModel::Validations::Callbacks
|
2010-06-16 11:30:37 -04:00
|
|
|
|
2012-01-24 15:04:04 -05:00
|
|
|
attr_accessor :name, :history
|
2010-06-16 11:30:37 -04:00
|
|
|
|
2012-01-24 15:04:04 -05:00
|
|
|
def initialize
|
|
|
|
@history = []
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DogWithMethodCallbacks < Dog
|
|
|
|
before_validation :set_before_validation_marker
|
|
|
|
after_validation :set_after_validation_marker
|
|
|
|
|
2016-08-07 19:05:28 -04:00
|
|
|
def set_before_validation_marker; history << "before_validation_marker"; end
|
|
|
|
def set_after_validation_marker; history << "after_validation_marker" ; end
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
2012-07-24 04:01:41 -04:00
|
|
|
class DogValidatorsAreProc < Dog
|
2016-08-07 19:05:28 -04:00
|
|
|
before_validation { history << "before_validation_marker" }
|
|
|
|
after_validation { history << "after_validation_marker" }
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class DogWithTwoValidators < Dog
|
2016-08-07 19:05:28 -04:00
|
|
|
before_validation { history << "before_validation_marker1" }
|
|
|
|
before_validation { history << "before_validation_marker2" }
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
2017-02-07 10:13:15 -05:00
|
|
|
class DogBeforeValidatorReturningFalse < Dog
|
2010-06-16 11:30:37 -04:00
|
|
|
before_validation { false }
|
2016-08-07 19:05:28 -04:00
|
|
|
before_validation { history << "before_validation_marker2" }
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
2014-12-08 09:35:25 -05:00
|
|
|
class DogBeforeValidatorThrowingAbort < Dog
|
|
|
|
before_validation { throw :abort }
|
2016-08-07 19:05:28 -04:00
|
|
|
before_validation { history << "before_validation_marker2" }
|
2014-12-08 09:35:25 -05:00
|
|
|
end
|
|
|
|
|
2014-12-14 22:30:41 -05:00
|
|
|
class DogAfterValidatorReturningFalse < Dog
|
|
|
|
after_validation { false }
|
2016-08-07 19:05:28 -04:00
|
|
|
after_validation { history << "after_validation_marker" }
|
2014-12-14 22:30:41 -05:00
|
|
|
end
|
|
|
|
|
2010-06-16 11:30:37 -04:00
|
|
|
class DogWithMissingName < Dog
|
2016-08-07 19:05:28 -04:00
|
|
|
before_validation { history << "before_validation_marker" }
|
2010-06-16 11:30:37 -04:00
|
|
|
validates_presence_of :name
|
|
|
|
end
|
|
|
|
|
2015-01-11 15:25:53 -05:00
|
|
|
class DogValidatorWithOnCondition < Dog
|
|
|
|
before_validation :set_before_validation_marker, on: :create
|
|
|
|
after_validation :set_after_validation_marker, on: :create
|
|
|
|
|
2016-08-07 19:05:28 -04:00
|
|
|
def set_before_validation_marker; history << "before_validation_marker"; end
|
|
|
|
def set_after_validation_marker; history << "after_validation_marker" ; end
|
2015-01-11 15:25:53 -05:00
|
|
|
end
|
|
|
|
|
2017-12-15 20:55:52 -05:00
|
|
|
class DogValidatorWithOnMultipleCondition < Dog
|
|
|
|
before_validation :set_before_validation_marker_on_context_a, on: :context_a
|
|
|
|
before_validation :set_before_validation_marker_on_context_b, on: :context_b
|
|
|
|
after_validation :set_after_validation_marker_on_context_a, on: :context_a
|
|
|
|
after_validation :set_after_validation_marker_on_context_b, on: :context_b
|
|
|
|
|
|
|
|
def set_before_validation_marker_on_context_a; history << "before_validation_marker on context_a"; end
|
|
|
|
def set_before_validation_marker_on_context_b; history << "before_validation_marker on context_b"; end
|
|
|
|
def set_after_validation_marker_on_context_a; history << "after_validation_marker on context_a" ; end
|
|
|
|
def set_after_validation_marker_on_context_b; history << "after_validation_marker on context_b" ; end
|
|
|
|
end
|
|
|
|
|
2013-04-21 11:34:12 -04:00
|
|
|
class DogValidatorWithIfCondition < Dog
|
|
|
|
before_validation :set_before_validation_marker1, if: -> { true }
|
|
|
|
before_validation :set_before_validation_marker2, if: -> { false }
|
|
|
|
|
|
|
|
after_validation :set_after_validation_marker1, if: -> { true }
|
|
|
|
after_validation :set_after_validation_marker2, if: -> { false }
|
|
|
|
|
2016-08-07 19:05:28 -04:00
|
|
|
def set_before_validation_marker1; history << "before_validation_marker1"; end
|
|
|
|
def set_before_validation_marker2; history << "before_validation_marker2" ; end
|
2013-04-21 11:34:12 -04:00
|
|
|
|
2016-08-07 19:05:28 -04:00
|
|
|
def set_after_validation_marker1; history << "after_validation_marker1"; end
|
|
|
|
def set_after_validation_marker2; history << "after_validation_marker2" ; end
|
2013-04-21 11:34:12 -04:00
|
|
|
end
|
|
|
|
|
2019-08-02 00:25:13 -04:00
|
|
|
class CallbacksWithMethodNamesShouldBeCalled < ActiveModel::TestCase
|
2013-04-21 11:34:12 -04:00
|
|
|
def test_if_condition_is_respected_for_before_validation
|
|
|
|
d = DogValidatorWithIfCondition.new
|
|
|
|
d.valid?
|
|
|
|
assert_equal ["before_validation_marker1", "after_validation_marker1"], d.history
|
|
|
|
end
|
|
|
|
|
2015-01-11 15:25:53 -05:00
|
|
|
def test_on_condition_is_respected_for_validation_with_matching_context
|
|
|
|
d = DogValidatorWithOnCondition.new
|
|
|
|
d.valid?(:create)
|
|
|
|
assert_equal ["before_validation_marker", "after_validation_marker"], d.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_condition_is_respected_for_validation_without_matching_context
|
|
|
|
d = DogValidatorWithOnCondition.new
|
|
|
|
d.valid?(:save)
|
|
|
|
assert_equal [], d.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_condition_is_respected_for_validation_without_context
|
|
|
|
d = DogValidatorWithOnCondition.new
|
|
|
|
d.valid?
|
|
|
|
assert_equal [], d.history
|
|
|
|
end
|
|
|
|
|
2017-12-15 20:55:52 -05:00
|
|
|
def test_on_multiple_condition_is_respected_for_validation_with_matching_context
|
|
|
|
d = DogValidatorWithOnMultipleCondition.new
|
|
|
|
d.valid?(:context_a)
|
|
|
|
assert_equal ["before_validation_marker on context_a", "after_validation_marker on context_a"], d.history
|
|
|
|
|
|
|
|
d = DogValidatorWithOnMultipleCondition.new
|
|
|
|
d.valid?(:context_b)
|
|
|
|
assert_equal ["before_validation_marker on context_b", "after_validation_marker on context_b"], d.history
|
|
|
|
|
|
|
|
d = DogValidatorWithOnMultipleCondition.new
|
|
|
|
d.valid?([:context_a, :context_b])
|
|
|
|
assert_equal([
|
|
|
|
"before_validation_marker on context_a",
|
|
|
|
"before_validation_marker on context_b",
|
|
|
|
"after_validation_marker on context_a",
|
|
|
|
"after_validation_marker on context_b"
|
|
|
|
], d.history)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_multiple_condition_is_respected_for_validation_without_matching_context
|
|
|
|
d = DogValidatorWithOnMultipleCondition.new
|
|
|
|
d.valid?(:save)
|
|
|
|
assert_equal [], d.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_on_multiple_condition_is_respected_for_validation_without_context
|
|
|
|
d = DogValidatorWithOnMultipleCondition.new
|
|
|
|
d.valid?
|
|
|
|
assert_equal [], d.history
|
|
|
|
end
|
|
|
|
|
2010-06-16 11:30:37 -04:00
|
|
|
def test_before_validation_and_after_validation_callbacks_should_be_called
|
|
|
|
d = DogWithMethodCallbacks.new
|
|
|
|
d.valid?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["before_validation_marker", "after_validation_marker"], d.history
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_before_validation_and_after_validation_callbacks_should_be_called_with_proc
|
2012-07-24 04:01:41 -04:00
|
|
|
d = DogValidatorsAreProc.new
|
2010-06-16 11:30:37 -04:00
|
|
|
d.valid?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["before_validation_marker", "after_validation_marker"], d.history
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_before_validation_and_after_validation_callbacks_should_be_called_in_declared_order
|
|
|
|
d = DogWithTwoValidators.new
|
|
|
|
d.valid?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["before_validation_marker1", "before_validation_marker2"], d.history
|
2010-06-16 11:30:37 -04:00
|
|
|
end
|
|
|
|
|
2014-12-08 09:35:25 -05:00
|
|
|
def test_further_callbacks_should_not_be_called_if_before_validation_throws_abort
|
|
|
|
d = DogBeforeValidatorThrowingAbort.new
|
2010-06-16 11:30:37 -04:00
|
|
|
output = d.valid?
|
|
|
|
assert_equal [], d.history
|
|
|
|
assert_equal false, output
|
|
|
|
end
|
|
|
|
|
2017-02-07 10:13:15 -05:00
|
|
|
def test_further_callbacks_should_be_called_if_before_validation_returns_false
|
|
|
|
d = DogBeforeValidatorReturningFalse.new
|
|
|
|
output = d.valid?
|
|
|
|
assert_equal ["before_validation_marker2"], d.history
|
|
|
|
assert_equal true, output
|
2014-12-08 09:35:25 -05:00
|
|
|
end
|
|
|
|
|
2014-12-14 22:30:41 -05:00
|
|
|
def test_further_callbacks_should_be_called_if_after_validation_returns_false
|
|
|
|
d = DogAfterValidatorReturningFalse.new
|
|
|
|
d.valid?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["after_validation_marker"], d.history
|
2014-12-14 22:30:41 -05:00
|
|
|
end
|
|
|
|
|
2010-06-16 11:30:37 -04:00
|
|
|
def test_validation_test_should_be_done
|
|
|
|
d = DogWithMissingName.new
|
|
|
|
output = d.valid?
|
2016-08-06 12:38:23 -04:00
|
|
|
assert_equal ["before_validation_marker"], d.history
|
2010-06-16 11:30:37 -04:00
|
|
|
assert_equal false, output
|
|
|
|
end
|
|
|
|
end
|