Class: CanCan::Rule

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.

Constructor Details

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.

# File 'lib/cancan/rule.rb', line 13definitialize(base_behavior,action,subject,conditions,block)raiseError,"You are not able to supply a block with a hash of conditions in #{action}#{subject} ability. Use either one."ifconditions.kind_of?(Hash)&&!block.nil?@match_all=action.nil?&&subject.nil?@base_behavior=base_behavior@actions=[action].flatten@subjects=[subject].flatten@conditions=conditions||{}@block=blockend

#conditions_empty? ⇒ Boolean

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

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 30defmatches_conditions?(action,subject,extra_args)if@match_allcall_block_with_all(action,subject,extra_args)elsif@block&&!subject_class?(subject)@block.call(subject,*extra_args)elsif@conditions.kind_of?(Hash)&&subject.class==Hashnested_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_behaviorendend