Skip to content

Commit ccb519c

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5663 [Monkey Patch Removal] Remove Object#__sortable__ (#5708)
* Remove Object#__sortable__ monkey patch, which is only used in Contextual::Memory. Its been moved to private method with a minor amount of refactoring. Existing tests already cover all the functionality. * deprecate __sortable__ instead of removing it --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent 78fb959 commit ccb519c

File tree

7 files changed

+35
-37
lines changed

7 files changed

+35
-37
lines changed

lib/mongoid/contextual/memory.rb

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -627,23 +627,24 @@ def apply_sorting
627627
end
628628
end
629629

630-
# Compare two values, checking for nil.
630+
# Compare two values, handling the cases when
631+
# either value is nil.
631632
#
632633
# @api private
633634
#
634635
# @example Compare the two objects.
635636
# context.compare(a, b)
636637
#
637638
# @param [ Object ] a The first object.
638-
# @param [ Object ] b The first object.
639+
# @param [ Object ] b The second object.
639640
#
640641
# @return [ Integer ] The comparison value.
641642
def compare(a, b)
642-
case
643-
when a.nil? then b.nil? ? 0 : 1
644-
when b.nil? then -1
645-
else a <=> b
646-
end
643+
return 0 if a.nil? && b.nil?
644+
return 1 if a.nil?
645+
return -1 if b.nil?
646+
647+
compare_operand(a) <=> compare_operand(b)
647648
end
648649

649650
# Sort the documents in place.
@@ -655,9 +656,8 @@ def compare(a, b)
655656
def in_place_sort(values)
656657
documents.sort! do |a, b|
657658
values.map do |field, direction|
658-
a_value, b_value = a[field], b[field]
659-
direction * compare(a_value.__sortable__, b_value.__sortable__)
660-
end.find { |value| !value.zero? } || 0
659+
direction * compare(a[field], b[field])
660+
end.detect { |value| !value.zero? } || 0
661661
end
662662
end
663663

@@ -683,6 +683,23 @@ def _session
683683
@criteria.send(:_session)
684684
end
685685

686+
# Get the operand value to be used in comparison.
687+
# Adds capability to sort boolean values.
688+
#
689+
# @example Get the comparison operand.
690+
# compare_operand(true) #=> 1
691+
#
692+
# @param [ Object ] value The value to be used in comparison.
693+
#
694+
# @return [ Integer | Object ] The comparison operand.
695+
def compare_operand(value)
696+
case value
697+
when TrueClass then 1
698+
when FalseClass then 0
699+
else value
700+
end
701+
end
702+
686703
# Retrieve the value for the current document at the given field path.
687704
#
688705
# For example, if I have the following models:

lib/mongoid/extensions/false_class.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
module Mongoid
55
module Extensions
6-
76
# Adds type-casting behavior to FalseClass.
87
module FalseClass
9-
108
# Get the value of the object as a mongo friendly sort value.
119
#
1210
# @example Get the object as sort criteria.
1311
# object.__sortable__
1412
#
1513
# @return [ Integer ] 0.
14+
# @deprecated
1615
def __sortable__
1716
0
1817
end
18+
Mongoid.deprecate(self, :__sortable__)
1919

2020
# Is the passed value a boolean?
2121
#
@@ -35,4 +35,4 @@ def is_a?(other)
3535
end
3636
end
3737

38-
::FalseClass.__send__(:include, Mongoid::Extensions::FalseClass)
38+
FalseClass.include Mongoid::Extensions::FalseClass

lib/mongoid/extensions/object.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ def __setter__
6060
# object.__sortable__
6161
#
6262
# @return [ Object ] self.
63+
# @deprecated
6364
def __sortable__
6465
self
6566
end
67+
Mongoid.deprecate(self, :__sortable__)
6668

6769
# Conversion of an object to an $inc-able value.
6870
#

lib/mongoid/extensions/true_class.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
module Mongoid
55
module Extensions
6-
76
# Adds type-casting behavior to TrueClass
87
module TrueClass
9-
108
# Get the value of the object as a mongo friendly sort value.
119
#
1210
# @example Get the object as sort criteria.
1311
# object.__sortable__
1412
#
1513
# @return [ Integer ] 1.
14+
# @deprecated
1615
def __sortable__
1716
1
1817
end
18+
Mongoid.deprecate(self, :__sortable__)
1919

2020
# Is the passed value a boolean?
2121
#
@@ -35,4 +35,4 @@ def is_a?(other)
3535
end
3636
end
3737

38-
::TrueClass.__send__(:include, Mongoid::Extensions::TrueClass)
38+
TrueClass.include Mongoid::Extensions::TrueClass

spec/mongoid/extensions/false_class_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66
describe Mongoid::Extensions::FalseClass do
77

8-
describe "#__sortable__" do
9-
10-
it "returns 0" do
11-
expect(false.__sortable__).to eq(0)
12-
end
13-
end
14-
158
describe "#is_a?" do
169

1710
context "when provided a Boolean" do

spec/mongoid/extensions/object_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,6 @@
128128
end
129129
end
130130

131-
describe "#__sortable__" do
132-
133-
it "returns self" do
134-
expect(object.__sortable__).to eq(object)
135-
end
136-
end
137-
138131
describe ".demongoize" do
139132

140133
let(:object) do

spec/mongoid/extensions/true_class_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66
describe Mongoid::Extensions::TrueClass do
77

8-
describe "#__sortable__" do
9-
10-
it "returns 1" do
11-
expect(true.__sortable__).to eq(1)
12-
end
13-
end
14-
158
describe "#is_a?" do
169

1710
context "when provided a Boolean" do

0 commit comments

Comments
 (0)