Skip to content

Commit ca3c6a5

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5675 [Monkey Patch Removal] Remove Object#__mongoize_fk__ (#5715)
* __mongoize_fk step 1: move code to right class * __mongoize_fk step 1: #mongoize_foreign_key should use class member variables * __mongoize_fk step 3: consolidate all specs to public #mongoize method * Update foreign_key.rb * deprecate, rather than remove * Set support requires a bit of baby-sitting... --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent 0aaf8d5 commit ca3c6a5

File tree

6 files changed

+309
-362
lines changed

6 files changed

+309
-362
lines changed

lib/mongoid/extensions/array.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ module ClassMethods
114114
# @param [ Object ] object The object to convert.
115115
#
116116
# @return [ Array ] The array of ids.
117+
# @deprecated
117118
def __mongoize_fk__(association, object)
118119
if object.resizable?
119120
object.blank? ? object : association.convert_to_foreign_key(object)
120121
else
121122
object.blank? ? [] : association.convert_to_foreign_key(Array(object))
122123
end
123124
end
125+
Mongoid.deprecate(self, :__mongoize_fk__)
124126

125127
# Turn the object from the ruby type we deal with to a Mongo friendly
126128
# type.

lib/mongoid/extensions/object.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,12 @@ module ClassMethods
215215
# @param [ Object ] object The object to convert.
216216
#
217217
# @return [ Object ] The converted object.
218+
# @deprecated
218219
def __mongoize_fk__(association, object)
219220
return nil if !object || object == ""
220221
association.convert_to_foreign_key(object)
221222
end
223+
Mongoid.deprecate(self, :__mongoize_fk__)
222224

223225
# Convert the object from its mongo friendly ruby type to this type.
224226
#

lib/mongoid/fields/foreign_key.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ForeignKey < Standard
1414
# @example Add the atomic changes.
1515
# field.add_atomic_changes(doc, "key", {}, [], [])
1616
#
17-
# @todo: Durran: Refactor, big time.
17+
# @todo: Refactor, big time.
1818
#
1919
# @param [ Document ] document The document to add to.
2020
# @param [ String ] name The name of the field.
@@ -95,7 +95,7 @@ def lazy?
9595
# @return [ Object ] The mongoized object.
9696
def mongoize(object)
9797
if type.resizable? || object_id_field?
98-
type.__mongoize_fk__(association, object)
98+
mongoize_foreign_key(object)
9999
else
100100
related_id_field.mongoize(object)
101101
end
@@ -124,6 +124,28 @@ def resizable?
124124

125125
private
126126

127+
# Convert the provided object to a Mongo-friendly foreign key.
128+
#
129+
# @example Convert the object to a foreign key.
130+
# mongoize_foreign_key(object)
131+
#
132+
# @param [ Object ] object The object to convert.
133+
#
134+
# @return [ Object ] The converted object.
135+
def mongoize_foreign_key(object)
136+
if type == Array || type == Set
137+
object = object.to_a if type == Set || object.is_a?(Set)
138+
139+
if object.resizable?
140+
object.blank? ? object : association.convert_to_foreign_key(object)
141+
else
142+
object.blank? ? [] : association.convert_to_foreign_key(Array(object))
143+
end
144+
elsif !(object.nil? || object == '')
145+
association.convert_to_foreign_key(object)
146+
end
147+
end
148+
127149
# Evaluate the default proc. In some cases we need to instance exec,
128150
# in others we don't.
129151
#

spec/mongoid/extensions/array_spec.rb

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -203,156 +203,6 @@
203203
end
204204
end
205205

