Module: CanCan::Ability::Actions

Included in:
CanCan::Ability
Defined in:
lib/cancan/ability/actions.rb

Instance Method Summary collapse

Instance Method Details

#alias_action(*args) ⇒ Object

Alias one or more actions into another one.

alias_action :update, :destroy, :to => :modify
can :modify, Comment

Then :modify permission will apply to both :update and :destroy requests.

can? :update, Comment # => true
can? :destroy, Comment # => true

This only works in one direction. Passing the aliased action into the “can?” call will not work because aliases are meant to generate more generic actions.

alias_action :update, :destroy, :to => :modify
can :update, Comment
can? :modify, Comment # => false

Unless that exact alias is used.

can :modify, Comment
can? :modify, Comment # => true

The following aliases are added by default for conveniently mapping common controller actions.

alias_action :index, :show, :to => :read
alias_action :new, :to => :create
alias_action :edit, :to => :update

This way one can use params in the controller to determine the permission.


35
36
37
38
39
40
# File 'lib/cancan/ability/actions.rb', line 35

def alias_action(*args)
  target = args.pop[:to]
  validate_target(target)
  aliased_actions[target] ||= []
  aliased_actions[target] += args
end

#aliased_actionsObject

Returns a hash of aliased actions. The key is the target and the value is an array of actions aliasing the key.


43
44
45
# File 'lib/cancan/ability/actions.rb', line 43

def aliased_actions
  @aliased_actions ||= default_alias_actions
end

#clear_aliased_actionsObject

Removes previously aliased actions including the defaults.


48
49
50
# File 'lib/cancan/ability/actions.rb', line 48

def clear_aliased_actions
  @aliased_actions = {}
end