Class: CanCan::Rule

Inherits:
Object
  • Object
show all
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

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:



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

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)

:nodoc:



6
7
8
# File 'lib/cancan/rule.rb', line 6

def actions
  @actions
end

#base_behaviorObject (readonly)

:nodoc:



6
7
8
# File 'lib/cancan/rule.rb', line 6

def base_behavior
  @base_behavior
end

#conditionsObject (readonly)

:nodoc:



6
7
8
# File 'lib/cancan/rule.rb', line 6

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.



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

def expanded_actions=(value)
  @expanded_actions = value
end

#subjectsObject (readonly)

:nodoc:



6
7
8
# File 'lib/cancan/rule.rb', line 6

def subjects
  @subjects
end

Instance Method Details

#associations_hash(conditions = @conditions) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/cancan/rule.rb', line 64

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



74
75
76
77
78
79
80
81
82
# File 'lib/cancan/rule.rb', line 74

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

#conditions_empty?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cancan/rule.rb', line 55

def conditions_empty?
  @conditions == {} || @conditions.nil?
end

#matches_conditions?(action, subject, extra_args) ⇒ Boolean

Matches the block or conditions hash

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cancan/rule.rb', line 32

def matches_conditions?(action, subject, extra_args)
  if @match_all
    call_block_with_all(action, subject, extra_args)
  elsif @block && !subject_class?(subject)
    @block.call(subject, *extra_args)
  elsif @conditions.is_a?(Hash) && subject.class == Hash
    nested_subject_matches_conditions?(subject)
  elsif @conditions.is_a?(Hash) && !subject_class?(subject)
    matches_conditions_hash?(subject)
  else
    # Don't stop at "cannot" definitions when there are conditions.
    conditions_empty? ? true : @base_behavior
  end
end

#only_block?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cancan/rule.rb', line 47

def only_block?
  conditions_empty? && @block
end

#only_raw_sql?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/cancan/rule.rb', line 51

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)


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

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)


59
60
61
62
# File 'lib/cancan/rule.rb', line 59

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