Permalink
Browse files

Merged pull request #343 from rahearn/mongoid-scope.

Adds ability to use Scope query with Mongoid
  • Loading branch information...
2 parents b1424df + 2b62041 commit 18c1007d3fff81d765554507dc97af7a25d9b596 @ryanb committed Apr 25, 2011
View
4 lib/cancan/model_adapters/mongoid_adapter.rb
@@ -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
View
8 spec/cancan/model_adapters/mongoid_adapter_spec.rb
@@ -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 18c1007

Please sign in to comment.