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
# File 'lib/cancan/rule.rb', line 13

def initialize(base_behavior, action, subject, conditions, block)
  raise Error, "You are not able to supply a block with a hash of conditions in #{action} #{subject} ability. Use either one." if conditions.kind_of?(Hash) && !block.nil?
  @match_all = action.nil? && subject.nil?
  @base_behavior = base_behavior
  @actions = [action].flatten
  @subjects = [subject].flatten
  @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



62
63
64
65
66
67
68
# File 'lib/cancan/rule.rb', line 62

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

#attributes_from_conditionsObject



70
71
72
73
74
75
76
# File 'lib/cancan/rule.rb', line 70

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

#conditions_empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cancan/rule.rb', line 53

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

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

Matches the block or conditions hash

Returns:

  • (Boolean)


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

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.kind_of?(Hash) && subject.class == Hash
    nested_subject_matches_conditions?(subject)
  elsif @conditions.kind_of?(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)


45
46
47
# File 'lib/cancan/rule.rb', line 45

def only_block?
  conditions_empty? && !@block.nil?
end

#only_raw_sql?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/cancan/rule.rb', line 49

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

#relevant?(action, subject) ⇒ Boolean

Matches both the subject and action, not necessarily the conditions

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/cancan/rule.rb', line 24

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)


57
58
59
60
# File 'lib/cancan/rule.rb', line 57

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