Module: CanCanNamespace::Ability

Includes:
CanCan::Ability
Defined in:
lib/cancan_namespace/ability.rb

Overview

This module is designed to be included into an Ability class. This will provide the “can” methods for defining and checking abilities.

class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject

Returns the value of attribute context.



21
22
23
# File 'lib/cancan_namespace/ability.rb', line 21

def context
  @context
end

Instance Method Details

#can(action = nil, subject = nil, conditions = nil, &block) ⇒ Object



35
36
37
# File 'lib/cancan_namespace/ability.rb', line 35

def can(action = nil, subject = nil, conditions = nil, &block)
  rules << CanCanNamespace::Rule.new(true, action, subject, conditions, block)
end

#can?(action, subject, *extra_args) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cancan_namespace/ability.rb', line 23

def can?(action, subject, *extra_args)
  context = @context
  if extra_args.last.kind_of?(Hash) && extra_args.last.has_key?(:context)
    context = extra_args.pop[:context]
  end
  
  match = relevant_rules_for_match(action, subject, context).detect do |rule|
    rule.matches_conditions?(action, subject, extra_args)
  end
  match ? match.base_behavior : false
end

#cannot(action = nil, subject = nil, conditions = nil, &block) ⇒ Object



39
40
41
# File 'lib/cancan_namespace/ability.rb', line 39

def cannot(action = nil, subject = nil, conditions = nil, &block)
  rules << CanCanNamespace::Rule.new(false, action, subject, conditions, block)
end