Merged pull request #343 from rahearn/mongoid-scope.
Adds ability to use Scope query with Mongoid
This commit is contained in:
commit
18c1007d3f
|
@ -18,6 +18,8 @@ module CanCan
|
||||||
def database_records
|
def database_records
|
||||||
if @rules.size == 0
|
if @rules.size == 0
|
||||||
@model_class.where(:_id => {'$exists' => false, '$type' => 7}) # return no records in Mongoid
|
@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
|
else
|
||||||
@rules.inject(@model_class.all) do |records, rule|
|
@rules.inject(@model_class.all) do |records, rule|
|
||||||
if rule.conditions.empty?
|
if rule.conditions.empty?
|
||||||
|
@ -37,4 +39,4 @@ end
|
||||||
# simplest way to add `accessible_by` to all Mongoid Documents
|
# simplest way to add `accessible_by` to all Mongoid Documents
|
||||||
module Mongoid::Document::ClassMethods
|
module Mongoid::Document::ClassMethods
|
||||||
include CanCan::ModelAdditions::ClassMethods
|
include CanCan::ModelAdditions::ClassMethods
|
||||||
end
|
end
|
||||||
|
|
|
@ -68,6 +68,14 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
|
||||||
MongoidProject.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
|
MongoidProject.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
|
||||||
end
|
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
|
describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
|
||||||
it "should handle :field.in" do
|
it "should handle :field.in" do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user