aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorRonan Limon Duparcmeur <ronan@2-45.pm>2018-11-09 16:45:18 +0100
committerRonan Limon Duparcmeur <ronan@2-45.pm>2018-11-13 09:00:20 +0100
commit13b77fa1cb49495f1da79fec6f06475d5dd29af7 (patch)
tree04712f1c5486e7bf2574f27b36e0e06e01028553 /activemodel
parente899e228e4fb86ed2a63d19c9137059952e954f0 (diff)
downloadrails-13b77fa1cb49495f1da79fec6f06475d5dd29af7.tar.gz
rails-13b77fa1cb49495f1da79fec6f06475d5dd29af7.tar.bz2
rails-13b77fa1cb49495f1da79fec6f06475d5dd29af7.zip
Fix ignored options in the `#added?` method
Fixes #34416
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/errors.rb6
-rw-r--r--activemodel/test/cases/errors_test.rb10
2 files changed, 13 insertions, 3 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index af94d52d45..9de6b609a3 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -327,11 +327,11 @@ 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)
+
if message.is_a? Symbol
- self.details[attribute.to_sym].map { |e| e[:error] }.include? message
+ details[attribute.to_sym].include? normalize_detail(message, options)
else
- message = message.call if message.respond_to?(:call)
- message = normalize_message(attribute, message, options)
self[attribute].include? message
end
end
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb
index 41ff6443fe..185b5a24ae 100644
--- a/activemodel/test/cases/errors_test.rb
+++ b/activemodel/test/cases/errors_test.rb
@@ -228,6 +228,16 @@ class ErrorsTest < ActiveModel::TestCase
assert_not person.errors.added?(:name)
end
+ test "added? returns false when checking for an error with an incorrect or missing option" do
+ person = Person.new
+ person.errors.add :name, :too_long, count: 25
+
+ assert person.errors.added? :name, :too_long, count: 25
+ assert_not person.errors.added? :name, :too_long, count: 24
+ assert_not person.errors.added? :name, :too_long
+ assert_not person.errors.added? :name, "is too long"
+ 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