提交 4a19b3de 编写于 作者: G Godfrey Chan

Pass through the `prepend` option to `AS::Callback`

I'm not sure what's the use case for this, but apparently it broke some apps.
Since it was not the intended result from #16210 I fixed it to not raise an
exception anymore. However, I didn't add documentation for it because I don't
know if this should be officially supported without knowing how it's meant to
be used.

In general, validations should be side-effect-free (other than adding to the
error message to `@errors`). Order-dependent validations seems like a bad idea.

Fixes #18002
上级 83534e56
......@@ -87,7 +87,7 @@ def validates_each(*attr_names, &block)
validates_with BlockValidator, _merge_attributes(attr_names), &block
end
VALID_OPTIONS_FOR_VALIDATE = [:on, :if, :unless].freeze
VALID_OPTIONS_FOR_VALIDATE = [:on, :if, :unless, :prepend].freeze
# Adds a validation method or block to the class. This is useful when
# overriding the +validate+ instance method becomes too unwieldy and
......
......@@ -172,7 +172,43 @@ def test_invalid_options_to_validate
Topic.validate :title, presence: true
end
message = 'Unknown key: :presence. Valid keys are: :on, :if, :unless. Perhaps you meant to call `validates` instead of `validate`?'
assert_equal message, error.message
assert_includes error.message, "Unknown key: :presence"
assert_includes error.message, "Perhaps you meant to call `validates` instead of `validate`?"
end
def test_callback_options_to_validate
klass = Class.new(Topic) do
attr_reader :call_sequence
def initialize(*)
super
@call_sequence = []
end
private
def validator_a
@call_sequence << :a
end
def validator_b
@call_sequence << :b
end
def validator_c
@call_sequence << :c
end
end
assert_nothing_raised do
klass.validate :validator_a, if: ->{ true }
klass.validate :validator_b, prepend: true
klass.validate :validator_c, unless: ->{ true }
end
t = klass.new
assert_predicate t, :valid?
assert_equal [:b, :a], t.call_sequence
end
def test_errors_conversions
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册