refactoring ability can? method - closes #12

This commit is contained in:
Ryan Bates 2009-11-25 17:31:40 -08:00
parent d4405e6070
commit c40490d672

View File

@ -40,22 +40,13 @@ module CanCan
# assert ability.cannot?(:destroy, Project.new) # assert ability.cannot?(:destroy, Project.new)
# end # end
# #
def can?(original_action, target) # TODO this could use some refactoring def can?(action, noun) # TODO this could use some refactoring
(@can_history || []).reverse.each do |base_behavior, can_action, can_target, can_block| (@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block|
can_actions = [can_action].flatten defined_actions = expand_actions(defined_action)
can_targets = [can_target].flatten defined_nouns = [defined_noun].flatten
possible_actions_for(original_action).each do |action| if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun)
if (can_actions.include?(:manage) || can_actions.include?(action)) && (can_targets.include?(:all) || can_targets.include?(target) || can_targets.any? { |c| c.kind_of?(Class) && target.kind_of?(c) }) result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
if can_block.nil? return base_behavior ? result : !result
return base_behavior
else
block_args = []
block_args << action if can_actions.include?(:manage)
block_args << (target.class == Class ? target : target.class) if can_targets.include?(:all)
block_args << (target.class == Class ? nil : target)
return base_behavior ? can_block.call(*block_args) : !can_block.call(*block_args)
end
end
end end
end end
false false
@ -112,9 +103,9 @@ module CanCan
# can :read, :stats # can :read, :stats
# can? :read, :stats # => true # can? :read, :stats # => true
# #
def can(action, target, &block) def can(action, noun, &block)
@can_history ||= [] @can_definitions ||= []
@can_history << [true, action, target, block] @can_definitions << [true, action, noun, block]
end end
# Define an ability which cannot be done. Accepts the same arguments as "can". # Define an ability which cannot be done. Accepts the same arguments as "can".
@ -129,9 +120,9 @@ module CanCan
# product.invisible? # product.invisible?
# end # end
# #
def cannot(action, target, &block) def cannot(action, noun, &block)
@can_history ||= [] @can_definitions ||= []
@can_history << [false, action, target, block] @can_definitions << [false, action, noun, block]
end end
# Alias one or more actions into another one. # Alias one or more actions into another one.
@ -164,13 +155,16 @@ module CanCan
# #
# This way one can use params[:action] in the controller to determine the permission. # This way one can use params[:action] in the controller to determine the permission.
def alias_action(*args) def alias_action(*args)
@aliased_actions ||= default_alias_actions
target = args.pop[:to] target = args.pop[:to]
@aliased_actions[target] = args aliased_actions[target] = args
end end
private private
def aliased_actions
@aliased_actions ||= default_alias_actions
end
def default_alias_actions def default_alias_actions
{ {
:read => [:index, :show], :read => [:index, :show],
@ -179,12 +173,34 @@ module CanCan
} }
end end
def possible_actions_for(initial_action) def expand_actions(actions)
actions = [initial_action] [actions].flatten.map do |action|
(@aliased_actions || default_alias_actions).each do |target, aliases| if aliased_actions[action]
actions += possible_actions_for(target) if aliases.include? initial_action [action, *aliased_actions[action]]
else
action
end
end.flatten
end
def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
if defined_block.nil?
true
else
block_args = []
block_args << action if defined_actions.include?(:manage)
block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all)
block_args << (noun.class == Class ? nil : noun)
return defined_block.call(*block_args)
end end
actions end
def includes_action?(actions, action)
actions.include?(:manage) || actions.include?(action)
end
def includes_noun?(nouns, noun)
nouns.include?(:all) || nouns.include?(noun) || nouns.any? { |c| c.kind_of?(Class) && noun.kind_of?(c) }
end end
end end
end end