Class: CanCan::Rule

Inherits:
Object
  • Object
show all
Includes:
ConditionsMatcher
Defined in:
lib/cancan/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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConditionsMatcher

#matches_conditions?

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.

Raises:



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cancan/rule.rb', line 15

def initialize(base_behavior, action, subject, conditions, block)
  both_block_and_hash_error = 'You are not able to supply a block with a hash of conditions in '\
                              "#{action} #{subject} ability. Use either one."
  raise Error, both_block_and_hash_error if conditions.is_a?(Hash) && block
  @match_all = action.nil? && subject.nil?
  @base_behavior = base_behavior
  @actions = Array(action)
  @subjects = Array(subject)
  @conditions = conditions || {}
  @block = block
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions



8
9
10
# File 'lib/cancan/rule.rb', line 8

def actions
  @actions
end

#base_behaviorObject (readonly)

Returns the value of attribute base_behavior



8
9
10
# File 'lib/cancan/rule.rb', line 8

def base_behavior
  @base_behavior
end

#conditionsObject (readonly)

Returns the value of attribute conditions



8
9
10
# File 'lib/cancan/rule.rb', line 8

def conditions
  @conditions
end

#expanded_actions=(value) ⇒ Object (writeonly)

Sets the attribute expanded_actions

Parameters:

  • value

    the value to set the attribute expanded_actions to.



9
10
11
# File 'lib/cancan/rule.rb', line 9

def expanded_actions=(value)
  @expanded_actions = value
end

#subjectsObject (readonly)

Returns the value of attribute subjects



8
9
10
# File 'lib/cancan/rule.rb', line 8

def subjects
  @subjects
end

Instance Method Details

#associations_hash(conditions = @conditions) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/cancan/rule.rb', line 46

def associations_hash(conditions = @conditions)
  hash = {}
  if conditions.is_a? Hash
    conditions.map do |name, value|
      hash[name] = associations_hash(value) if value.is_a? Hash
    end
  end
  hash
end

#attributes_from_conditionsObject



56
57
58
59
60
61
62
63
64
# File 'lib/cancan/rule.rb', line 56

def attributes_from_conditions
  attributes = {}
  if @conditions.is_a? Hash
    @conditions.each do |key, value|
      attributes[key] = value unless [Array, Range, Hash].include? value.class
    end
  end
  attributes
end

#only_block?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/cancan/rule.rb', line 33

def only_block?
  conditions_empty? && @block
end

#only_raw_sql?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cancan/rule.rb', line 37

def only_raw_sql?
  @block.nil? && !conditions_empty? && !@conditions.is_a?(Hash)
end

#relevant?(action, subject) ⇒ Boolean

Matches both the subject and action, not necessarily the conditions

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/cancan/rule.rb', line 28

def relevant?(action, subject)
  subject = subject.values.first if subject.class == Hash
  @match_all || (matches_action?(action) && matches_subject?(subject))
end

#unmergeable?Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/cancan/rule.rb', line 41

def unmergeable?
  @conditions.respond_to?(:keys) && @conditions.present? &&
    (!@conditions.keys.first.is_a? Symbol)
end