mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #14526 from chancancode/fix_ar_equality
Ensure we are returning either `true` or `false` for `#==`
This commit is contained in:
commit
66b42a67af
3 changed files with 9 additions and 8 deletions
|
@ -299,7 +299,7 @@ module ActiveRecord
|
|||
def ==(comparison_object)
|
||||
super ||
|
||||
comparison_object.instance_of?(self.class) &&
|
||||
id &&
|
||||
!id.nil? &&
|
||||
comparison_object.id == id
|
||||
end
|
||||
alias :eql? :==
|
||||
|
|
|
@ -151,7 +151,7 @@ module ActiveRecord
|
|||
super ||
|
||||
other_aggregation.kind_of?(self.class) &&
|
||||
name == other_aggregation.name &&
|
||||
other_aggregation.options &&
|
||||
!other_aggregation.options.nil? &&
|
||||
active_record == other_aggregation.active_record
|
||||
end
|
||||
|
||||
|
|
|
@ -533,6 +533,7 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
|
||||
def test_equality_of_new_records
|
||||
assert_not_equal Topic.new, Topic.new
|
||||
assert_equal false, Topic.new == Topic.new
|
||||
end
|
||||
|
||||
def test_equality_of_destroyed_records
|
||||
|
@ -544,6 +545,12 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert_equal topic_2, topic_1
|
||||
end
|
||||
|
||||
def test_equality_with_blank_ids
|
||||
one = Subscriber.new(:id => '')
|
||||
two = Subscriber.new(:id => '')
|
||||
assert_equal one, two
|
||||
end
|
||||
|
||||
def test_hashing
|
||||
assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
|
||||
end
|
||||
|
@ -578,12 +585,6 @@ class BasicsTest < ActiveRecord::TestCase
|
|||
assert_equal nil, Topic.find_by_id(topic.id)
|
||||
end
|
||||
|
||||
def test_blank_ids
|
||||
one = Subscriber.new(:id => '')
|
||||
two = Subscriber.new(:id => '')
|
||||
assert_equal one, two
|
||||
end
|
||||
|
||||
def test_comparison_with_different_objects
|
||||
topic = Topic.create
|
||||
category = Category.create(:name => "comparison")
|
||||
|
|
Loading…
Reference in a new issue