can? should only go to db if there are mongoid criteria in the conditions.

Easier to just do a simple comparison on the object in memory
than to search the database.  Also this allows method calls
and other attributes that might not be found in the database.
This commit is contained in:
Tyler Gannon
2010-11-15 19:43:54 -08:00
parent dbcd93e095
commit f6aaa581ef
4 changed files with 43 additions and 28 deletions

View File

@@ -49,6 +49,12 @@ describe CanCan::MongoidAdditions do
collection.name !~ /system/
end.each(&:drop)
end
it "should compare properties on mongoid documents with the " do
model = @model_class.new
@ability.can :read, @model_class, :id => model.id
@ability.should be_able_to :read, model
end
it "should return [] when no ability is defined so no records are found" do
@model_class.create :title => 'Sir'
@@ -88,6 +94,22 @@ describe CanCan::MongoidAdditions do
@ability.can?(:read, obj2).should == false
end
describe "activates only when there are Criteria in the hash" do
it "Calls where on the model class when there are criteria" do
obj = @model_class.create :title => 'Bird'
@conditions = {:title.nin => ["Fork", "Spoon"]}
@model_class.should_receive(:where).with(@conditions) {[obj]}
@ability.can :read, @model_class, @conditions
@ability.can?(:read, obj)
end
it "Calls the base version if there are no mongoid criteria" do
obj = @model_class.new :title => 'Bird'
@conditions = {:id => obj.id}
@ability.can :read, @model_class, @conditions
@ability.should be_able_to(:read, obj)
end
end
it "should handle :field.nin" do
obj = @model_class.create :title => 'Sir'
@ability.can :read, @model_class, :title.nin => ["Lord", "Madam"]
@@ -141,4 +163,4 @@ describe CanCan::MongoidAdditions do
@model_class.accessible_by(@ability)
}.should raise_error(CanCan::Error)
end
end
end

View File

@@ -5,9 +5,11 @@ require 'supermodel' # shouldn't Bundler do this already?
require 'active_support/all'
require 'matchers'
require 'cancan/matchers'
require 'mongoid'
require 'active_support'
RSpec.configure do |config|
config.mock_with :rr
config.mock_with :rspec
config.before(:each) do
Project.delete_all
Category.delete_all