support arrays being passed to 'can' to specify multiple actions or classes - closes #2
This commit is contained in:
parent
4322da9d0a
commit
766fe86a9f
|
@ -1,3 +1,5 @@
|
||||||
|
* support arrays being passed to "can" to specify multiple actions or classes - see issue #2
|
||||||
|
|
||||||
* adding "cannot?" method to ability, controller, and view which is inverse of "can?" - see issue #1
|
* adding "cannot?" method to ability, controller, and view which is inverse of "can?" - see issue #1
|
||||||
|
|
||||||
* BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4
|
* BACKWARDS INCOMPATIBLE: use Ability#initialize instead of 'prepare' to set up abilities - see issue #4
|
||||||
|
|
|
@ -4,14 +4,16 @@ module CanCan
|
||||||
|
|
||||||
def can?(original_action, target) # TODO this could use some refactoring
|
def can?(original_action, target) # TODO this could use some refactoring
|
||||||
(@can_history || []).reverse.each do |can_action, can_target, can_block|
|
(@can_history || []).reverse.each do |can_action, can_target, can_block|
|
||||||
|
can_actions = [can_action].flatten
|
||||||
|
can_targets = [can_target].flatten
|
||||||
possible_actions_for(original_action).each do |action|
|
possible_actions_for(original_action).each do |action|
|
||||||
if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target))
|
if (can_actions.include?(:manage) || can_actions.include?(action)) && (can_targets.include?(:all) || can_targets.include?(target) || can_targets.any? { |c| target.kind_of?(c) })
|
||||||
if can_block.nil?
|
if can_block.nil?
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
block_args = []
|
block_args = []
|
||||||
block_args << action if can_action == :manage
|
block_args << action if can_actions.include?(:manage)
|
||||||
block_args << (target.class == Class ? target : target.class) if can_target == :all
|
block_args << (target.class == Class ? target : target.class) if can_targets.include?(:all)
|
||||||
block_args << (target.class == Class ? nil : target)
|
block_args << (target.class == Class ? nil : target)
|
||||||
return can_block.call(*block_args)
|
return can_block.call(*block_args)
|
||||||
end
|
end
|
||||||
|
|
|
@ -85,4 +85,18 @@ describe CanCan::Ability do
|
||||||
it "should offer cannot? method which is simply invert of can?" do
|
it "should offer cannot? method which is simply invert of can?" do
|
||||||
@ability.cannot?(:tie, String).should be_true
|
@ability.cannot?(:tie, String).should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should be able to specify multiple actions and match any" do
|
||||||
|
@ability.can [:read, :update], :all
|
||||||
|
@ability.can?(:read, 123).should be_true
|
||||||
|
@ability.can?(:update, 123).should be_true
|
||||||
|
@ability.can?(:count, 123).should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be able to specify multiple classes and match any" do
|
||||||
|
@ability.can :update, [String, Array]
|
||||||
|
@ability.can?(:update, "foo").should be_true
|
||||||
|
@ability.can?(:update, []).should be_true
|
||||||
|
@ability.can?(:update, 123).should be_false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user