Permalink
Browse files

Merge branch 'master' of https://github.com/stellard/cancan into stel…

…lard-master
  • Loading branch information...
2 parents 2012311 + cff9229 commit 78cbea5733d34450a9deae70b2530e2d1b8db1d2 @ryanb committed Jan 19, 2011
Showing with 33 additions and 8 deletions.
  1. +1 −1 Gemfile
  2. +14 −6 lib/cancan/model_adapters/mongoid_adapter.rb
  3. +18 −1 spec/cancan/model_adapters/mongoid_adapter_spec.rb
View
@@ -11,7 +11,7 @@ when "data_mapper"
gem "dm-migrations", "~> 1.0.2"
when "mongoid"
gem "bson_ext", "~> 1.1"
- gem "mongoid", "~> 2.0.0.beta.19"
+ gem "mongoid", "~> 2.0.0.beta.20"
else
raise "Unknown model adapter: #{ENV["MODEL_ADAPTER"]}"
end
@@ -16,14 +16,22 @@ def self.matches_conditions_hash?(subject, conditions)
end
def database_records
- @model_class.where(conditions)
+ if @rules.size == 0
+ @model_class.where(false_query)
+ else
+ criteria = @model_class.all
+ @rules.each do |rule|
+ criteria = chain_criteria(rule, criteria)
+ end
+ criteria
+ end
end
-
- def conditions
- if @rules.size == 0
- false_query
+
+ def chain_criteria rule, criteria
+ if rule.base_behavior
+ criteria.or(rule.conditions)
else
- @rules.first.conditions
+ criteria.excludes(rule.conditions)
end
end
@@ -56,7 +56,7 @@ class MongoidProject
lord = MongoidProject.create(:title => 'Lord')
dude = MongoidProject.create(:title => 'Dude')
- MongoidProject.accessible_by(@ability, :read).should == [sir]
+ MongoidProject.accessible_by(@ability, :read).entries.should == [sir]
end
it "should return everything when the defined ability is manage all" do
@@ -154,7 +154,24 @@ class MongoidProject
@ability.can :read, MongoidProject, :foo => {:bar => 1}
MongoidProject.accessible_by(@ability, :read).entries.first.should == obj
end
+
+ it "should exclude from the result if set to cannot" do
+ obj = MongoidProject.create(:bar => 1)
+ obj2 = MongoidProject.create(:bar => 2)
+ @ability.can :read, MongoidProject
+ @ability.cannot :read, MongoidProject, :bar => 2
+ MongoidProject.accessible_by(@ability, :read).entries.should == [obj]
+ end
+ it "should combine the rules" do
+ obj = MongoidProject.create(:bar => 1)
+ obj2 = MongoidProject.create(:bar => 2)
+ obj3 = MongoidProject.create(:bar => 3)
+ @ability.can :read, MongoidProject, :bar => 1
+ @ability.can :read, MongoidProject, :bar => 2
+ MongoidProject.accessible_by(@ability, :read).entries.should =~ [obj, obj2]
+ end
+
it "should not allow to fetch records when ability with just block present" do
@ability.can :read, MongoidProject do
false

0 comments on commit 78cbea5

Please sign in to comment.