Merge pull request #31117 from renuo/fix_errors_added

fix bug on added? method
This commit is contained in:
Rafael França 2017-11-13 16:13:54 -05:00 committed by GitHub
commit 233d6a2b56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

@ -322,10 +322,14 @@ def add(attribute, message = :invalid, options = {})
# person.errors.added? :name, :too_long # => false
# person.errors.added? :name, "is too long" # => false
def added?(attribute, message = :invalid, options = {})
if message.is_a? Symbol
self.details[attribute].map { |e| e[:error] }.include? message
else
message = message.call if message.respond_to?(:call)
message = normalize_message(attribute, message, options)
self[attribute].include? message
end
end
# Returns all the full error messages in an array.
#

@ -223,6 +223,13 @@ def test_no_key
assert !person.errors.added?(:name)
end
test "added? returns false when checking for an error by symbol and a different error with same message is present" do
I18n.backend.store_translations("en", errors: { attributes: { name: { wrong: "is wrong", used: "is wrong" } } })
person = Person.new
person.errors.add(:name, :wrong)
assert !person.errors.added?(:name, :used)
end
test "size calculates the number of error messages" do
person = Person.new
person.errors.add(:name, "cannot be blank")