Skip to content

Commit

Permalink
Handle empty strings when setting ObjectID
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernerd Schaefer and Veezus Kreist authored and Pairing Workstation Hancock committed Aug 3, 2010
1 parent b3f2aab commit 0168df2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/objectid/conversions.rb
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions spec/integration/mongoid/extensions_spec.rb
Expand Up @@ -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
Expand Down
32 changes: 17 additions & 15 deletions spec/unit/mongoid/extensions/objectid/conversions_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 0168df2

Please sign in to comment.