From 884c97fad02ccc061a7d34058709b32f6f6a6a14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 16 Nov 2021 00:50:35 +0000 Subject: [PATCH] Remove deprecated support concat errors to `ActiveModel::Errors#messages` --- .../test/template/active_model_helper_test.rb | 10 +++++----- .../test/template/form_options_helper_test.rb | 2 +- activemodel/CHANGELOG.md | 4 ++++ activemodel/lib/active_model/errors.rb | 8 -------- activemodel/test/cases/errors_test.rb | 2 +- activemodel/test/cases/validations_test.rb | 2 +- .../has_many_through_associations_test.rb | 18 +++++++++--------- guides/source/7_0_release_notes.md | 2 ++ 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/actionview/test/template/active_model_helper_test.rb b/actionview/test/template/active_model_helper_test.rb index fd58c935da..742ffcbd7e 100644 --- a/actionview/test/template/active_model_helper_test.rb +++ b/actionview/test/template/active_model_helper_test.rb @@ -20,11 +20,11 @@ class ActiveModelHelperTest < ActionView::TestCase super @post = Post.new - assert_deprecated { @post.errors[:author_name] << "can't be empty" } - assert_deprecated { @post.errors[:body] << "foo" } - assert_deprecated { @post.errors[:category] << "must exist" } - assert_deprecated { @post.errors[:published] << "must be accepted" } - assert_deprecated { @post.errors[:updated_at] << "bar" } + @post.errors.add(:author_name, "can't be empty") + @post.errors.add(:body, "foo") + @post.errors.add(:category, "must exist") + @post.errors.add(:published, "must be accepted") + @post.errors.add(:updated_at, "bar") @post.author_name = "" @post.body = "Back to the hill and over it again!" diff --git a/actionview/test/template/form_options_helper_test.rb b/actionview/test/template/form_options_helper_test.rb index af9fee7b55..979d476a74 100644 --- a/actionview/test/template/form_options_helper_test.rb +++ b/actionview/test/template/form_options_helper_test.rb @@ -1341,7 +1341,7 @@ class FormOptionsHelperTest < ActionView::TestCase def test_time_zone_select_with_priority_zones_and_errors @firm = Firm.new("D") @firm.extend ActiveModel::Validations - assert_deprecated { @firm.errors[:time_zone] << "invalid" } + @firm.errors.add(:time_zone, "invalid") zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] html = time_zone_select("firm", "time_zone", zones) assert_dom_equal "
" \ diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index e8488ab595..10c0a71d50 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,7 @@ +* Remove deprecated support concat errors to `ActiveModel::Errors#messages`. + + *Rafael Mendonça França* + * Remove deprecated `ActiveModel::Errors#to_xml`. *Rafael Mendonça França* diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index b0f270e39f..a8ce1f32bf 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -542,14 +542,6 @@ module ActiveModel super(content.freeze) end - def <<(message) - ActiveSupport::Deprecation.warn("Calling `<<` to an ActiveModel::Errors message array in order to add an error is deprecated. Please call `ActiveModel::Errors#add` instead.") - - @errors.add(@attribute, message) - __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.") diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 75e3ef639a..b5bb87753c 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -39,7 +39,7 @@ class ErrorsTest < ActiveModel::TestCase def test_include? errors = ActiveModel::Errors.new(Person.new) - assert_deprecated { errors[:foo] << "omg" } + errors.add(:foo, "omg") assert_includes errors, :foo, "errors should include :foo" assert_includes errors, "foo", "errors should include 'foo' as :foo" end diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index f53399ff55..da839fae78 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -64,7 +64,7 @@ class ValidationsTest < ActiveModel::TestCase def test_errors_on_nested_attributes_expands_name t = Topic.new - assert_deprecated { t.errors["replies.name"] << "can't be blank" } + t.errors.add("replies.name", "can't be blank") assert_equal ["Replies name can't be blank"], t.errors.full_messages end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index a8f1d5a885..ec8df88719 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -759,7 +759,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_push_with_invalid_join_record repair_validations(Contract) do - Contract.validate { |r| r.errors[:base] << "Invalid Contract" } + Contract.validate { |r| r.errors.add(:base, "Invalid Contract") } firm = companies(:first_firm) lifo = Developer.new(name: "lifo") @@ -1213,8 +1213,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_create_should_not_raise_exception_when_join_record_has_errors repair_validations(Categorization) do - Categorization.validate { |r| r.errors[:base] << "Invalid Categorization" } - assert_deprecated { Category.create(name: "Fishing", authors: [Author.first]) } + Categorization.validate { |r| r.errors.add(:base, "Invalid Categorization") } + Category.create(name: "Fishing", authors: [Author.first]) end end @@ -1225,28 +1225,28 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_create_bang_should_raise_exception_when_join_record_has_errors repair_validations(Categorization) do - Categorization.validate { |r| r.errors[:base] << "Invalid Categorization" } + Categorization.validate { |r| r.errors.add(:base, "Invalid Categorization") } assert_raises(ActiveRecord::RecordInvalid) do - assert_deprecated { Category.create!(name: "Fishing", authors: [Author.first]) } + Category.create!(name: "Fishing", authors: [Author.first]) end end end def test_save_bang_should_raise_exception_when_join_record_has_errors repair_validations(Categorization) do - Categorization.validate { |r| r.errors[:base] << "Invalid Categorization" } + Categorization.validate { |r| r.errors.add(:base, "Invalid Categorization") } c = Category.new(name: "Fishing", authors: [Author.first]) assert_raises(ActiveRecord::RecordInvalid) do - assert_deprecated { c.save! } + c.save! end end end def test_save_returns_falsy_when_join_record_has_errors repair_validations(Categorization) do - Categorization.validate { |r| r.errors[:base] << "Invalid Categorization" } + Categorization.validate { |r| r.errors.add(:base, "Invalid Categorization") } c = Category.new(name: "Fishing", authors: [Author.first]) - assert_deprecated { assert_not c.save } + assert_not c.save end end diff --git a/guides/source/7_0_release_notes.md b/guides/source/7_0_release_notes.md index 43017a5f59..c6a1049a8d 100644 --- a/guides/source/7_0_release_notes.md +++ b/guides/source/7_0_release_notes.md @@ -134,6 +134,8 @@ Please refer to the [Changelog][active-model] for detailed changes. * Remove deprecated `ActiveModel::Errors#to_xml`. +* Remove deprecated support concat errors to `ActiveModel::Errors#messages`. + ### Deprecations ### Notable changes