Skip to content

Commit

Permalink
Merge pull request #49677 from DmitryPogrebnoy/49670
Browse files Browse the repository at this point in the history
Make `==(other)` method of AttributeSet safe #49670
  • Loading branch information
kamipo committed Oct 20, 2023
2 parents 7c58911 + 3ed0229 commit 5dac2c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_set.rb
Expand Up @@ -104,7 +104,7 @@ def reverse_merge!(target_attributes)
end

def ==(other)
attributes == other.attributes
other.is_a?(AttributeSet) && attributes == other.send(:attributes)
end

protected
Expand Down
8 changes: 8 additions & 0 deletions activemodel/test/cases/attribute_set_test.rb
Expand Up @@ -279,6 +279,14 @@ def builder.build_from_database(values = {}, additional_types = {})
assert_not_equal attributes2, attributes3
end

test "==(other) is safe to use with any instance" do
attribute_set = AttributeSet.new({})

assert_equal false, attribute_set == nil
assert_equal false, attribute_set == 1
assert_equal true, attribute_set == attribute_set
end

private
def attributes_with_uninitialized_key
builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::Float.new)
Expand Down

0 comments on commit 5dac2c8

Please sign in to comment.