Skip to content

Commit

Permalink
Converting ids to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Nov 2, 2009
1 parent 383e61f commit 830c0cc
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def read_attribute(name)

# Reloads the +Document+ attributes from the database.
def reload
@attributes = HashWithIndifferentAccess.new(collection.find_one(id))
@attributes = HashWithIndifferentAccess.new(collection.find_one(:_id => id))
end

# Returns the id of the Document
Expand Down Expand Up @@ -257,7 +257,7 @@ def generate_key
values = primary_key.collect { |key| @attributes[key] }
@attributes[:_id] = values.join(" ").parameterize.to_s
else
@attributes[:_id] = Mongo::ObjectID.new unless id
@attributes[:_id] = Mongo::ObjectID.new.to_s unless id
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Comment < Mongoid::Document

it "persists a new record to the database" do
person = Person.create(:test => "Test")
person.id.should be_a_kind_of(Mongo::ObjectID)
person.id.should be_a_kind_of(String)
person.attributes[:test].should == "Test"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Name < Mongoid::Document
context "when finding by id" do

it "returns the document in the array with that id" do
name = @association.find(Mongo::ObjectID.new)
name = @association.find(Mongo::ObjectID.new.to_s)
name.should == @parent
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/mongoid/criteria_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ class Person < Mongoid::Document
describe "#id" do

it "adds the _id query to the selector" do
id = Mongo::ObjectID.new
id = Mongo::ObjectID.new.to_s
@criteria.id(id)
@criteria.selector.should == { :_id => id }
end

it "returns self" do
id = Mongo::ObjectID.new
id = Mongo::ObjectID.new.to_s
@criteria.id(id.to_s).should == @criteria
end

Expand Down Expand Up @@ -413,7 +413,7 @@ class Person < Mongoid::Document
context "with a single argument" do

it "creates a criteria for an object id" do
id = Mongo::ObjectID.new
id = Mongo::ObjectID.new.to_s
criteria = Mongoid::Criteria.translate(id)
criteria.selector.should == { :_id => id }
end
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/mongoid/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Address < Mongoid::Document
context "when an id is passed in" do

before do
@id = Mongo::ObjectID.new
@id = Mongo::ObjectID.new.to_s
end

it "delegates to criteria" do
Expand Down Expand Up @@ -526,8 +526,8 @@ class Address < Mongoid::Document

before do
@attributes = { "title" => "Herr" }
@person = Person.new(:_id => Mongo::ObjectID.new)
@collection.expects(:find_one).with(@person.id).returns(@attributes)
@person = Person.new(:_id => Mongo::ObjectID.new.to_s)
@collection.expects(:find_one).with(:_id => @person.id).returns(@attributes)
end

it "reloads the object attribtues from the database" do
Expand All @@ -549,7 +549,7 @@ class Address < Mongoid::Document
describe "#to_param" do

it "returns the id" do
id = Mongo::ObjectID.new
id = Mongo::ObjectID.new.to_s
Person.new(:_id => id).to_param.should == id.to_s
end

Expand Down
12 changes: 0 additions & 12 deletions spec/unit/mongoid/extensions/object/conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ class Person < Mongoid::Document

describe "#set" do

context "when object does not have attributes" do

before do
@id = Mongo::ObjectID.new
end

it "returns the object" do
Mongo::ObjectID.set(@id).should == @id
end

end

context "when object has attributes" do

before do
Expand Down

0 comments on commit 830c0cc

Please sign in to comment.