Skip to content

Commit 90e1711

Browse files
authored
MONGOID-5410 change Hash#mongoize to always return BSON::Documents (#5420)
* MONGOID-5410 change Hash#mongoize to always return BSON::Documents * MONGOID-5410 fix tests
1 parent 6c03385 commit 90e1711

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

lib/mongoid/extensions/hash.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ module ClassMethods
220220
# @return [ Hash | nil ] The object mongoized or nil.
221221
def mongoize(object)
222222
return if object.nil?
223-
if object.is_a?(Hash)
224-
# Need to use transform_values! which maintains the BSON::Document
225-
# instead of transform_values which always returns a hash. To do this,
226-
# we first need to dup the hash.
223+
case object
224+
when BSON::Document
227225
object.dup.transform_values!(&:mongoize)
226+
when Hash
227+
BSON::Document.new(object.transform_values(&:mongoize))
228228
end
229229
end
230230

spec/mongoid/attributes_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,8 +1593,8 @@
15931593
expect(person.map).to be_nil
15941594
end
15951595

1596-
it "can set a Hash value" do
1597-
expect(person.map).to eq( { somekey: "somevalue" } )
1596+
it "can set a Hash value with stringified keys" do
1597+
expect(person.map).to eq( { "somekey" => "somevalue" } )
15981598
end
15991599
end
16001600

spec/mongoid/changeable_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@
207207

208208
it "returns an array of the original value and new value" do
209209
expect(person.send(:attribute_change, "map")).to eq(
210-
[{ location: "Home" }, { location: "Work" }]
210+
[{ "location" => "Home" }, { "location" => "Work" }]
211211
)
212212
end
213213

214214
it "allows access via (attribute)_change" do
215215
expect(person.map_change).to eq(
216-
[{ location: "Home" }, { location: "Work" }]
216+
[{ "location" => "Home" }, { "location" => "Work" }]
217217
)
218218
end
219219

@@ -225,7 +225,7 @@
225225

226226
it "returns an array of the original value and new value" do
227227
expect(person.send(:attribute_change, "map")).to eq(
228-
[{ location: "Home" }, { location: "Work", lat: 20.0 }]
228+
[{ "location" => "Home" }, { "location" => "Work", "lat" => 20.0 }]
229229
)
230230
end
231231
end

spec/mongoid/extensions/hash_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@
335335
it "converts the elements properly" do
336336
expect(mongoized[:date]).to eq(Time.utc(2012, 1, 1, 0, 0, 0))
337337
end
338+
339+
it "mongoizes to a BSON::Document" do
340+
expect(mongoized).to be_a(BSON::Document)
341+
end
338342
end
339343

340344
context "when object is nil" do
@@ -356,6 +360,20 @@
356360
expect(mongoized).to be_nil
357361
end
358362
end
363+
364+
describe "when mongoizing a BSON::Document" do
365+
let(:mongoized) do
366+
Hash.mongoize(BSON::Document.new({ x: 1, y: 2 }))
367+
end
368+
369+
it "returns the same hash" do
370+
expect(mongoized).to eq({ "x" => 1, "y" => 2 })
371+
end
372+
373+
it "returns a BSON::Document" do
374+
expect(mongoized).to be_a(BSON::Document)
375+
end
376+
end
359377
end
360378

361379
describe "#mongoize" do

spec/mongoid/fields_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,34 @@
893893
expect(product.attributes_before_type_cast["price"]).to eq(1)
894894
end
895895
end
896+
897+
context "when assigning a hash" do
898+
let(:person) { Person.create!(map: { x: 1 }) }
899+
900+
it "is a BSON::Document" do
901+
expect(person.map).to be_a(BSON::Document)
902+
end
903+
904+
it "has the correct contents" do
905+
expect(person.map).to eq({ "x" => 1 })
906+
end
907+
end
908+
909+
context "when loading a hash from the db" do
910+
before do
911+
Person.create!(map: { x: 1 })
912+
end
913+
914+
let(:person) { Person.first }
915+
916+
it "is a BSON::Document" do
917+
expect(person.map).to be_a(BSON::Document)
918+
end
919+
920+
it "has the correct contents" do
921+
expect(person.map).to eq({ "x" => 1 })
922+
end
923+
end
896924
end
897925

898926
describe "#defaults" do

0 commit comments

Comments
 (0)