Skip to content

Commit 276e846

Browse files
authored
MONGOID-5632 update_all with atomic operations and aliased fields (#5670)
* MONGOID-5632 update_all with atomic operations and aliased fields * failing specs due to __consolidate__ change * jruby 9.3 is complaining about #start_with? on a Symbol * fix failure related to expected arguments
1 parent 0aaf865 commit 276e846

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

lib/mongoid/association/proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Proxy
2626

2727
# We undefine most methods to get them sent through to the target.
2828
instance_methods.each do |method|
29-
undef_method(method) unless method.start_with?('__') || KEEPER_METHODS.include?(method)
29+
undef_method(method) unless method.to_s.start_with?('__') || KEEPER_METHODS.include?(method)
3030
end
3131

3232
include Threaded::Lifecycle

lib/mongoid/extensions/hash.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ def __consolidate__(klass)
3939
consolidated = {}
4040
each_pair do |key, value|
4141
if key =~ /\$/
42-
value.each_pair do |_key, _value|
43-
value[_key] = (key == "$rename") ? _value.to_s : mongoize_for(key, klass, _key, _value)
42+
value.keys.each do |key2|
43+
value2 = value[key2]
44+
real_key = klass.database_field_name(key2)
45+
46+
value.delete(key2) if real_key != key2
47+
value[real_key] = (key == "$rename") ? value2.to_s : mongoize_for(key, klass, real_key, value2)
4448
end
4549
consolidated[key] ||= {}
4650
consolidated[key].update(value)

spec/mongoid/contextual/mongo_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,20 @@
36113611
end
36123612
end
36133613

3614+
context 'when using aliased field names' do
3615+
before do
3616+
context.update_all('$set' => { years: 100 })
3617+
end
3618+
3619+
it "updates the first matching document" do
3620+
expect(depeche_mode.reload.years).to eq(100)
3621+
end
3622+
3623+
it "updates the last matching document" do
3624+
expect(new_order.reload.years).to eq(100)
3625+
end
3626+
end
3627+
36143628
context "when the attributes must be mongoized" do
36153629

36163630
before do

spec/mongoid/extensions/hash_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179

180180
it "moves the non hash values under the provided key" do
181181
expect(consolidated).to eq({
182-
"$set" => { name: "Tool", likes: 10 }, "$inc" => { plays: 1 }
182+
"$set" => { 'name' => "Tool", likes: 10 }, "$inc" => { 'plays' => 1 }
183183
})
184184
end
185185
end
@@ -196,7 +196,7 @@
196196

197197
it "moves the non hash values under the provided key" do
198198
expect(consolidated).to eq({
199-
"$set" => { likes: 10, name: "Tool" }, "$inc" => { plays: 1 }
199+
"$set" => { likes: 10, 'name' => "Tool" }, "$inc" => { 'plays' => 1 }
200200
})
201201
end
202202
end
@@ -214,7 +214,7 @@
214214

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

spec/mongoid/tasks/database_rake_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@
353353

354354
expect_any_instance_of(Mongo::ClientEncryption)
355355
.to receive(:create_data_key)
356-
.with('local')
356+
.with('local', {})
357357
.and_call_original
358358
end
359359

0 commit comments

Comments
 (0)