Permalink
Browse files

Adds ability to use Scope query with Mongoid

Same limitations apply as with active record
* can not be OR'd with other rules for same ability/controller
  • Loading branch information...
Ryan Ahearn
Ryan Ahearn committed Apr 15, 2011
1 parent b1424df commit 2b6204117fa314de695b8954e774c89b88a8226d
@@ -18,6 +18,8 @@ def self.matches_conditions_hash?(subject, conditions)
def database_records
if @rules.size == 0
@model_class.where(:_id => {'$exists' => false, '$type' => 7}) # return no records in Mongoid
+ elsif @rules.size == 1 && @rules[0].conditions.is_a?(Mongoid::Criteria)
+ @rules[0].conditions
else
@rules.inject(@model_class.all) do |records, rule|
if rule.conditions.empty?
@@ -37,4 +39,4 @@ def database_records
# simplest way to add `accessible_by` to all Mongoid Documents
module Mongoid::Document::ClassMethods
include CanCan::ModelAdditions::ClassMethods
-end
+end
@@ -68,6 +68,14 @@ class MongoidProject
MongoidProject.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
end
+ it "should allow a scope for conditions" do
+ @ability.can :read, MongoidProject, MongoidProject.where(:title => 'Sir')
+ sir = MongoidProject.create(:title => 'Sir')
+ lord = MongoidProject.create(:title => 'Lord')
+ dude = MongoidProject.create(:title => 'Dude')
+
+ MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
+ end
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
it "should handle :field.in" do

0 comments on commit 2b62041

Please sign in to comment.