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 end
def conditions def conditions
@can_definitions.first.try(:tableized_conditions) @can_definitions.first.instance_variable_get(:@conditions)
end end
end end
@ -82,7 +82,8 @@ module CanCan
# Here only the articles which the user can update are returned. This # Here only the articles which the user can update are returned. This
# 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? if query.conditions.blank?
# this query is sure to return no results # 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 # 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 == [] @model_class.accessible_by(@ability, :read).should == []
end 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 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'
@ability.can :read, @model_class, :title.in => ["Sir", "Madam"] @ability.can :read, @model_class, :title.in => ["Sir", "Madam"]
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :title => 'Lord' obj2 = @model_class.create :title => 'Lord'
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
@ -72,7 +82,8 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :title => 'Sir' obj = @model_class.create :title => 'Sir'
@ability.can :read, @model_class, :title.nin => ["Lord", "Madam"] @ability.can :read, @model_class, :title.nin => ["Lord", "Madam"]
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :title => 'Lord' obj2 = @model_class.create :title => 'Lord'
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
@ -81,7 +92,8 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :titles => ['Palatin', 'Margrave'] obj = @model_class.create :titles => ['Palatin', 'Margrave']
@ability.can :read, @model_class, :titles.size => 2 @ability.can :read, @model_class, :titles.size => 2
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :titles => ['Palatin', 'Margrave', 'Marquis'] obj2 = @model_class.create :titles => ['Palatin', 'Margrave', 'Marquis']
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
@ -90,7 +102,8 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :titles => ['Palatin', 'Margrave'] obj = @model_class.create :titles => ['Palatin', 'Margrave']
@ability.can :read, @model_class, :titles.exists => true @ability.can :read, @model_class, :titles.exists => true
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create obj2 = @model_class.create
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
@ -99,15 +112,17 @@ describe CanCan::MongoidAdditions do
obj = @model_class.create :age => 50 obj = @model_class.create :age => 50
@ability.can :read, @model_class, :age.gt => 45 @ability.can :read, @model_class, :age.gt => 45
@ability.can?(:read, obj).should == true @ability.can?(:read, obj).should == true
@model_class.accessible_by(@ability, :read).should == [obj]
obj2 = @model_class.create :age => 40 obj2 = @model_class.create :age => 40
@ability.can?(:read, obj2).should == false @ability.can?(:read, obj2).should == false
end end
end end
it "should call where with matching ability conditions" do it "should call where with matching ability conditions" do
@ability.can :read, @model_class, :foo => {:bar => 1} obj = @model_class.create :foo => {:bar => 1}
@model_class.accessible_by(@ability, :read).should == @model_class.where(:foos => { :bar => 1 }) @ability.can :read, @model_class, :foo => {:bar => 1}
@model_class.accessible_by(@ability, :read).entries.first.should == obj
end end
it "should not allow to fetch records when ability with just block present" do it "should not allow to fetch records when ability with just block present" do