Fix bug with CanDefinition#tableized_conditions being used with Mongoid documents and add more specs for accesible_by with Mongoid.

This commit is contained in:
Mani Tadayon 2010-10-14 17:39:19 -07:00
parent 25bf479f48
commit d256aeb26e
2 changed files with 24 additions and 8 deletions

View File

@ -22,7 +22,7 @@ module CanCan
end
def conditions
@can_definitions.first.try(:tableized_conditions)
@can_definitions.first.instance_variable_get(:@conditions)
end
end
@ -83,6 +83,7 @@ module CanCan
# internally uses Ability#conditions method, see that for more information.
def accessible_by(ability, action = :read)
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

View File

@ -58,11 +58,21 @@ describe CanCan::MongoidAdditions do
@model_class.accessible_by(@ability, :read).should == []
end
it "should return the correct records based on the defined ability" do
@ability.can :read, @model_class, :title => "Sir"
sir = @model_class.create :title => 'Sir'
lord = @model_class.create :title => 'Lord'
dude = @model_class.create :title => 'Dude'
@model_class.accessible_by(@ability, :read).should == [sir]
end
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
it "should handle :field.in" do
obj = @model_class.create :title => 'Sir'
@ability.can :read, @model_class, :title.in => ["Sir", "Madam"]
@ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :title => 'Lord'
@ability.can?(:read, obj2).should == false
@ -72,6 +82,7 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :title => 'Sir'
@ability.can :read, @model_class, :title.nin => ["Lord", "Madam"]
@ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :title => 'Lord'
@ability.can?(:read, obj2).should == false
@ -81,6 +92,7 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :titles => ['Palatin', 'Margrave']
@ability.can :read, @model_class, :titles.size => 2
@ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :titles => ['Palatin', 'Margrave', 'Marquis']
@ability.can?(:read, obj2).should == false
@ -90,6 +102,7 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :titles => ['Palatin', 'Margrave']
@ability.can :read, @model_class, :titles.exists => true
@ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create
@ability.can?(:read, obj2).should == false
@ -99,6 +112,7 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :age => 50
@ability.can :read, @model_class, :age.gt => 45
@ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :age => 40
@ability.can?(:read, obj2).should == false
@ -106,8 +120,9 @@ describe CanCan::MongoidAdditions do
end
it "should call where with matching ability conditions" do
obj = @model_class.create :foo => {:bar => 1}
@ability.can :read, @model_class, :foo => {:bar => 1}
@model_class.accessible_by(@ability, :read).should == @model_class.where(:foos => { :bar => 1 })
@model_class.accessible_by(@ability, :read).entries.first.should == obj
end
it "should not allow to fetch records when ability with just block present" do