aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activemodel/lib/active_model/error.rb15
-rw-r--r--activemodel/lib/active_model/errors.rb2
-rw-r--r--activemodel/test/cases/error_test.rb20
-rw-r--r--activerecord/lib/active_record/autosave_association.rb4
4 files changed, 37 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/error.rb b/activemodel/lib/active_model/error.rb
index b1912f2604..9731fa74df 100644
--- a/activemodel/lib/active_model/error.rb
+++ b/activemodel/lib/active_model/error.rb
@@ -62,5 +62,20 @@ module ActiveModel
full_message == Error.new(@base, attribute, type, **options).full_message
end
+
+ def ==(other)
+ attributes_for_hash == other.attributes_for_hash
+ end
+ alias eql? ==
+
+ def hash
+ attributes_for_hash.hash
+ end
+
+ protected
+
+ def attributes_for_hash
+ [@base, @attribute, @raw_type, @options]
+ end
end
end
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb
index bb64bc6264..2d559e06d4 100644
--- a/activemodel/lib/active_model/errors.rb
+++ b/activemodel/lib/active_model/errors.rb
@@ -64,7 +64,7 @@ module ActiveModel
include Enumerable
extend Forwardable
- def_delegators :@errors, :size, :clear, :blank?, :empty?
+ def_delegators :@errors, :size, :clear, :blank?, :empty?, :uniq!
# TODO: forward all enumerable methods after `each` deprecation is removed.
def_delegators :@errors, :count
diff --git a/activemodel/test/cases/error_test.rb b/activemodel/test/cases/error_test.rb
index f557d50c32..d1193d123f 100644
--- a/activemodel/test/cases/error_test.rb
+++ b/activemodel/test/cases/error_test.rb
@@ -170,4 +170,24 @@ class ErrorTest < ActiveModel::TestCase
assert_equal "name can't be blank", error.full_message
}
end
+
+ test "equality by base attribute, type and options" do
+ person = Person.new
+
+ e1 = ActiveModel::Error.new(person, :name, foo: :bar)
+ e2 = ActiveModel::Error.new(person, :name, foo: :bar)
+ e2.instance_variable_set(:@_humanized_attribute, "Name")
+
+ assert_equal(e1, e2)
+ end
+
+ test "inequality" do
+ person = Person.new
+ error = ActiveModel::Error.new(person, :name, foo: :bar)
+
+ assert error != ActiveModel::Error.new(person, :name, foo: :baz)
+ assert error != ActiveModel::Error.new(person, :name)
+ assert error != ActiveModel::Error.new(person, :title, foo: :bar)
+ assert error != ActiveModel::Error.new(Person.new, :name, foo: :bar)
+ end
end
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 453c6e4c8c..e05b7cd6d2 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -491,9 +491,7 @@ module ActiveRecord
end
def _ensure_no_duplicate_errors
- errors.messages.each_key do |attribute|
- errors[attribute].uniq!
- end
+ errors.uniq!
end
end
end