Class: CanCan::Rule
- Inherits:
-
Object
- Object
- CanCan::Rule
- 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
-
#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.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#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
- #with_scope? ⇒ Boolean
Methods included from ParameterValidators
Methods included from Relevant
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.
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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
15 16 17 |
# File 'lib/cancan/rule.rb', line 15 def actions @actions end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
15 16 17 |
# File 'lib/cancan/rule.rb', line 15 def attributes @attributes end |
#base_behavior ⇒ Object (readonly)
Returns the value of attribute base_behavior.
15 16 17 |
# File 'lib/cancan/rule.rb', line 15 def base_behavior @base_behavior end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
15 16 17 |
# File 'lib/cancan/rule.rb', line 15 def block @block end |
#conditions ⇒ Object
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
16 17 18 |
# File 'lib/cancan/rule.rb', line 16 def (value) @expanded_actions = value end |
#subjects ⇒ Object (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_conditions ⇒ Object
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
51 52 53 |
# File 'lib/cancan/rule.rb', line 51 def can_rule? base_behavior end |
#cannot_catch_all? ⇒ Boolean
55 56 57 |
# File 'lib/cancan/rule.rb', line 55 def cannot_catch_all? !can_rule? && catch_all? end |
#catch_all? ⇒ 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 |
#inspect ⇒ Object
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
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
64 65 66 |
# File 'lib/cancan/rule.rb', line 64 def only_block? conditions_empty? && @block end |
#only_raw_sql? ⇒ 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
72 73 74 |
# File 'lib/cancan/rule.rb', line 72 def with_scope? defined?(ActiveRecord) && @conditions.is_a?(ActiveRecord::Relation) end |