Skip to content

Commit

Permalink
Merge pull request #33361 from jhubert/bugfix/fix-added-string-attrib…
Browse files Browse the repository at this point in the history
…utes

Fix regression in use of string attribute in the added? method
  • Loading branch information
eileencodes committed Jul 14, 2018
2 parents a8d63c2 + a199181 commit 05bef14
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def add(attribute, message = :invalid, options = {})
# 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
self.details[attribute.to_sym].map { |e| e[:error] }.include? message
else
message = message.call if message.respond_to?(:call)
message = normalize_message(attribute, message, options)
Expand Down
6 changes: 6 additions & 0 deletions activemodel/test/cases/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ def test_no_key
assert person.errors.added?(:name, :blank)
end

test "added? returns true when string attribute is used with a symbol message" do
person = Person.new
person.errors.add(:name, :blank)
assert person.errors.added?("name", :blank)
end

test "added? handles proc messages" do
person = Person.new
message = Proc.new { "cannot be blank" }
Expand Down

0 comments on commit 05bef14

Please sign in to comment.