Fix bug with Mongoid document where :manage :all caused accessible_by to return nothing and add specs to test for :manage :all.
This commit is contained in:
		
							parent
							
								
									d256aeb26e
								
							
						
					
					
						commit
						dbcd93e095
					
				@ -22,7 +22,16 @@ module CanCan
 | 
				
			|||||||
    end    
 | 
					    end    
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    def conditions
 | 
					    def conditions
 | 
				
			||||||
      @can_definitions.first.instance_variable_get(:@conditions)
 | 
					      if @can_definitions.size == 0
 | 
				
			||||||
 | 
					        false_query
 | 
				
			||||||
 | 
					      else
 | 
				
			||||||
 | 
					        @can_definitions.first.instance_variable_get(:@conditions)
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    def false_query
 | 
				
			||||||
 | 
					      # this query is sure to return no results
 | 
				
			||||||
 | 
					      {:_id => {'$exists' => false, '$type' => 7}}  # type 7 is an ObjectID (default for _id)      
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -83,14 +92,7 @@ module CanCan
 | 
				
			|||||||
      # internally uses Ability#conditions method, see that for more information.
 | 
					      # internally uses Ability#conditions method, see that for more information.
 | 
				
			||||||
      def accessible_by(ability, action = :read)        
 | 
					      def accessible_by(ability, action = :read)        
 | 
				
			||||||
        query = ability.query(action, self)     
 | 
					        query = ability.query(action, self)     
 | 
				
			||||||
           
 | 
					        where(query.conditions)
 | 
				
			||||||
        if query.conditions.blank?
 | 
					 | 
				
			||||||
          # this query is sure to return no results
 | 
					 | 
				
			||||||
          # we need this so there is a Mongoid::Criteria object to return, since an empty array would cause problems
 | 
					 | 
				
			||||||
          where({:_id => {'$exists' => false, '$type' => 7}})  # type 7 is an ObjectID (default for _id)
 | 
					 | 
				
			||||||
        else   
 | 
					 | 
				
			||||||
          where(query.conditions)
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
				
			|||||||
@ -55,7 +55,7 @@ describe CanCan::MongoidAdditions do
 | 
				
			|||||||
    @model_class.create :title  => 'Lord'
 | 
					    @model_class.create :title  => 'Lord'
 | 
				
			||||||
    @model_class.create :title  => 'Dude'
 | 
					    @model_class.create :title  => 'Dude'
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
    @model_class.accessible_by(@ability, :read).should == []
 | 
					    @model_class.accessible_by(@ability, :read).entries.should == []
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  it "should return the correct records based on the defined ability" do
 | 
					  it "should return the correct records based on the defined ability" do
 | 
				
			||||||
@ -67,6 +67,16 @@ describe CanCan::MongoidAdditions do
 | 
				
			|||||||
    @model_class.accessible_by(@ability, :read).should == [sir]
 | 
					    @model_class.accessible_by(@ability, :read).should == [sir]
 | 
				
			||||||
  end  
 | 
					  end  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it "should return everything when the defined ability is manage all" do
 | 
				
			||||||
 | 
					    @ability.can :manage, :all
 | 
				
			||||||
 | 
					    sir   = @model_class.create :title  => 'Sir'
 | 
				
			||||||
 | 
					    lord  = @model_class.create :title  => 'Lord'
 | 
				
			||||||
 | 
					    dude  = @model_class.create :title  => 'Dude'  
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    @model_class.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
 | 
				
			||||||
 | 
					  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
 | 
				
			||||||
      obj = @model_class.create :title  => 'Sir'
 | 
					      obj = @model_class.create :title  => 'Sir'
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user