Skip to content

Commit 5dac2c8

Browse files
authored
Merge pull request rails#49677 from DmitryPogrebnoy/49670
Make `==(other)` method of AttributeSet safe rails#49670
2 parents 7c58911 + 3ed0229 commit 5dac2c8

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

activemodel/lib/active_model/attribute_set.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def reverse_merge!(target_attributes)
104104
end
105105

106106
def ==(other)
107-
attributes == other.attributes
107+
other.is_a?(AttributeSet) && attributes == other.send(:attributes)
108108
end
109109

110110
protected

activemodel/test/cases/attribute_set_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ def builder.build_from_database(values = {}, additional_types = {})
279279
assert_not_equal attributes2, attributes3
280280
end
281281

282+
test "==(other) is safe to use with any instance" do
283+
attribute_set = AttributeSet.new({})
284+
285+
assert_equal false, attribute_set == nil
286+
assert_equal false, attribute_set == 1
287+
assert_equal true, attribute_set == attribute_set
288+
end
289+
282290
private
283291
def attributes_with_uninitialized_key
284292
builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new)

0 commit comments

Comments
 (0)