Skip to content

Commit 7378141

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5670 [Monkey Patch Removal] Remove Hash#delete_id and Hash#extract_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]>
1 parent fc98ff0 commit 7378141

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

lib/mongoid/association/nested/many.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def over_limit?(attributes)
101101
# @param [ Hash ] attrs The single document attributes to process.
102102
def process_attributes(parent, attrs)
103103
return if reject?(parent, attrs)
104-
if id = attrs.extract_id
104+
105+
if (id = extract_id(attrs))
105106
update_nested_relation(parent, id, attrs)
106107
else
107108
existing.push(Factory.build(@class_name, attrs)) unless destroyable?(attrs)
@@ -153,7 +154,7 @@ def destroy_document(relation, doc)
153154
# @param [ Document ] doc The document to update.
154155
# @param [ Hash ] attrs The attributes.
155156
def update_document(doc, attrs)
156-
attrs.delete_id
157+
delete_id(attrs)
157158
if association.embedded?
158159
doc.assign_attributes(attrs)
159160
else

lib/mongoid/association/nested/nested_buildable.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,33 @@ def update_only?
6565
def convert_id(klass, id)
6666
klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
6767
end
68+
69+
private
70+
71+
# Get the id attribute from the given hash, whether it's
72+
# prefixed with an underscore or is a symbol.
73+
#
74+
# @example Get the id.
75+
# extract_id({ _id: 1 })
76+
#
77+
# @param [ Hash ] hash The hash from which to extract.
78+
#
79+
# @return [ Object ] The value of the id.
80+
def extract_id(hash)
81+
hash['_id'] || hash[:_id] || hash['id'] || hash[:id]
82+
end
83+
84+
# Deletes the id key from the given hash.
85+
#
86+
# @example Delete an id value.
87+
# delete_id({ "_id" => 1 })
88+
#
89+
# @param [ Hash ] hash The hash from which to delete.
90+
#
91+
# @return [ Object ] The deleted value, or nil.
92+
def delete_id(hash)
93+
hash.delete('_id') || hash.delete(:_id) || hash.delete('id') || hash.delete(:id)
94+
end
6895
end
6996
end
7097
end

lib/mongoid/association/nested/one.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def build(parent)
2929
return if reject?(parent, attributes)
3030
@existing = parent.send(association.name)
3131
if update?
32-
attributes.delete_id
32+
delete_id(attributes)
3333
existing.assign_attributes(attributes)
3434
elsif replace?
3535
parent.send(association.setter, Factory.build(@class_name, attributes))

lib/mongoid/criteria.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def embedded?
162162
#
163163
# @return [ Object ] The id.
164164
def extract_id
165-
selector.extract_id
165+
selector['_id'] || selector[:_id] || selector['id'] || selector[:id]
166166
end
167167

168168
# Adds a criterion to the +Criteria+ that specifies additional options
@@ -225,7 +225,7 @@ def initialize(klass)
225225
# may be desired.
226226
#
227227
# @example Merge the criteria with another criteria.
228-
# criteri.merge(other_criteria)
228+
# criteria.merge(other_criteria)
229229
#
230230
# @example Merge the criteria with a hash. The hash must contain a klass
231231
# key and the key/value pairs correspond to method names/args.

lib/mongoid/extensions/hash.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ def _mongoid_unsatisfiable_criteria?
101101
# {}.delete_id
102102
#
103103
# @return [ Object ] The deleted value, or nil.
104+
# @deprecated
104105
def delete_id
105106
delete("_id") || delete(:_id) || delete("id") || delete(:id)
106107
end
108+
Mongoid.deprecate(self, :delete_id)
107109

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

119123
# Turn the object from the ruby type we deal with to a Mongo friendly
120124
# type.

0 commit comments

Comments
 (0)