Skip to content

Commit 8a821e4

Browse files
authored
MONGOID-5257 make === equivalent to == (#5172)
* MONGOID-5257 make === equivalent to == * MONGOID-5257 move class === to equality * MONGOID-5257 test all === tests with feature flag turned on and off
1 parent 3a5329f commit 8a821e4

File tree

3 files changed

+101
-94
lines changed

3 files changed

+101
-94
lines changed

lib/mongoid/document.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,6 @@ def as_attributes
272272

273273
module ClassMethods
274274

275-
# Performs class equality checking.
276-
#
277-
# @example Compare the classes.
278-
# document === other
279-
#
280-
# @param [ Document, Object ] other The other object to compare with.
281-
#
282-
# @return [ true, false ] True if the classes are equal, false if not.
283-
def ===(other)
284-
if Mongoid.legacy_triple_equals
285-
other.class == Class ? self <= other : other.is_a?(self)
286-
else
287-
other.is_a?(self)
288-
end
289-
end
290-
291275
# Instantiate a new object, only when loaded from the database or when
292276
# the attributes have already been typecast.
293277
#

lib/mongoid/equality.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module Mongoid
44

55
# This module contains the behavior of Mongoid's clone/dup of documents.
66
module Equality
7+
# Leave the current contents of this module outside of InstanceMethods
8+
# to prevent cherry picking conflicts. For now...
9+
extend ActiveSupport::Concern
710

811
# Default comparison is via the string version of the id.
912
#
@@ -40,7 +43,11 @@ def ==(other)
4043
#
4144
# @return [ true, false ] True if the classes are equal, false if not.
4245
def ===(other)
43-
other.class == Class ? self.class === other : self == other
46+
if Mongoid.legacy_triple_equals
47+
super
48+
else
49+
other.class == Class ? self.class === other : self == other
50+
end
4451
end
4552

4653
# Delegates to ==. Used when needing checks in hashes.
@@ -54,5 +61,23 @@ def ===(other)
5461
def eql?(other)
5562
self == (other)
5663
end
64+
65+
module ClassMethods
66+
# Performs class equality checking.
67+
#
68+
# @example Compare the classes.
69+
# document === other
70+
#
71+
# @param [ Document, Object ] other The other object to compare with.
72+
#
73+
# @return [ true, false ] True if the classes are equal, false if not.
74+
def ===(other)
75+
if Mongoid.legacy_triple_equals
76+
other.is_a?(self)
77+
else
78+
other.class == Class ? self <= other : other.is_a?(self)
79+
end
80+
end
81+
end
5782
end
5883
end

spec/mongoid/equality_spec.rb

Lines changed: 75 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -85,151 +85,123 @@
8585

8686
describe ".===" do
8787

88-
context "when comparable is an instance of this document" do
88+
context "when legacy_triple_equals is set" do
89+
config_override :legacy_triple_equals, true
90+
91+
context "when comparable is an instance of this document" do
8992

90-
with_config_values :legacy_triple_equals, false, true do
9193
it "returns true" do
9294
expect(klass === person).to be true
9395
end
9496
end
95-
end
9697

97-
context "when comparable is a relation of this document" do
98+
context "when comparable is a relation of this document" do
9899

99-
let(:relation) do
100-
Post.new(person: person).person
101-
end
100+
let(:relation) do
101+
Post.new(person: person).person
102+
end
102103

103-
with_config_values :legacy_triple_equals, false, true do
104104
it "returns true" do
105105
expect(klass === relation).to be true
106106
end
107107
end
108-
end
109108

110-
context "when comparable is the same class" do
111-
112-
context "when legacy_triple_equals is not set" do
113-
config_override :legacy_triple_equals, false
109+
context "when comparable is the same class" do
114110

115111
it "returns false" do
116112
expect(klass === Person).to be false
117113
end
118114
end
119115

120-
context "when legacy_triple_equals is set" do
121-
config_override :legacy_triple_equals, true
122-
123-
it "returns true" do
124-
expect(klass === Person).to be true
125-
end
126-
end
127-
end
128-
129-
context "when the comparable is a subclass" do
116+
context "when the comparable is a subclass" do
130117

131-
with_config_values :legacy_triple_equals, false, true do
132118
it "returns false" do
133119
expect(Person === Doctor).to be false
134120
end
135121
end
136-
end
137122

138-
context "when the comparable is an instance of a subclass" do
123+
context "when the comparable is an instance of a subclass" do
139124

140-
with_config_values :legacy_triple_equals, false, true do
141125
it "returns true" do
142126
expect(Person === Doctor.new).to be true
143127
end
144128
end
145129
end
146130

147-
context "when comparing to a class" do
148-
149-
context "when legacy_triple_equals is not set" do
150-
config_override :legacy_triple_equals, false
131+
context "when legacy_triple_equals is not set" do
132+
config_override :legacy_triple_equals, false
151133

152-
context "when the class is the same" do
134+
context "when comparable is an instance of this document" do
153135

154-
it "returns false" do
155-
expect(Person === Person).to be false
156-
end
136+
it "returns true" do
137+
expect(klass === person).to be true
157138
end
139+
end
158140

159-
context "when the class is a subclass" do
141+
context "when comparable is a relation of this document" do
160142

161-
it "returns false" do
162-
expect(Person === Doctor).to be false
163-
end
143+
let(:relation) do
144+
Post.new(person: person).person
164145
end
165146

166-
context "when the class is a superclass" do
167-
168-
it "returns false" do
169-
expect(Doctor === Person).to be false
170-
end
147+
it "returns true" do
148+
expect(klass === relation).to be true
171149
end
172150
end
173151

174-
context "when legacy_triple_equals is set" do
175-
config_override :legacy_triple_equals, true
152+
context "when comparable is the same class" do
176153

177-
context "when the class is the same" do
178-
179-
it "returns true" do
180-
expect(Person === Person).to be true
181-
end
154+
it "returns true" do
155+
expect(klass === Person).to be true
182156
end
157+
end
183158

184-
context "when the class is a subclass" do
159+
context "when the comparable is a subclass" do
185160

186-
it "returns false" do
187-
expect(Person === Doctor).to be false
188-
end
161+
it "returns false" do
162+
expect(Person === Doctor).to be false
189163
end
164+
end
190165

191-
context "when the class is a superclass" do
166+
context "when the comparable is an instance of a subclass" do
192167

193-
it "returns true" do
194-
expect(Doctor === Person).to be true
195-
end
168+
it "returns true" do
169+
expect(Person === Doctor.new).to be true
196170
end
197171
end
198172
end
199173
end
200174

201175
describe "#===" do
202176

203-
context "when comparable is the same type" do
177+
context "when legacy_triple_equals is set" do
178+
config_override :legacy_triple_equals, true
204179

205-
context "when the instance is different" do
180+
context "when comparable is the same type" do
206181

207-
it "returns false" do
208-
expect(person === Person.new).to be false
182+
context "when the instance is different" do
183+
184+
it "returns false" do
185+
expect(person === Person.new).to be false
186+
end
209187
end
210-
end
211188

212-
context "when the instance is the same" do
189+
context "when the instance is the same" do
213190

214-
it "returns true" do
215-
expect(person === person).to be true
191+
it "returns true" do
192+
expect(person === person).to be true
193+
end
216194
end
217195
end
218-
end
219196

220-
context "when the comparable is a subclass" do
197+
context "when the comparable is a subclass" do
221198

222-
with_config_values :legacy_triple_equals, false, true do
223199
it "returns false" do
224200
expect(person === Doctor.new).to be false
225201
end
226202
end
227-
end
228-
229-
context "when comparing to a class" do
230203

231-
context "when legacy_triple_equals is not set" do
232-
config_override :legacy_triple_equals, false
204+
context "when comparing to a class" do
233205

234206
context "when the class is the same" do
235207

@@ -252,10 +224,36 @@
252224
end
253225
end
254226
end
227+
end
228+
229+
context "when legacy_triple_equals is not set" do
230+
config_override :legacy_triple_equals, false
231+
232+
context "when comparable is the same type" do
233+
234+
context "when the instance is different" do
235+
236+
it "returns false" do
237+
expect(person === Person.new).to be false
238+
end
239+
end
255240

256-
context "when legacy_triple_equals is set" do
257-
config_override :legacy_triple_equals, true
241+
context "when the instance is the same" do
242+
243+
it "returns true" do
244+
expect(person === person).to be true
245+
end
246+
end
247+
end
248+
249+
context "when the comparable is a subclass" do
250+
251+
it "returns false" do
252+
expect(person === Doctor.new).to be false
253+
end
254+
end
258255

256+
context "when comparing to a class" do
259257
context "when the class is the same" do
260258

261259
it "returns true" do

0 commit comments

Comments
 (0)