206-
describe ".__mongoize_fk__" do
207-
208-
context "when the related model uses object ids" do
209-
210-
let(:association) do
211-
Person.relations["preferences"]
212-
end
213-
214-
context "when provided an object id" do
215-
216-
let(:object_id) do
217-
BSON::ObjectId.new
218-
end
219-
220-
let(:fk) do
221-
Array.__mongoize_fk__(association, object_id)
222-
end
223-
224-
it "returns the object id as an array" do
225-
expect(fk).to eq([ object_id ])
226-
end
227-
end
228-
229-
context "when provided a object ids" do
230-
231-
let(:object_id) do
232-
BSON::ObjectId.new
233-
end
234-
235-
let(:fk) do
236-
Array.__mongoize_fk__(association, [ object_id ])
237-
end
238-
239-
it "returns the object ids" do
240-
expect(fk).to eq([ object_id ])
241-
end
242-
end
243-
244-
context "when provided a string" do
245-
246-
context "when the string is a legal object id" do
247-
248-
let(:object_id) do
249-
BSON::ObjectId.new
250-
end
251-
252-
let(:fk) do
253-
Array.__mongoize_fk__(association, object_id.to_s)
254-
end
255-
256-
it "returns the object id in an array" do
257-
expect(fk).to eq([ object_id ])
258-
end
259-
end
260-
261-
context "when the string is not a legal object id" do
262-
263-
let(:string) do
264-
"blah"
265-
end
266-
267-
let(:fk) do
268-
Array.__mongoize_fk__(association, string)
269-
end
270-
271-
it "returns the string in an array" do
272-
expect(fk).to eq([ string ])
273-
end
274-
end
275-
276-
context "when the string is blank" do
277-
278-
let(:fk) do
279-
Array.__mongoize_fk__(association, "")
280-
end
281-
282-
it "returns an empty array" do
283-
expect(fk).to be_empty
284-
end
285-
end
286-
end
287-
288-
context "when provided nil" do
289-
290-
let(:fk) do
291-
Array.__mongoize_fk__(association, nil)
292-
end
293-
294-
it "returns an empty array" do
295-
expect(fk).to be_empty
296-
end
297-
end
298-
299-
context "when provided an array of strings" do
300-
301-
context "when the strings are legal object ids" do
302-
303-
let(:object_id) do
304-
BSON::ObjectId.new
305-
end
306-
307-
let(:fk) do
308-
Array.__mongoize_fk__(association, [ object_id.to_s ])
309-
end
310-
311-
it "returns the object id in an array" do
312-
expect(fk).to eq([ object_id ])
313-
end
314-
end
315-
316-
context "when the strings are not legal object ids" do
317-
318-
let(:string) do
319-
"blah"
320-
end
321-
322-
let(:fk) do
323-
Array.__mongoize_fk__(association, [ string ])
324-
end
325-
326-
it "returns the string in an array" do
327-
expect(fk).to eq([ string ])
328-
end
329-
end
330-
331-
context "when the strings are blank" do
332-
333-
let(:fk) do
334-
Array.__mongoize_fk__(association, [ "", "" ])
335-
end
336-
337-
it "returns an empty array" do
338-
expect(fk).to be_empty
339-
end
340-
end
341-
end
342-
343-
context "when provided nils" do
344-
345-
let(:fk) do
346-
Array.__mongoize_fk__(association, [ nil, nil, nil ])
347-
end
348-
349-
it "returns an empty array" do
350-
expect(fk).to be_empty
351-
end
352-
end
353-
end
354-
end
355-
356206
describe "#__mongoize_time__" do
357207

358208
let(:array) do

spec/mongoid/extensions/object_spec.rb

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -23,97 +23,6 @@
2323
end
2424
end
2525

26-
describe ".__mongoize_fk__" do
27-
28-
context "when the related model uses object ids" do
29-
30-
let(:association) do
31-
Game.relations["person"]
32-
end
33-
34-
context "when provided an object id" do
35-
36-
let(:object_id) do
37-
BSON::ObjectId.new
38-
end
39-
40-
let(:fk) do
41-
Object.__mongoize_fk__(association, object_id)
42-
end
43-
44-
it "returns the object id" do
45-
expect(fk).to eq(object_id)
46-
end
47-
end
48-
49-
context "when provided a string" do
50-
51-
context "when the string is a legal object id" do
52-
53-
let(:object_id) do
54-
BSON::ObjectId.new
55-
end
56-
57-
let(:fk) do
58-
Object.__mongoize_fk__(association, object_id.to_s)
59-
end
60-
61-
it "returns the object id" do
62-
expect(fk).to eq(object_id)
63-
end
64-
end
65-
66-
context "when the string is not a legal object id" do
67-
68-
let(:string) do
69-
"blah"
70-
end
71-
72-
let(:fk) do
73-
Object.__mongoize_fk__(association, string)
74-
end
75-
76-
it "returns the string" do
77-
expect(fk).to eq(string)
78-
end
79-
end
80-
81-
context "when the string is blank" do
82-
83-
let(:fk) do
84-
Object.__mongoize_fk__(association, "")
85-
end
86-
87-
it "returns nil" do
88-
expect(fk).to be_nil
89-
end
90-
end
91-
end
92-
93-
context "when provided nil" do
94-
95-
let(:fk) do
96-
Object.__mongoize_fk__(association, nil)
97-
end
98-
99-
it "returns nil" do
100-
expect(fk).to be_nil
101-
end
102-
end
103-
104-
context "when provided an empty array" do
105-
106-
let(:fk) do
107-
Object.__mongoize_fk__(association, [])
108-
end
109-
110-
it "returns an empty array" do
111-
expect(fk).to eq([])
112-
end
113-
end
114-
end
115-
end
116-
11726
describe "#__mongoize_time__" do
11827

11928
it "returns self" do

0 commit comments

Comments
 (0)