Skip to content

Commit

Permalink
Get rid of extra loop
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Aug 27, 2023
1 parent 37fdeba commit 65ccfbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/contextual/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ def load_async
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, prepare_atomic_updates(klass, attributes), opts)
end

Expand Down Expand Up @@ -1076,6 +1075,7 @@ def retrieve_nth_to_last_with_limit(n, limit)
# @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)
Expand Down
28 changes: 14 additions & 14 deletions spec/mongoid/contextual/mongo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4573,9 +4573,9 @@
end

describe '#prepare_atomic_updates' do
let(:instance) { described_class.new(Band.where(name: 'Depeche Mode')) }
subject(:updates) { instance.send(:prepare_atomic_updates, Band, hash) }

subject { 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

Expand All @@ -4585,10 +4585,10 @@
end

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

Expand All @@ -4598,10 +4598,10 @@
end

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

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

0 comments on commit 65ccfbc

Please sign in to comment.