Fix bug with Mongoid document where :manage :all caused accessible_by to return nothing and add specs to test for :manage :all.
This commit is contained in:
parent
d256aeb26e
commit
dbcd93e095
|
@ -22,10 +22,19 @@ module CanCan
|
||||||
end
|
end
|
||||||
|
|
||||||
def conditions
|
def conditions
|
||||||
|
if @can_definitions.size == 0
|
||||||
|
false_query
|
||||||
|
else
|
||||||
@can_definitions.first.instance_variable_get(:@conditions)
|
@can_definitions.first.instance_variable_get(:@conditions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def false_query
|
||||||
|
# this query is sure to return no results
|
||||||
|
{:_id => {'$exists' => false, '$type' => 7}} # type 7 is an ObjectID (default for _id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# customize to handle Mongoid queries in ability definitions conditions
|
# customize to handle Mongoid queries in ability definitions conditions
|
||||||
class CanDefinition
|
class CanDefinition
|
||||||
def matches_conditions_hash?(subject, conditions = @conditions)
|
def matches_conditions_hash?(subject, conditions = @conditions)
|
||||||
|
@ -83,16 +92,9 @@ module CanCan
|
||||||
# internally uses Ability#conditions method, see that for more information.
|
# internally uses Ability#conditions method, see that for more information.
|
||||||
def accessible_by(ability, action = :read)
|
def accessible_by(ability, action = :read)
|
||||||
query = ability.query(action, self)
|
query = ability.query(action, self)
|
||||||
|
|
||||||
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' => false, '$type' => 7}}) # type 7 is an ObjectID (default for _id)
|
|
||||||
else
|
|
||||||
where(query.conditions)
|
where(query.conditions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.extend ClassMethods
|
base.extend ClassMethods
|
||||||
|
|
|
@ -55,7 +55,7 @@ describe CanCan::MongoidAdditions do
|
||||||
@model_class.create :title => 'Lord'
|
@model_class.create :title => 'Lord'
|
||||||
@model_class.create :title => 'Dude'
|
@model_class.create :title => 'Dude'
|
||||||
|
|
||||||
@model_class.accessible_by(@ability, :read).should == []
|
@model_class.accessible_by(@ability, :read).entries.should == []
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return the correct records based on the defined ability" do
|
it "should return the correct records based on the defined ability" do
|
||||||
|
@ -67,6 +67,16 @@ describe CanCan::MongoidAdditions do
|
||||||
@model_class.accessible_by(@ability, :read).should == [sir]
|
@model_class.accessible_by(@ability, :read).should == [sir]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return everything when the defined ability is manage all" do
|
||||||
|
@ability.can :manage, :all
|
||||||
|
sir = @model_class.create :title => 'Sir'
|
||||||
|
lord = @model_class.create :title => 'Lord'
|
||||||
|
dude = @model_class.create :title => 'Dude'
|
||||||
|
|
||||||
|
@model_class.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
|
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
|
||||||
it "should handle :field.in" do
|
it "should handle :field.in" do
|
||||||
obj = @model_class.create :title => 'Sir'
|
obj = @model_class.create :title => 'Sir'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user