diff --git a/lib/mongoid/extensions/objectid/conversions.rb b/lib/mongoid/extensions/objectid/conversions.rb index 71b279aaae..06e346dc9d 100644 --- a/lib/mongoid/extensions/objectid/conversions.rb +++ b/lib/mongoid/extensions/objectid/conversions.rb @@ -5,7 +5,7 @@ module ObjectID #:nodoc: module Conversions #:nodoc: def set(value) if value.is_a?(::String) - BSON::ObjectID.from_string(value) + BSON::ObjectID.from_string(value) unless value.blank? else value end diff --git a/spec/integration/mongoid/extensions_spec.rb b/spec/integration/mongoid/extensions_spec.rb index b39bc5b0e9..69e35ce56b 100644 --- a/spec/integration/mongoid/extensions_spec.rb +++ b/spec/integration/mongoid/extensions_spec.rb @@ -22,32 +22,35 @@ context "setting association foreign keys" do - context "when value is a string" do + let(:game) { Game.new } + let(:person) { Person.create(:ssn => "555555555555555") } - before do - @game = Game.new - @person = Person.create(:ssn => "555555555555555") + context "when value is an empty string" do + + it "should set the foreign key to nil" do + game.person_id = "" + game.save + game.reload.person_id.should be_nil end + end + + context "when value is a populated string" do + it "should set the foreign key as ObjectID" do - @game.person_id = @person.id.to_s - @game.save - @game.reload.person_id.should == @person.id + game.person_id = person.id.to_s + game.save + game.reload.person_id.should == person.id end end context "when value is a ObjectID" do - before do - @game = Game.new - @person = Person.create(:ssn => "555555555555555") - end - it "should keep the the foreign key as ObjectID" do - @game.person_id = @person.id - @game.save - @game.reload.person_id.should == @person.id + game.person_id = person.id + game.save + game.reload.person_id.should == person.id end end diff --git a/spec/unit/mongoid/extensions/objectid/conversions_spec.rb b/spec/unit/mongoid/extensions/objectid/conversions_spec.rb index c0ec8b77a6..3a4aa35e2a 100644 --- a/spec/unit/mongoid/extensions/objectid/conversions_spec.rb +++ b/spec/unit/mongoid/extensions/objectid/conversions_spec.rb @@ -2,13 +2,7 @@ describe Mongoid::Extensions::ObjectID::Conversions do - let(:object_id) do - BSON::ObjectID.new - end - - let(:object_id_string) do - "4c52c439931a90ab29000003" - end + let(:object_id) { BSON::ObjectID.new } describe "#get" do @@ -18,18 +12,26 @@ end - describe "#set with ObjectID" do + describe "#set" do - it "returns self" do - BSON::ObjectID.set(object_id).should == object_id - end + let(:object_id_string) { "4c52c439931a90ab29000003" } - end + context "with a blank string" do + it "returns nil" do + BSON::ObjectID.set("").should be_nil + end + end - describe "#set with String" do + context "with a populated string" do + it "returns ObjectID" do + BSON::ObjectID.set(object_id_string).should == BSON::ObjectID.from_string(object_id_string) + end + end - it "returns ObjectID" do - BSON::ObjectID.set(object_id_string).should == BSON::ObjectID.from_string(object_id_string) + context "with an ObjectID" do + it "returns self" do + BSON::ObjectID.set(object_id).should == object_id + end end end