mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #29714 from jahfer/implement-errors-merge
Add ActiveModel::Errors#merge!
This commit is contained in:
commit
98f8f81879
3 changed files with 28 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Add method `#merge!` for `ActiveModel::Errors`.
|
||||||
|
|
||||||
|
*Jahfer Husain*
|
||||||
|
|
||||||
* Fix regression in numericality validator when comparing Decimal and Float input
|
* Fix regression in numericality validator when comparing Decimal and Float input
|
||||||
values with more scale than the schema.
|
values with more scale than the schema.
|
||||||
|
|
||||||
|
|
|
@ -93,6 +93,18 @@ module ActiveModel
|
||||||
@details = other.details.dup
|
@details = other.details.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Merges the errors from <tt>other</tt>.
|
||||||
|
#
|
||||||
|
# other - The ActiveModel::Errors instance.
|
||||||
|
#
|
||||||
|
# Examples
|
||||||
|
#
|
||||||
|
# person.errors.merge!(other)
|
||||||
|
def merge!(other)
|
||||||
|
@messages.merge!(other.messages) { |_, ary1, ary2| ary1 + ary2 }
|
||||||
|
@details.merge!(other.details) { |_, ary1, ary2| ary1 + ary2 }
|
||||||
|
end
|
||||||
|
|
||||||
# Clear the error messages.
|
# Clear the error messages.
|
||||||
#
|
#
|
||||||
# person.errors.full_messages # => ["name cannot be nil"]
|
# person.errors.full_messages # => ["name cannot be nil"]
|
||||||
|
|
|
@ -375,6 +375,18 @@ class ErrorsTest < ActiveModel::TestCase
|
||||||
assert_equal [:name], person.errors.details.keys
|
assert_equal [:name], person.errors.details.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "merge errors" do
|
||||||
|
errors = ActiveModel::Errors.new(Person.new)
|
||||||
|
errors.add(:name, :invalid)
|
||||||
|
|
||||||
|
person = Person.new
|
||||||
|
person.errors.add(:name, :blank)
|
||||||
|
person.errors.merge!(errors)
|
||||||
|
|
||||||
|
assert_equal({ name: ["can't be blank", "is invalid"] }, person.errors.messages)
|
||||||
|
assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details)
|
||||||
|
end
|
||||||
|
|
||||||
test "errors are marshalable" do
|
test "errors are marshalable" do
|
||||||
errors = ActiveModel::Errors.new(Person.new)
|
errors = ActiveModel::Errors.new(Person.new)
|
||||||
errors.add(:name, :invalid)
|
errors.add(:name, :invalid)
|
||||||
|
|
Loading…
Reference in a new issue