cleanup whitespace
This commit is contained in:
		
							parent
							
								
									bbb02f7c8f
								
							
						
					
					
						commit
						8628aa0038
					
				@ -9,18 +9,18 @@ module CanCan
 | 
			
		||||
        query_without_mongoid_support(action, subject)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    def query_with_mongoid_support(action, subject)
 | 
			
		||||
      MongoidQuery.new(subject, relevant_rules_for_query(action, subject))
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
  class MongoidQuery
 | 
			
		||||
    def initialize(sanitizer, rules)
 | 
			
		||||
      @sanitizer = sanitizer
 | 
			
		||||
      @rules = rules
 | 
			
		||||
    end    
 | 
			
		||||
    
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def conditions
 | 
			
		||||
      if @rules.size == 0
 | 
			
		||||
        false_query
 | 
			
		||||
@ -28,36 +28,36 @@ module CanCan
 | 
			
		||||
        @rules.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)      
 | 
			
		||||
      {:_id => {'$exists' => false, '$type' => 7}}  # type 7 is an ObjectID (default for _id)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # customize to handle Mongoid queries in ability definitions conditions
 | 
			
		||||
  # Mongoid Criteria are simpler to check than normal conditions hashes  
 | 
			
		||||
  # Mongoid Criteria are simpler to check than normal conditions hashes
 | 
			
		||||
  # When no conditions are given, true should be returned.
 | 
			
		||||
  # The default CanCan behavior relies on the fact that conditions.all? will return true when conditions is empty
 | 
			
		||||
  # The way ruby handles all? for empty hashes can be unexpected:
 | 
			
		||||
  #   {}.all?{|a| a == 5} 
 | 
			
		||||
  #   {}.all?{|a| a == 5}
 | 
			
		||||
  #   => true
 | 
			
		||||
  #   {}.all?{|a| a != 5} 
 | 
			
		||||
  #   {}.all?{|a| a != 5}
 | 
			
		||||
  #   => true
 | 
			
		||||
  class Rule
 | 
			
		||||
    def matches_conditions_hash_with_mongoid_subject?(subject, conditions = @conditions)          
 | 
			
		||||
      if defined?(::Mongoid) && subject.class.include?(::Mongoid::Document) && conditions.any?{|k,v| !k.kind_of?(Symbol)}      
 | 
			
		||||
        if conditions.empty?  
 | 
			
		||||
    def matches_conditions_hash_with_mongoid_subject?(subject, conditions = @conditions)
 | 
			
		||||
      if defined?(::Mongoid) && subject.class.include?(::Mongoid::Document) && conditions.any?{|k,v| !k.kind_of?(Symbol)}
 | 
			
		||||
        if conditions.empty?
 | 
			
		||||
          true
 | 
			
		||||
        else
 | 
			
		||||
          subject.class.where(conditions).include?(subject)  # just use Mongoid's where function
 | 
			
		||||
        end
 | 
			
		||||
      else 
 | 
			
		||||
      else
 | 
			
		||||
        matches_conditions_hash_without_mongoid_subject? subject, conditions
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
    # could use alias_method_chain, but it's not worth adding activesupport as a gem dependency    
 | 
			
		||||
 | 
			
		||||
    # could use alias_method_chain, but it's not worth adding activesupport as a gem dependency
 | 
			
		||||
    alias_method :matches_conditions_hash_without_mongoid_subject?, :matches_conditions_hash?
 | 
			
		||||
    alias_method :matches_conditions_hash?, :matches_conditions_hash_with_mongoid_subject?
 | 
			
		||||
  end
 | 
			
		||||
@ -71,40 +71,40 @@ module CanCan
 | 
			
		||||
      # is usually called from a controller and passed the +current_ability+.
 | 
			
		||||
      #
 | 
			
		||||
      #   @articles = Article.accessible_by(current_ability)
 | 
			
		||||
      # 
 | 
			
		||||
      #
 | 
			
		||||
      # Here only the articles which the user is able to read will be returned.
 | 
			
		||||
      # If the user does not have permission to read any articles then an empty
 | 
			
		||||
      # result is returned. Since this is a scope it can be combined with any
 | 
			
		||||
      # other scopes or pagination.
 | 
			
		||||
      # 
 | 
			
		||||
      #
 | 
			
		||||
      # An alternative action can optionally be passed as a second argument.
 | 
			
		||||
      # 
 | 
			
		||||
      #
 | 
			
		||||
      #   @articles = Article.accessible_by(current_ability, :update)
 | 
			
		||||
      # 
 | 
			
		||||
      #
 | 
			
		||||
      # Here only the articles which the user can update are returned. This
 | 
			
		||||
      # internally uses Ability#conditions method, see that for more information.
 | 
			
		||||
      def accessible_by(ability, action = :read)        
 | 
			
		||||
        query = ability.query(action, self)     
 | 
			
		||||
      def accessible_by(ability, action = :read)
 | 
			
		||||
        query = ability.query(action, self)
 | 
			
		||||
        where(query.conditions)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    def self.included(base)
 | 
			
		||||
      base.extend ClassMethods
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# Info on monkeypatching Mongoid : 
 | 
			
		||||
