Skip to content

Commit

Permalink
Convert params to Model.find(id) to whatever the _id field type is de…
Browse files Browse the repository at this point in the history
…fined as, no matter what the type. Closes #1380.
  • Loading branch information
durran committed Dec 3, 2011
1 parent 98d989a commit 1011f8d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +32,9 @@ For instructions on upgrading to newer versions, visit

* \#1387 Added `Model#cache_key` for use in Rails caching. (Seivan Heidari)

* \#1380 Calling Model.find(id) will now properly convert to and from any type
based on the type of the _id field.

### Resolved Issues

* \#1463 Batch insert consumers are now scoped to collection to avoid persistence
Expand Down
5 changes: 3 additions & 2 deletions lib/mongoid/criterion/optional.rb
Expand Up @@ -89,11 +89,12 @@ def extras(extras)
#
# @return [ Criteria ] The cloned criteria.
def for_ids(*ids)
field = klass.fields["_id"]
ids.flatten!
if ids.size > 1
where(:_id.in => ::BSON::ObjectId.convert(klass, ids))
any_in(:_id => ids.map{ |id| field.serialize(id) })
else
where(:_id => ids.first)
where(:_id => field.serialize(ids.first))
end
end

Expand Down
4 changes: 1 addition & 3 deletions spec/functional/mongoid/criteria_spec.rb
Expand Up @@ -80,9 +80,7 @@
end

it 'should find the object with a matching BSON::ObjectId argument' do
expect {
Person.find(BSON::ObjectId(person.id))
}.to raise_error
Person.find(BSON::ObjectId(person.id)).should eq(person)
end
end
end
Expand Down
41 changes: 41 additions & 0 deletions spec/functional/mongoid/finders_spec.rb
Expand Up @@ -8,6 +8,47 @@

describe "#find" do

context "when using integer ids" do

before(:all) do
Person.identity :type => Integer
end

after(:all) do
Person.identity :type => BSON::ObjectId
end

context "when passed a string" do

let!(:person) do
Person.create(:_id => 1, :ssn => "123-44-4321")
end

let(:from_db) do
Person.find("1")
end

it "returns the matching document" do
from_db.should eq(person)
end
end

context "when passed an array of strings" do

let!(:person) do
Person.create(:_id => 2, :ssn => "123-44-4321")
end

let(:from_db) do
Person.find([ "2" ])
end

it "returns the matching documents" do
from_db.should eq([ person ])
end
end
end

context "when using string ids" do

let!(:person) do
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/mongoid/criterion/optional_spec.rb
Expand Up @@ -342,8 +342,8 @@
base.for_ids(id)
end

it "adds the _id query to the selector" do
criteria.selector.should eq({ :_id => id })
it "adds the string _id query to the selector" do
criteria.selector.should eq({ :_id => id.to_s })
end

it "returns a copy" do
Expand Down Expand Up @@ -375,7 +375,7 @@
end

it "adds the _id query to the selector" do
base.for_ids(ids).selector.should eq({ :_id => ids.first })
base.for_ids(ids).selector.should eq({ :_id => ids.first.to_s })
end
end
end
Expand Down

0 comments on commit 1011f8d

Please sign in to comment.