adding aliasing of actions
This commit is contained in:
parent
6c89c32059
commit
0b8b51b4fc
|
@ -4,30 +4,47 @@ module CanCan
|
||||||
base.extend ClassMethods
|
base.extend ClassMethods
|
||||||
end
|
end
|
||||||
|
|
||||||
def can?(action, target)
|
def can?(original_action, target)
|
||||||
self.class.can_history.reverse.each do |can_action, can_target, can_block|
|
(self.class.can_history || []).reverse.each do |can_action, can_target, can_block|
|
||||||
if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
|
possible_actions_for(original_action).each do |action|
|
||||||
if can_block.nil?
|
if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
|
||||||
return true
|
if can_block.nil?
|
||||||
else
|
return true
|
||||||
block_args = []
|
else
|
||||||
block_args << action if can_action == :manage
|
block_args = []
|
||||||
block_args << (target.class == Class ? target : target.class) if can_target == :all
|
block_args << action if can_action == :manage
|
||||||
block_args << (target.class == Class ? nil : target)
|
block_args << (target.class == Class ? target : target.class) if can_target == :all
|
||||||
return can_block.call(*block_args)
|
block_args << (target.class == Class ? nil : target)
|
||||||
|
return can_block.call(*block_args)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def possible_actions_for(initial_action)
|
||||||
|
actions = [initial_action]
|
||||||
|
(self.class.aliased_actions || []).each do |target, aliases|
|
||||||
|
actions += possible_actions_for(target) if aliases.include? initial_action
|
||||||
|
end
|
||||||
|
actions
|
||||||
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
attr_reader :can_history
|
attr_reader :can_history
|
||||||
|
attr_reader :aliased_actions
|
||||||
|
|
||||||
def can(action, target, &block)
|
def can(action, target, &block)
|
||||||
@can_history ||= []
|
@can_history ||= []
|
||||||
@can_history << [action, target, block]
|
@can_history << [action, target, block]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def alias_action(*args)
|
||||||
|
@aliased_actions ||= {}
|
||||||
|
target = args.pop[:to]
|
||||||
|
@aliased_actions[target] = args
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,8 @@ require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
class Ability
|
class Ability
|
||||||
include CanCan::Ability
|
include CanCan::Ability
|
||||||
|
alias_action :update, :destroy, :to => :modify
|
||||||
|
|
||||||
can :read, :all
|
can :read, :all
|
||||||
can :read, Symbol do |sym|
|
can :read, Symbol do |sym|
|
||||||
sym
|
sym
|
||||||
|
@ -12,6 +14,9 @@ class Ability
|
||||||
can :manage, Array do |action, object|
|
can :manage, Array do |action, object|
|
||||||
[action, object]
|
[action, object]
|
||||||
end
|
end
|
||||||
|
can :modify, :all do |object_class, object|
|
||||||
|
:modify_called
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class AdminAbility
|
class AdminAbility
|
||||||
|
@ -52,6 +57,11 @@ describe Ability do
|
||||||
@ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]]
|
@ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]]
|
||||||
@ability.can?(:stuff, Array).should == [:stuff, nil]
|
@ability.can?(:stuff, Array).should == [:stuff, nil]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should alias update or destroy actions to modify action" do
|
||||||
|
@ability.can?(:update, 123).should == :modify_called
|
||||||
|
@ability.can?(:destroy, 123).should == :modify_called
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe AdminAbility do
|
describe AdminAbility do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user