Sha256: a24f21930be9cad700f5d423d9b06202eb007a3a08d4b7de6cc30e8953389be3

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

module CanCanNamespace
  # This class is used internally and should only be called through Ability.
  # it holds the information about a "can" call made on Ability and provides
  # helpful methods to determine permission checking and conditions hash generation.
  class Rule < ::CanCan::Rule
    # The first argument when initializing is the base_behavior which is a true/false
    # value. True for "can" and false for "cannot". The next two arguments are the action
    # and subject respectively (such as :read, @project). The third argument is a hash
    # of conditions and the last one is the block passed to the "can" call.
    def initialize(base_behavior, action, subject, conditions, block)
      if conditions.kind_of?(Hash) && conditions.has_key?(:context)
        @contexts = [conditions.delete(:context)].flatten.map(&:to_s)
        conditions = nil if conditions.keys.empty?
      else
        @contexts = []
      end
      
      super
    end
    
    def has_conditions?
      !conditions_empty? && @conditions.kind_of?(Hash)
    end
    
    # Matches both the subject and action, not necessarily the conditions
    def relevant?(action, subject, context = nil)
      subject = subject.values.first if subject.class == Hash
      @match_all || (matches_action?(action) && matches_subject?(subject) && matches_context(context))
    end
    
    def matches_context(context)
      (context.nil? && @contexts.empty?) || (context && @contexts.include?(context.to_s))
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/cancan_namespace-0.1.3/lib/cancan_namespace/rule.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/cancan_namespace-0.1.3/lib/cancan_namespace/rule.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/cancan_namespace-0.1.3/lib/cancan_namespace/rule.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/cancan_namespace-0.1.3/lib/cancan_namespace/rule.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/cancan_namespace-0.1.3/lib/cancan_namespace/rule.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/cancan_namespace-0.1.3/lib/cancan_namespace/rule.rb
cancan_namespace-0.1.3 lib/cancan_namespace/rule.rb
cancan_namespace-0.1.2 lib/cancan_namespace/rule.rb