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

- (Rule) initialize(base_behavior, action, subject, conditions, block)

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

- (Object) actions (readonly)

:nodoc:



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

def actions
  @actions
end

- (Object) base_behavior (readonly)

:nodoc:



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

def base_behavior
  @base_behavior
end

- (Object) conditions (readonly)

:nodoc:



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

def conditions
  @conditions
end

- (Object) expanded_actions=(value) (writeonly)

Sets the attribute expanded_actions



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

def expanded_actions=(value)
  @expanded_actions = value
end

- (Object) subjects (readonly)

:nodoc:



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

def subjects
  @subjects
end

Instance Method Details

- (Object) associations_hash(conditions = @conditions)



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

- (Object) attributes_from_conditions



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

- (Boolean) conditions_empty?



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

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

- (Boolean) matches_conditions?(action, subject, extra_args)

Matches the block or conditions hash



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

- (Boolean) only_block?



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

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

- (Boolean) only_raw_sql?



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

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

- (Boolean) relevant?(action, subject)

Matches both the subject and action, not necessarily the conditions



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

- (Boolean) unmergeable?



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