# Info on monkeypatching Mongoid :
 | 
			
		||||
# http://log.mazniak.org/post/719062325/monkey-patching-activesupport-concern-and-you#footer
 | 
			
		||||
if defined?(::Mongoid)
 | 
			
		||||
  module Mongoid
 | 
			
		||||
    module Components
 | 
			
		||||
      old_block = @_included_block
 | 
			
		||||
      @_included_block = Proc.new do 
 | 
			
		||||
      @_included_block = Proc.new do
 | 
			
		||||
        class_eval(&old_block) if old_block
 | 
			
		||||
        include CanCan::MongoidAdditions
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end  
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
  class MongoidProject
 | 
			
		||||
    include Mongoid::Document
 | 
			
		||||
    include CanCan::MongoidAdditions
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
    referenced_in :mongoid_category
 | 
			
		||||
 | 
			
		||||
    class << self
 | 
			
		||||
@ -42,7 +42,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
      before(:all) do
 | 
			
		||||
        @mongoid_class = Object.send(:remove_const, :Mongoid)
 | 
			
		||||
      end
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
      after(:all) do
 | 
			
		||||
        Object.const_set(:Mongoid, @mongoid_class)
 | 
			
		||||
      end
 | 
			
		||||
@ -51,12 +51,12 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
        stub(@model_class).scoped { :scoped_stub }
 | 
			
		||||
        @ability = Object.new
 | 
			
		||||
        @ability.extend(CanCan::Ability)
 | 
			
		||||
          
 | 
			
		||||
 | 
			
		||||
        @ability.can :read, @model_class
 | 
			
		||||
        lambda {    
 | 
			
		||||
          @ability.can? :read, @model_class.new 
 | 
			
		||||
        lambda {
 | 
			
		||||
          @ability.can? :read, @model_class.new
 | 
			
		||||
        }.should_not raise_error
 | 
			
		||||
      end    
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context "Mongoid defined" do
 | 
			
		||||
@ -69,8 +69,8 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
      after(:each) do
 | 
			
		||||
        Mongoid.master.collections.select do |collection|
 | 
			
		||||
          collection.name !~ /system/
 | 
			
		||||
        end.each(&:drop)    
 | 
			
		||||
      end    
 | 
			
		||||
        end.each(&:drop)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "should compare properties on mongoid documents with the conditions hash" do
 | 
			
		||||
        model = @model_class.new
 | 
			
		||||
@ -93,16 +93,16 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
        dude  = @model_class.create :title  => 'Dude'
 | 
			
		||||
 | 
			
		||||
        @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'  
 | 
			
		||||
        dude  = @model_class.create :title  => 'Dude'
 | 
			
		||||
 | 
			
		||||
        @model_class.accessible_by(@ability, :read).entries.should == [sir, lord, dude]
 | 
			
		||||
      end  
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      describe "Mongoid::Criteria where clause Symbol extensions using MongoDB expressions" do
 | 
			
		||||
@ -119,7 +119,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
        describe "activates only when there are Criteria in the hash" do
 | 
			
		||||
          it "Calls where on the model class when there are criteria" do
 | 
			
		||||
            obj = @model_class.create :title  => 'Bird'
 | 
			
		||||
            @conditions = {:title.nin => ["Fork", "Spoon"]}      
 | 
			
		||||
            @conditions = {:title.nin => ["Fork", "Spoon"]}
 | 
			
		||||
            mock(@model_class).where(@conditions) {[obj]}
 | 
			
		||||
            @ability.can :read, @model_class, @conditions
 | 
			
		||||
            @ability.should be_able_to(:read, obj)
 | 
			
		||||
@ -150,7 +150,7 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
 | 
			
		||||
          obj2 = @model_class.create :titles  => ['Palatin', 'Margrave', 'Marquis']
 | 
			
		||||
          @ability.can?(:read, obj2).should == false
 | 
			
		||||
        end    
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        it "should handle :field.exists" do
 | 
			
		||||
          obj = @model_class.create :titles  => ['Palatin', 'Margrave']
 | 
			
		||||
@ -170,12 +170,12 @@ if ENV["MODEL_ADAPTER"] == "mongoid"
 | 
			
		||||
 | 
			
		||||
          obj2 = @model_class.create :age  => 40
 | 
			
		||||
          @ability.can?(:read, obj2).should == false
 | 
			
		||||
        end    
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "should call where with matching ability conditions" do
 | 
			
		||||
        obj = @model_class.create :foo => {:bar => 1}
 | 
			
		||||
        @ability.can :read, @model_class, :foo => {:bar => 1}    
 | 
			
		||||
        @ability.can :read, @model_class, :foo => {:bar => 1}
 | 
			
		||||
        @model_class.accessible_by(@ability, :read).entries.first.should == obj
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user