Class: CanCan::Rule
- Inherits:
-
Object
- Object
- CanCan::Rule
- 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
:nodoc:.
-
#base_behavior ⇒ Object
readonly
:nodoc:.
-
#conditions ⇒ Object
readonly
:nodoc:.
-
#expanded_actions ⇒ Object
writeonly
Sets the attribute expanded_actions.
-
#subjects ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #associations_hash(conditions = @conditions) ⇒ Object
- #attributes_from_conditions ⇒ Object
- #conditions_empty? ⇒ Boolean
-
#initialize(base_behavior, action, subject, conditions, block) ⇒ Rule
constructor
The first argument when initializing is the base_behavior which is a true/false value.
-
#matches_conditions?(action, subject, extra_args) ⇒ Boolean
Matches the block or conditions hash.
- #only_block? ⇒ Boolean
- #only_raw_sql? ⇒ Boolean
-
#relevant?(action, subject) ⇒ Boolean
Matches both the subject and action, not necessarily the conditions.
Constructor Details
#initialize(base_behavior, action, subject, conditions, 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.
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
#actions ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/cancan/rule.rb', line 6 def actions @actions end |
#base_behavior ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/cancan/rule.rb', line 6 def base_behavior @base_behavior end |
#conditions ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/cancan/rule.rb', line 6 def conditions @conditions end |
#expanded_actions=(value) ⇒ Object (writeonly)
Sets the attribute expanded_actions
7 8 9 |
# File 'lib/cancan/rule.rb', line 7 def (value) @expanded_actions = value end |
#subjects ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/cancan/rule.rb', line 6 def subjects @subjects end |
Instance Method Details
#associations_hash(conditions = @conditions) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/cancan/rule.rb', line 57 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 |
#attributes_from_conditions ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/cancan/rule.rb', line 65 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 |
#conditions_empty? ⇒ Boolean
53 54 55 |
# File 'lib/cancan/rule.rb', line 53 def conditions_empty? @conditions == {} || @conditions.nil? end |
#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 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 |
#only_block? ⇒ Boolean
45 46 47 |
# File 'lib/cancan/rule.rb', line 45 def only_block? conditions_empty? && !@block.nil? end |
#only_raw_sql? ⇒ Boolean
49 50 51 |
# File 'lib/cancan/rule.rb', line 49 def only_raw_sql? @block.nil? && !conditions_empty? && !@conditions.kind_of?(Hash) end |
#relevant?(action, subject) ⇒ Boolean
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 |