Class: CanCan::CanDefinition
- Inherits:
-
Object
- Object
- CanCan::CanDefinition
- Includes:
- ActiveSupport::Inflector
- Defined in:
- lib/cancan/can_definition.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
-
#block ⇒ Object
readonly
Returns the value of attribute block.
Instance Method Summary collapse
- #association_joins(conditions = @conditions) ⇒ Object
- #can?(action, subject, extra_args) ⇒ Boolean
-
#conditions(options = {}) ⇒ Object
Returns a hash of conditions.
-
#expand_actions(aliased_actions) ⇒ Object
Accepts a hash of aliased actions and returns an array of actions which match.
-
#initialize(base_behavior, action, subject, conditions, block) ⇒ CanDefinition
constructor
The first argument when initializing is the base_behavior which is a true/false value.
- #matches?(action, subject) ⇒ Boolean
Constructor Details
#initialize(base_behavior, action, subject, conditions, block) ⇒ CanDefinition
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 |
# File 'lib/cancan/can_definition.rb', line 13 def initialize(base_behavior, action, subject, conditions, block) @base_behavior = base_behavior @actions = [action].flatten @subjects = [subject].flatten @conditions = conditions || {} @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
7 8 9 |
# File 'lib/cancan/can_definition.rb', line 7 def block @block end |
Instance Method Details
#association_joins(conditions = @conditions) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cancan/can_definition.rb', line 53 def association_joins(conditions = @conditions) joins = [] conditions.each do |name, value| if value.kind_of? Hash nested = association_joins(value) if nested joins << {name => nested} else joins << name end end end joins unless joins.empty? end |
#can?(action, subject, extra_args) ⇒ Boolean
34 35 36 37 |
# File 'lib/cancan/can_definition.rb', line 34 def can?(action, subject, extra_args) result = can_without_base_behavior?(action, subject, extra_args) @base_behavior ? result : !result end |
#conditions(options = {}) ⇒ Object
Returns a hash of conditions. If the “:tableize => true” option is passed it will pluralize the association conditions to match the table name.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cancan/can_definition.rb', line 41 def conditions( = {}) if [:tableize] && @conditions.kind_of?(Hash) @conditions.inject({}) do |tableized_conditions, (name, value)| name = tableize(name).to_sym if value.kind_of? Hash tableized_conditions[name] = value tableized_conditions end else @conditions end end |
#expand_actions(aliased_actions) ⇒ Object
Accepts a hash of aliased actions and returns an array of actions which match. This should be called before “matches?” and other checking methods since they rely on the actions to be expanded.
24 25 26 27 28 |
# File 'lib/cancan/can_definition.rb', line 24 def (aliased_actions) @expanded_actions = @actions.map do |action| aliased_actions[action] ? [action, *aliased_actions[action]] : action end.flatten end |
#matches?(action, subject) ⇒ Boolean
30 31 32 |
# File 'lib/cancan/can_definition.rb', line 30 def matches?(action, subject) matches_action?(action) && matches_subject?(subject) end |