Permalink
Browse files

Merge pull request #1 from codegourmet/topic/mm-negative-queries

Topic/mm negative queries
  • Loading branch information...
maxprokopiev committed Sep 23, 2013
2 parents 0a2556b + 42c1228 commit 4a7442158bb740f2cd142477b40fad09d9533851
@@ -34,8 +34,14 @@ def database_records
if process_can_rules && rule.base_behavior
records.where rule.conditions
elsif !rule.base_behavior
- records.remove rule.conditions
- records
+ neg_conds = {}
+ rule.conditions.each_pair do |k,v|
+ if !k.is_a?(Symbol) || v.is_a?(Hash)
+ raise ArgumentError.new("unable to use complex cannot queries")
+ end
+ neg_conds[k] = { :$ne => v }
+ end
+ records.where(neg_conds)
else
records
end
@@ -94,6 +94,29 @@ class MongoMapperProject
MongoMapperProject.accessible_by(@ability, :read).entries.should == [obj]
end
+ it "should raise when a cannot query is complex" do
+ obj = MongoMapperProject.create(:bar => 1)
+ @ability.can :read, MongoMapperProject
+ @ability.cannot :read, MongoMapperProject, :bar.in => [1, 2]
+ lambda { MongoMapperProject.accessible_by(@ability, :read).entries }.should raise_error
+
+ @ability.can :edit, MongoMapperProject
+ @ability.cannot :edit, MongoMapperProject, :bar => {:$gt => 3, :$lt => 5}
+ lambda { MongoMapperProject.accessible_by(@ability, :edit).entries }.should raise_error
+ end
+
+ it "should not modify the database" do
+ obj = MongoMapperProject.create(:bar => 1)
+ obj2 = MongoMapperProject.create(:bar => 2)
+ objects = MongoMapperProject.all
+
+ @ability.can :read, MongoMapperProject
+ @ability.cannot :read, MongoMapperProject, :bar => 2
+ entries = MongoMapperProject.accessible_by(@ability, :read).entries
+
+ MongoMapperProject.all.should == objects
+ end
+
it "should combine the rules" do
obj = MongoMapperProject.create(:bar => 1)
obj2 = MongoMapperProject.create(:bar => 2)

0 comments on commit 4a74421

Please sign in to comment.