Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5654 [Monkey Patch Removal] Move Hash#__consolidate__ into Mongoid::Contextual #5689

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ def load_async
# @return [ true | false ] If the update succeeded.
def update_documents(attributes, method = :update_one, opts = {})
return false unless attributes
attributes = Hash[attributes.map { |k, v| [klass.database_field_name(k.to_s), v] }]
view.send(method, attributes.__consolidate__(klass), opts)

view.send(method, prepare_atomic_updates(klass, attributes), opts)
end

# Apply the field limitations.
Expand Down Expand Up @@ -1065,6 +1065,49 @@ def retrieve_nth_to_last_with_limit(n, limit)
raw_docs = v.to_a.reverse
process_raw_docs(raw_docs, limit)
end

# Convert the key/values in the attributes into a hash of atomic updates.
# Non-operator keys are assumed to use $set operation.
#
# @param [ Class ] klass The model class.
# @param [ Hash ] attributes The attributes to convert.
#
# @return [ Hash ] The prepared atomic updates.
def prepare_atomic_updates(klass, attributes)
attributes.each_pair.with_object({}) do |(key, value), atomic_updates|
key = klass.database_field_name(key)
if key.to_s.start_with?('$')
value = value.each_with_object({}) do |(key2, value2), hash|
key2 = klass.database_field_name(key2)
hash[key2] = key == '$rename' ? value2.to_s : mongoize_for_atomic_update(klass, key, key2, value2)
end
atomic_updates[key] ||= {}
atomic_updates[key].update(value)
else
atomic_updates['$set'] ||= {}
atomic_updates['$set'][key] = mongoize_for_atomic_update(klass, key, key, value)
end
end
end

# Mongoize a value for an atomic update for the given klass, operator, and field.
#
# @param [ Class ] klass The model class.
# @param [ String ] operator The operator.
# @param [ String | Symbol ] field_name The field key.
# @param [ Object ] value The value to mongoize.
#
# @return [ Object ] The mongoized value.
def mongoize_for_atomic_update(klass, operator, field_name, value)
field = klass.fields[field_name.to_s]
return value unless field

mongoized = field.mongoize(value)
if Mongoid::Persistable::LIST_OPERATIONS.include?(operator) && field.resizable? && !value.is_a?(Array)
mongoized = mongoized.first
end
mongoized
end
end
end
end
55 changes: 0 additions & 55 deletions lib/mongoid/extensions/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,6 @@ def __mongoize_object_id__
end
end

# Consolidate the key/values in the hash under an atomic $set.
#
# @example Consolidate the hash.
# { name: "Placebo" }.__consolidate__
#
# @return [ Hash ] A new consolidated hash.
def __consolidate__(klass)
consolidated = {}
each_pair do |key, value|
if key =~ /\$/
value.keys.each do |key2|
value2 = value[key2]
real_key = klass.database_field_name(key2)

value.delete(key2) if real_key != key2
value[real_key] = (key == "$rename") ? value2.to_s : mongoize_for(key, klass, real_key, value2)
end
consolidated[key] ||= {}
consolidated[key].update(value)
else
consolidated["$set"] ||= {}
consolidated["$set"].update(key => mongoize_for(key, klass, key, value))
end
end
consolidated
end

# Checks whether conditions given in this hash are known to be
# unsatisfiable, i.e., querying with this hash will always return no
# documents.
Expand Down Expand Up @@ -186,34 +159,6 @@ def to_criteria
criteria
end

private

# Mongoize for the klass, key and value.
#
# @api private
#
# @example Mongoize for the klass, field and value.
# {}.mongoize_for("$push", Band, "name", "test")
#
# @param [ String ] operator The operator.
# @param [ Class ] klass The model class.
# @param [ String | Symbol ] key The field key.
# @param [ Object ] value The value to mongoize.
#
# @return [ Object ] The mongoized value.
def mongoize_for(operator, klass, key, value)
field = klass.fields[key.to_s]
if field
val = field.mongoize(value)
if Mongoid::Persistable::LIST_OPERATIONS.include?(operator) && field.resizable?
val = val.first if !value.is_a?(Array)
end
val
else
value
end
end

module ClassMethods

# Turn the object from the ruby type we deal with to a Mongo friendly
Expand Down
48 changes: 48 additions & 0 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4571,4 +4571,52 @@
end
end
end

describe '#prepare_atomic_updates' do
subject(:updates) { instance.send(:prepare_atomic_updates, Band, hash) }

let(:instance) { described_class.new(Band.where(name: 'Depeche Mode')) }

context 'when the hash already contains the key' do

context 'when the $set is first' do
let(:hash) do
{ '$set' => { name: 'Tool' }, likes: 10, '$inc' => { plays: 1 } }
end

it 'moves the non hash values under the provided key' do
expect(updates).to eq({
'$set' => { 'name' => 'Tool', 'likes' => 10 },
'$inc' => { 'plays' => 1 }
})
end
end

context 'when the $set is not first' do
let(:hash) do
{ likes: 10, '$inc' => { plays: 1 }, '$set' => { name: 'Tool' } }
end

it 'moves the non hash values under the provided key' do
expect(updates).to eq({
'$set' => { 'likes' => 10, 'name' => 'Tool' },
'$inc' => { 'plays' => 1 }
})
end
end
end

context 'when the hash does not contain the key' do
let(:hash) do
{ likes: 10, '$inc' => { plays: 1 }, name: 'Tool' }
end

it 'moves the non hash values under the provided key' do
expect(updates).to eq({
'$set' => { 'likes' => 10, 'name' => 'Tool' },
'$inc' => { 'plays' => 1 }
})
end
end
end
end
57 changes: 0 additions & 57 deletions spec/mongoid/extensions/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,63 +163,6 @@
end
end

describe "#__consolidate__" do

context "when the hash already contains the key" do

context "when the $set is first" do

let(:hash) do
{ "$set" => { name: "Tool" }, likes: 10, "$inc" => { plays: 1 }}
end

let(:consolidated) do
hash.__consolidate__(Band)
end

it "moves the non hash values under the provided key" do
expect(consolidated).to eq({
"$set" => { 'name' => "Tool", likes: 10 }, "$inc" => { 'plays' => 1 }
})
end
end

context "when the $set is not first" do

let(:hash) do
{ likes: 10, "$inc" => { plays: 1 }, "$set" => { name: "Tool" }}
end

let(:consolidated) do
hash.__consolidate__(Band)
end

it "moves the non hash values under the provided key" do
expect(consolidated).to eq({
"$set" => { likes: 10, 'name' => "Tool" }, "$inc" => { 'plays' => 1 }
})
end
end
end

context "when the hash does not contain the key" do

let(:hash) do
{ likes: 10, "$inc" => { plays: 1 }, name: "Tool"}
end

let(:consolidated) do
hash.__consolidate__(Band)
end

it "moves the non hash values under the provided key" do
expect(consolidated).to eq({
"$set" => { likes: 10, name: "Tool" }, "$inc" => { 'plays' => 1 }
})
end
end
end

context "when the hash key is a string" do

let(:hash) do
Expand Down