From a4deb637984e4aff7c679f4c5d1eebc789774105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 13 Apr 2020 19:07:59 -0400 Subject: [PATCH] No need to deprecate Errors#first This deprecation is useless since the result is still an Error object and there is no way to fix the code to remove the deprecation. Let's just accept as a breaking change. --- activemodel/CHANGELOG.md | 6 ++---- activemodel/lib/active_model/errors.rb | 19 ------------------- activemodel/test/cases/errors_test.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index f7e2022231..5590357895 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -37,15 +37,13 @@ extra parameters, found in the original `details` hash. The change tries its best at maintaining backward compatibility, however - some edge cases won’t be covered, mainly related to manipulating - `errors.messages` and `errors.details` hashes directly. Moving forward, + some edge cases won’t be covered, like `errors#first` will return `ActiveModel::Error` and manipulating + `errors.messages` and `errors.details` hashes directly will have no effect. Moving forward, please convert those direct manipulations to use provided API methods instead. The list of deprecated methods and their planned future behavioral changes at the next major release are: * `errors#slice!` will be removed. - * `errors#first` will return Error object instead. - * `errors#last` will return Error object instead. * `errors#each` with the `key, value` two-arguments block will stop working, while the `error` single-argument block would return `Error` object. * `errors#values` will be removed. * `errors#keys` will be removed. diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 4d97efb72a..9df0c1c472 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -208,16 +208,6 @@ module ActiveModel DeprecationHandlingMessageArray.new(messages_for(attribute), self, attribute) end - def first - deprecation_index_access_warning(:first) - super - end - - def last - deprecation_index_access_warning(:last) - super - end - # Iterates through each error object. # # person.errors.add(:name, :too_short, count: 2) @@ -585,15 +575,6 @@ module ActiveModel def deprecation_rename_warning(old_method_name, new_method_name) ActiveSupport::Deprecation.warn("ActiveModel::Errors##{old_method_name} is deprecated. Please call ##{new_method_name} instead.") end - - def deprecation_index_access_warning(method_name, alternative_message) - message = +"ActiveModel::Errors##{method_name} is deprecated. In the next release it would return `Error` object instead." - if alternative_message - message << "\n\nTo achieve the same use:\n\n " - message << alternative_message - end - ActiveSupport::Deprecation.warn(message) - end end class DeprecationHandlingMessageHash < SimpleDelegator diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 5519139d85..f88bd85937 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -63,6 +63,14 @@ class ErrorsTest < ActiveModel::TestCase } end + def test_first + errors = ActiveModel::Errors.new(Person.new) + errors.add(:name, :blank) + + error = errors.first + assert_kind_of ActiveModel::Error, error + end + def test_dup errors = ActiveModel::Errors.new(Person.new) errors.add(:name)