Skip to content

Commit

Permalink
MONGOID-5675 [Monkey Patch Removal] Remove Object#__mongoize_fk__ (#5715
Browse files Browse the repository at this point in the history
)

* __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]>
  • Loading branch information
johnnyshields and jamis authored Nov 8, 2023
1 parent 0aaf8d5 commit ca3c6a5
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 362 deletions.
2 changes: 2 additions & 0 deletions lib/mongoid/extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ module ClassMethods
# @param [ Object ] object The object to convert.
#
# @return [ Array ] The array of ids.
# @deprecated
def __mongoize_fk__(association, object)
if object.resizable?
object.blank? ? object : association.convert_to_foreign_key(object)
else
object.blank? ? [] : association.convert_to_foreign_key(Array(object))
end
end
Mongoid.deprecate(self, :__mongoize_fk__)

# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,12 @@ module ClassMethods
# @param [ Object ] object The object to convert.
#
# @return [ Object ] The converted object.
# @deprecated
def __mongoize_fk__(association, object)
return nil if !object || object == ""
association.convert_to_foreign_key(object)
end
Mongoid.deprecate(self, :__mongoize_fk__)

# Convert the object from its mongo friendly ruby type to this type.
#
Expand Down
26 changes: 24 additions & 2 deletions lib/mongoid/fields/foreign_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ForeignKey < Standard
# @example Add the atomic changes.
# field.add_atomic_changes(doc, "key", {}, [], [])
#
# @todo: Durran: Refactor, big time.
# @todo: Refactor, big time.
#
# @param [ Document ] document The document to add to.
# @param [ String ] name The name of the field.
Expand Down Expand Up @@ -95,7 +95,7 @@ def lazy?
# @return [ Object ] The mongoized object.
def mongoize(object)
if type.resizable? || object_id_field?
type.__mongoize_fk__(association, object)
mongoize_foreign_key(object)
else
related_id_field.mongoize(object)
end
Expand Down Expand Up @@ -124,6 +124,28 @@ def resizable?

private

# Convert the provided object to a Mongo-friendly foreign key.
#
# @example Convert the object to a foreign key.
# mongoize_foreign_key(object)
#
# @param [ Object ] object The object to convert.
#
# @return [ Object ] The converted object.
def mongoize_foreign_key(object)
if type == Array || type == Set
object = object.to_a if type == Set || object.is_a?(Set)

if object.resizable?
object.blank? ? object : association.convert_to_foreign_key(object)
else
object.blank? ? [] : association.convert_to_foreign_key(Array(object))
end
elsif !(object.nil? || object == '')
association.convert_to_foreign_key(object)
end
end

# Evaluate the default proc. In some cases we need to instance exec,
# in others we don't.
#
Expand Down
150 changes: 0 additions & 150 deletions spec/mongoid/extensions/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,156 +203,6 @@
end
end

describe ".__mongoize_fk__" do

context "when the related model uses object ids" do

let(:association) do
Person.relations["preferences"]
end

context "when provided an object id" do

let(:object_id) do
BSON::ObjectId.new
end

let(:fk) do
Array.__mongoize_fk__(association, object_id)
end

it "returns the object id as an array" do
expect(fk).to eq([ object_id ])
end
end

context "when provided a object ids" do

let(:object_id) do
BSON::ObjectId.new
end

let(:fk) do
Array.__mongoize_fk__(association, [ object_id ])
end

it "returns the object ids" do
expect(fk).to eq([ object_id ])
end
end

context "when provided a string" do

context "when the string is a legal object id" do

let(:object_id) do
BSON::ObjectId.new
end

let(:fk) do
Array.__mongoize_fk__(association, object_id.to_s)
end

it "returns the object id in an array" do
expect(fk).to eq([ object_id ])
end
end

context "when the string is not a legal object id" do

let(:string) do
"blah"
end

let(:fk) do
Array.__mongoize_fk__(association, string)
end

it "returns the string in an array" do
expect(fk).to eq([ string ])
end
end

context "when the string is blank" do

let(:fk) do
Array.__mongoize_fk__(association, "")
end

it "returns an empty array" do
expect(fk).to be_empty
end
end
end

context "when provided nil" do

let(:fk) do
Array.__mongoize_fk__(association, nil)
end

it "returns an empty array" do
expect(fk).to be_empty
end
end

context "when provided an array of strings" do

context "when the strings are legal object ids" do

let(:object_id) do
BSON::ObjectId.new
end

let(:fk) do
Array.__mongoize_fk__(association, [ object_id.to_s ])
end

it "returns the object id in an array" do
expect(fk).to eq([ object_id ])
end
end

context "when the strings are not legal object ids" do

let(:string) do
"blah"
end

let(:fk) do
Array.__mongoize_fk__(association, [ string ])
end

it "returns the string in an array" do
expect(fk).to eq([ string ])
end
end

context "when the strings are blank" do

let(:fk) do
Array.__mongoize_fk__(association, [ "", "" ])
end

it "returns an empty array" do
expect(fk).to be_empty
end
end
end

context "when provided nils" do

let(:fk) do
Array.__mongoize_fk__(association, [ nil, nil, nil ])
end

it "returns an empty array" do
expect(fk).to be_empty
end
end
end
end

describe "#__mongoize_time__" do

let(:array) do
Expand Down
91 changes: 0 additions & 91 deletions spec/mongoid/extensions/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,97 +23,6 @@
end
end

describe ".__mongoize_fk__" do

context "when the related model uses object ids" do

let(:association) do
Game.relations["person"]
end

context "when provided an object id" do

let(:object_id) do
BSON::ObjectId.new
end

let(:fk) do
Object.__mongoize_fk__(association, object_id)
end

it "returns the object id" do
expect(fk).to eq(object_id)
end
end

context "when provided a string" do

context "when the string is a legal object id" do

let(:object_id) do
BSON::ObjectId.new
end

let(:fk) do
Object.__mongoize_fk__(association, object_id.to_s)
end

it "returns the object id" do
expect(fk).to eq(object_id)
end
end

context "when the string is not a legal object id" do

let(:string) do
"blah"
end

let(:fk) do
Object.__mongoize_fk__(association, string)
end

it "returns the string" do
expect(fk).to eq(string)
end
end

context "when the string is blank" do

let(:fk) do
Object.__mongoize_fk__(association, "")
end

it "returns nil" do
expect(fk).to be_nil
end
end
end

context "when provided nil" do

let(:fk) do
Object.__mongoize_fk__(association, nil)
end

it "returns nil" do
expect(fk).to be_nil
end
end

context "when provided an empty array" do

let(:fk) do
Object.__mongoize_fk__(association, [])
end

it "returns an empty array" do
expect(fk).to eq([])
end
end
end
end

describe "#__mongoize_time__" do

it "returns self" do
Expand Down
Loading

0 comments on commit ca3c6a5

Please sign in to comment.