Skip to content

Commit 5b2b836

Browse files
Merge branch 'master' into 5780-toggle-negation-state
2 parents 3b657e1 + 6be82fb commit 5b2b836

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/mongoid/equality.rb

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Equality
1818
#
1919
# @return [ Integer ] -1, 0, 1.
2020
def <=>(other)
21+
return super unless other.is_a?(Mongoid::Equality)
2122
attributes["_id"].to_s <=> other.attributes["_id"].to_s
2223
end
2324

lib/mongoid/traversable.rb

+24-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ module Mongoid
88
# around traversing the document graph.
99
module Traversable
1010
extend ActiveSupport::Concern
11+
# This code is extracted from ActiveSupport so that we do not depend on
12+
# their private API that may change at any time.
13+
# This code should be reviewed and maybe removed when implementing
14+
# https://jira.mongodb.org/browse/MONGOID-5832
15+
class << self
16+
# @api private
17+
def __redefine(owner, name, value)
18+
if owner.singleton_class?
19+
owner.redefine_method(name) { value }
20+
owner.send(:public, name)
21+
end
22+
owner.redefine_singleton_method(name) { value }
23+
owner.singleton_class.send(:public, name)
24+
owner.redefine_singleton_method("#{name}=") do |new_value|
25+
if owner.equal?(self)
26+
value = new_value
27+
else
28+
::Mongoid::Traversable.redefine(self, name, new_value)
29+
end
30+
end
31+
owner.singleton_class.send(:public, "#{name}=")
32+
end
33+
end
1134

1235
# Class-level methods for the Traversable behavior.
1336
module ClassMethods
@@ -105,11 +128,7 @@ def discriminator_key=(value)
105128
if value
106129
Mongoid::Fields::Validators::Macro.validate_field_name(self, value)
107130
value = value.to_s
108-
if defined?(::ActiveSupport::ClassAttribute)
109-
::ActiveSupport::ClassAttribute.redefine(self, 'discriminator_key', value)
110-
else
111-
super
112-
end
131+
::Mongoid::Traversable.__redefine(self, 'discriminator_key', value)
113132
else
114133
# When discriminator key is set to nil, replace the class's definition
115134
# of the discriminator key reader (provided by class_attribute earlier)

spec/mongoid/equality_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
it "compares based on the document id" do
190190
expect(first <=> second).to eq(-1)
191191
end
192+
193+
it "doesn't break if one isn't a document" do
194+
expect do
195+
first <=> "Foo"
196+
end.to_not raise_error
197+
end
192198
end
193199

194200
describe "#eql?" do

0 commit comments

Comments
 (0)