Class: CanCan::Rule

Inherits:
Object
  • Object
show all
Includes:
ConditionsMatcher, ParameterValidators, Relevant
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 ParameterValidators

#valid_attribute_param?

Methods included from Relevant

#relevant?

Methods included from ConditionsMatcher

#matches_conditions?

Constructor Details

#initialize(base_behavior, action, subject, *extra_args, &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:


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cancan/rule.rb', line 22

def initialize(base_behavior, action, subject, *extra_args, &block)
  # for backwards compatibility, attributes are an optional parameter. Check if
  # attributes were passed or are actually conditions
  attributes, extra_args = parse_attributes_from_extra_args(extra_args)
  condition_and_block_check(extra_args, block, action, subject)
  @match_all = action.nil? && subject.nil?
  raise Error, "Subject is required for #{action}" if action && subject.nil?

  @base_behavior = base_behavior
  @actions = wrap(action)
  @subjects = wrap(subject)
  @attributes = wrap(attributes)
  @conditions = extra_args || {}
  @block = block
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.


15
16
17
# File 'lib/cancan/rule.rb', line 15

def actions
  @actions
end

#attributesObject (readonly)

Returns the value of attribute attributes.


15
16
17
# File 'lib/cancan/rule.rb', line 15

def attributes
  @attributes
end

#base_behaviorObject (readonly)

Returns the value of attribute base_behavior.


15
16
17
# File 'lib/cancan/rule.rb', line 15

def base_behavior
  @base_behavior
end

#blockObject (readonly)

Returns the value of attribute block.


15
16
17
# File 'lib/cancan/rule.rb', line 15

def block
  @block
end

#conditionsObject

Returns the value of attribute conditions.


15
16
17
# File 'lib/cancan/rule.rb', line 15

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.


16
17
18
# File 'lib/cancan/rule.rb', line 16

def expanded_actions=(value)
  @expanded_actions = value
end

#subjectsObject (readonly)

Returns the value of attribute subjects.


15
16
17
# File 'lib/cancan/rule.rb', line 15

def subjects
  @subjects
end

Instance Method Details

#associations_hash(conditions = @conditions) ⇒ Object


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

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


86
87
88
89
90
91
92
93
94
# File 'lib/cancan/rule.rb', line 86

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

#can_rule?Boolean

Returns:

  • (Boolean)

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

def can_rule?
  base_behavior
end

#cannot_catch_all?Boolean

Returns:

  • (Boolean)

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

def cannot_catch_all?
  !can_rule? && catch_all?
end

#catch_all?Boolean

Returns:

  • (Boolean)

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

def catch_all?
  (with_scope? && @conditions.where_values_hash.empty?) ||
    (!with_scope? && [nil, false, [], {}, '', ' '].include?(@conditions))
end

#inspectObject


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cancan/rule.rb', line 38

def inspect
  repr = "#<#{self.class.name}"
  repr += "#{@base_behavior ? 'can' : 'cannot'} #{@actions.inspect}, #{@subjects.inspect}, #{@attributes.inspect}"

  if with_scope?
    repr += ", #{@conditions.where_values_hash}"
  elsif [Hash, String].include?(@conditions.class)
    repr += ", #{@conditions.inspect}"
  end

  repr + '>'
end

#matches_attributes?(attribute) ⇒ Boolean

Returns:

  • (Boolean)

96
97
98
99
100
101
# File 'lib/cancan/rule.rb', line 96

def matches_attributes?(attribute)
  return true if @attributes.empty?
  return @base_behavior if attribute.nil?

  @attributes.include?(attribute.to_sym)
end

#only_block?Boolean

Returns:

  • (Boolean)

64
65
66
# File 'lib/cancan/rule.rb', line 64

def only_block?
  conditions_empty? && @block
end

#only_raw_sql?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'lib/cancan/rule.rb', line 68

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

#with_scope?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/cancan/rule.rb', line 72

def with_scope?
  defined?(ActiveRecord) && @conditions.is_a?(ActiveRecord::Relation)
end