Fix accessible_by for Mongoid documents when no ability is defined.

The previous spec that checked for this was not right, since there were no documents in the collection, so every query would return an empty result.
This commit is contained in:
Mani Tadayon 2010-10-13 19:41:30 -07:00
parent ab82dcbc8f
commit 25bf479f48
2 changed files with 5 additions and 1 deletions

View File

@ -86,7 +86,7 @@ module CanCan
if query.conditions.blank?
# this query is sure to return no results
# we need this so there is a Mongoid::Criteria object to return, since an empty array would cause problems
where({:_id => {'$exists' => true, '$type' => 2}})
where({:_id => {'$exists' => false, '$type' => 7}}) # type 7 is an ObjectID (default for _id)
else
where(query.conditions)
end

View File

@ -51,6 +51,10 @@ describe CanCan::MongoidAdditions do
end
it "should return [] when no ability is defined so no records are found" do
@model_class.create :title => 'Sir'
@model_class.create :title => 'Lord'
@model_class.create :title => 'Dude'
@model_class.accessible_by(@ability, :read).should == []
end