Merge pull request #37088 from enriikke/fix-activemodel-error-frozen

Fix FrozenError on ActiveModel::Error clear and delete
This commit is contained in:
Rafael França 2019-12-17 19:39:28 -03:00 committed by GitHub
commit 886f397e98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -567,6 +567,12 @@ module ActiveModel
__setobj__ prepare_content
end
def delete(attribute)
ActiveSupport::Deprecation.warn("Calling `delete` to an ActiveModel::Errors messages hash is deprecated. Please call `ActiveModel::Errors#delete` instead.")
@errors.delete(attribute)
end
private
def prepare_content
content = @errors.to_hash
@ -597,6 +603,12 @@ module ActiveModel
__setobj__ @errors.messages_for(@attribute)
self
end
def clear
ActiveSupport::Deprecation.warn("Calling `clear` to an ActiveModel::Errors message array in order to delete all errors is deprecated. Please call `ActiveModel::Errors#delete` instead.")
@errors.delete(@attribute)
end
end
class DeprecationHandlingDetailsHash < SimpleDelegator

View File

@ -103,6 +103,15 @@ class ErrorsTest < ActiveModel::TestCase
assert_empty person.errors
end
test "clear errors by key" do
person = Person.new
person.validate!
assert_equal 1, person.errors.count
assert_deprecated { person.errors[:name].clear }
assert_empty person.errors
end
test "error access is indifferent" do
errors = ActiveModel::Errors.new(Person.new)
errors.add(:name, "omg")
@ -619,6 +628,15 @@ class ErrorsTest < ActiveModel::TestCase
)
end
test "messages delete (deprecated)" do
person = Person.new
person.validate!
assert_equal 1, person.errors.count
assert_deprecated { person.errors.messages.delete(:name) }
assert_empty person.errors
end
test "group_by_attribute" do
person = Person.new
error = person.errors.add(:name, :invalid, message: "is bad")