Class: CanCanNamespace::Rule

Inherits:
CanCan::Rule
  • Object
show all
Defined in:
lib/cancan_namespace/rule.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(base_behavior, action, subject, conditions, block) ⇒ 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.



10
11
12
13
14
15
16
17
18
19
# File 'lib/cancan_namespace/rule.rb', line 10

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

Instance Method Details

#has_conditions?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/cancan_namespace/rule.rb', line 21

def has_conditions?
  !conditions_empty? && @conditions.kind_of?(Hash)
end

#matches_context(context) ⇒ Object



31
32
33
# File 'lib/cancan_namespace/rule.rb', line 31

def matches_context(context)
  (context.nil? && @contexts.empty?) || (context && @contexts.include?(context.to_s))
end

#relevant?(action, subject, context = nil) ⇒ Boolean

Matches both the subject and action, not necessarily the conditions

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/cancan_namespace/rule.rb', line 26

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