aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-11-13 16:13:54 -0500
committerGitHub <noreply@github.com>2017-11-13 16:13:54 -0500
commit233d6a2b563e34156c5fc2317c19dc1823447c18 (patch)
treea1e0e86b98b7cdcf5ecb748a2fcf38ffa29afc50 /activemodel
parent68fe6b08ee72cc47263e0d2c9ff07f75c4b42761 (diff)
parent15cb4efadb61a8813967d3c25f4adfc9a918a0c0 (diff)
downloadrails-233d6a2b563e34156c5fc2317c19dc1823447c18.tar.gz
rails-233d6a2b563e34156c5fc2317c19dc1823447c18.tar.bz2
rails-233d6a2b563e34156c5fc2317c19dc1823447c18.zip
Merge pull request #31117 from renuo/fix_errors_added
fix bug on added? method
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/errors.rb10
-rw-r--r--activemodel/test/cases/errors_test.rb7
2 files changed, 14 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index 971bdd08b1..275e3f1313 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -322,9 +322,13 @@ module ActiveModel
# person.errors.added? :name, :too_long # => false
# person.errors.added? :name, "is too long" # => false
def added?(attribute, message = :invalid, options = {})
- message = message.call if message.respond_to?(:call)
- message = normalize_message(attribute, message, options)
- self[attribute].include? message
+ 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.
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb
index ab18af0de1..d5c282b620 100644
--- a/activemodel/test/cases/errors_test.rb
+++ b/activemodel/test/cases/errors_test.rb
@@ -223,6 +223,13 @@ class ErrorsTest < ActiveModel::TestCase
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")