Class: CanCan::Rule
- Inherits:
-
Object
- Object
- CanCan::Rule
- Includes:
- ConditionsMatcher, ParameterValidators
- 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
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#base_behavior ⇒ Object
readonly
Returns the value of attribute base_behavior.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#expanded_actions ⇒ Object
writeonly
Sets the attribute expanded_actions.
-
#subjects ⇒ Object
readonly
Returns the value of attribute subjects.
Instance Method Summary collapse
- #associations_hash(conditions = @conditions) ⇒ Object
- #attributes_from_conditions ⇒ Object
- #can_rule? ⇒ Boolean
- #cannot_catch_all? ⇒ Boolean
- #catch_all? ⇒ Boolean
-
#initialize(base_behavior, action, subject, *extra_args, &block) ⇒ Rule
constructor
The first argument when initializing is the base_behavior which is a true/false value.
- #inspect ⇒ Object
- #matches_attributes?(attribute) ⇒ Boolean
- #only_block? ⇒ Boolean
- #only_raw_sql? ⇒ Boolean
-
#relevant?(action, subject) ⇒ Boolean
Matches both the action, subject, and attribute, not necessarily the conditions.
- #with_scope? ⇒ Boolean
Methods included from ParameterValidators
Methods included from ConditionsMatcher
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.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cancan/rule.rb', line 18 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 = Array(action) @subjects = Array(subject) @attributes = Array(attributes) @conditions = extra_args || {} @block = block end |
Instance Attribute Details
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
11 12 13 |
# File 'lib/cancan/rule.rb', line 11 def actions @actions end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/cancan/rule.rb', line 11 def attributes @attributes end |
#base_behavior ⇒ Object (readonly)
Returns the value of attribute base_behavior.
11 12 13 |
# File 'lib/cancan/rule.rb', line 11 def base_behavior @base_behavior end |
#conditions ⇒ Object
Returns the value of attribute conditions.
11 12 13 |
# File 'lib/cancan/rule.rb', line 11 def conditions @conditions end |
#expanded_actions=(value) ⇒ Object (writeonly)
Sets the attribute expanded_actions
12 13 14 |
# File 'lib/cancan/rule.rb', line 12 def (value) @expanded_actions = value end |
#subjects ⇒ Object (readonly)
Returns the value of attribute subjects.
11 12 13 |
# File 'lib/cancan/rule.rb', line 11 def subjects @subjects end |
Instance Method Details
#associations_hash(conditions = @conditions) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/cancan/rule.rb', line 78 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_conditions ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/cancan/rule.rb', line 88 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
47 48 49 |
# File 'lib/cancan/rule.rb', line 47 def can_rule? base_behavior end |
#cannot_catch_all? ⇒ Boolean
51 52 53 |
# File 'lib/cancan/rule.rb', line 51 def cannot_catch_all? !can_rule? && catch_all? end |
#catch_all? ⇒ Boolean
55 56 57 58 |
# File 'lib/cancan/rule.rb', line 55 def catch_all? (with_scope? && @conditions.where_values_hash.empty?) || (!with_scope? && [nil, false, [], {}, '', ' '].include?(@conditions)) end |
#inspect ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cancan/rule.rb', line 34 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
98 99 100 101 102 103 |
# File 'lib/cancan/rule.rb', line 98 def matches_attributes?(attribute) return true if @attributes.empty? return @base_behavior if attribute.nil? @attributes.include?(attribute.to_sym) end |
#only_block? ⇒ Boolean
66 67 68 |
# File 'lib/cancan/rule.rb', line 66 def only_block? conditions_empty? && @block end |
#only_raw_sql? ⇒ Boolean
70 71 72 |
# File 'lib/cancan/rule.rb', line 70 def only_raw_sql? @block.nil? && !conditions_empty? && !@conditions.is_a?(Hash) end |
#relevant?(action, subject) ⇒ Boolean
Matches both the action, subject, and attribute, not necessarily the conditions
61 62 63 64 |
# File 'lib/cancan/rule.rb', line 61 def relevant?(action, subject) subject = subject.values.first if subject.class == Hash @match_all || (matches_action?(action) && matches_subject?(subject)) end |
#with_scope? ⇒ Boolean
74 75 76 |
# File 'lib/cancan/rule.rb', line 74 def with_scope? @conditions.is_a?(ActiveRecord::Relation) end |