From 1523838567606327fabf08c4b5aeacbca7b2201c Mon Sep 17 00:00:00 2001 From: lulalala Date: Wed, 21 Jul 2021 20:56:28 +0800 Subject: [PATCH] Slimmer ActiveModel::Errors#inspect Only show @errors array and hide @base --- activemodel/CHANGELOG.md | 3 +++ activemodel/lib/active_model/errors.rb | 6 ++++++ activemodel/test/cases/errors_test.rb | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 1efa642c54..33b3b30ff9 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -14,5 +14,8 @@ *Lukas Pokorny* +* Make ActiveModel::Errors#inspect slimmer for readability + + *lulalala* Please check [6-1-stable](https://github.com/rails/rails/blob/6-1-stable/activemodel/CHANGELOG.md) for previous changes. diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 10a8100601..e249889c23 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -571,6 +571,12 @@ module ActiveModel add_from_legacy_details_hash(data["details"]) if data.key?("details") end + def inspect # :nodoc: + inspection = @errors.inspect + + "#<#{self.class.name} #{inspection}>" + end + private def normalize_arguments(attribute, type, **options) # Evaluate proc first diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index f88bd85937..20844f8c5f 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -880,4 +880,11 @@ class ErrorsTest < ActiveModel::TestCase assert_equal({}, errors.messages) assert_equal({}, errors.details) end + + test "inspect" do + errors = ActiveModel::Errors.new(Person.new) + errors.add(:base) + + assert_equal(%(#), errors.inspect) + end end