Skip to content

Commit

Permalink
MONGOID-5670 [Monkey Patch Removal] Remove Hash#delete_id and Hash#ex…
Browse files Browse the repository at this point in the history
…tract_id (#5701)

* Move Hash#delete_id and Hash#extract_id to Nested::NestedBuildable

* Fix failing spec

* deprecate, don't remove

---------

Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
johnnyshields and jamis authored Nov 7, 2023
1 parent fc98ff0 commit 7378141
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/mongoid/association/nested/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def over_limit?(attributes)
# @param [ Hash ] attrs The single document attributes to process.
def process_attributes(parent, attrs)
return if reject?(parent, attrs)
if id = attrs.extract_id

if (id = extract_id(attrs))
update_nested_relation(parent, id, attrs)
else
existing.push(Factory.build(@class_name, attrs)) unless destroyable?(attrs)
Expand Down Expand Up @@ -153,7 +154,7 @@ def destroy_document(relation, doc)
# @param [ Document ] doc The document to update.
# @param [ Hash ] attrs The attributes.
def update_document(doc, attrs)
attrs.delete_id
delete_id(attrs)
if association.embedded?
doc.assign_attributes(attrs)
else
Expand Down
27 changes: 27 additions & 0 deletions lib/mongoid/association/nested/nested_buildable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ def update_only?
def convert_id(klass, id)
klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
end

private

# Get the id attribute from the given hash, whether it's
# prefixed with an underscore or is a symbol.
#
# @example Get the id.
# extract_id({ _id: 1 })
#
# @param [ Hash ] hash The hash from which to extract.
#
# @return [ Object ] The value of the id.
def extract_id(hash)
hash['_id'] || hash[:_id] || hash['id'] || hash[:id]
end

# Deletes the id key from the given hash.
#
# @example Delete an id value.
# delete_id({ "_id" => 1 })
#
# @param [ Hash ] hash The hash from which to delete.
#
# @return [ Object ] The deleted value, or nil.
def delete_id(hash)
hash.delete('_id') || hash.delete(:_id) || hash.delete('id') || hash.delete(:id)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/association/nested/one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def build(parent)
return if reject?(parent, attributes)
@existing = parent.send(association.name)
if update?
attributes.delete_id
delete_id(attributes)
existing.assign_attributes(attributes)
elsif replace?
parent.send(association.setter, Factory.build(@class_name, attributes))
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/criteria.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def embedded?
#
# @return [ Object ] The id.
def extract_id
selector.extract_id
selector['_id'] || selector[:_id] || selector['id'] || selector[:id]
end

# Adds a criterion to the +Criteria+ that specifies additional options
Expand Down Expand Up @@ -225,7 +225,7 @@ def initialize(klass)
# may be desired.
#
# @example Merge the criteria with another criteria.
# criteri.merge(other_criteria)
# criteria.merge(other_criteria)
#
# @example Merge the criteria with a hash. The hash must contain a klass
# key and the key/value pairs correspond to method names/args.
Expand Down
4 changes: 4 additions & 0 deletions lib/mongoid/extensions/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ def _mongoid_unsatisfiable_criteria?
# {}.delete_id
#
# @return [ Object ] The deleted value, or nil.
# @deprecated
def delete_id
delete("_id") || delete(:_id) || delete("id") || delete(:id)
end
Mongoid.deprecate(self, :delete_id)

# Get the id attribute from this hash, whether it's prefixed with an
# underscore or is a symbol.
Expand All @@ -112,9 +114,11 @@ def delete_id
# { :_id => 1 }.extract_id
#
# @return [ Object ] The value of the id.
# @deprecated
def extract_id
self["_id"] || self[:_id] || self["id"] || self[:id]
end
Mongoid.deprecate(self, :extract_id)

# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
Expand Down

0 comments on commit 7378141

Please sign in to comment.