support custom objects (usually symbols) in can definition - closes #8

This commit is contained in:
Ryan Bates 2009-11-25 09:55:50 -08:00
parent 5bd1a85410
commit e60365505c
3 changed files with 16 additions and 1 deletions

View File

@ -1,3 +1,5 @@
* support custom objects (usually symbols) in can definition - see issue #8
0.2.0 (Nov 17, 2009) 0.2.0 (Nov 17, 2009)
* fix behavior of load_and_authorize_resource for namespaced controllers - see issue #3 * fix behavior of load_and_authorize_resource for namespaced controllers - see issue #3

View File

@ -45,7 +45,7 @@ module CanCan
can_actions = [can_action].flatten can_actions = [can_action].flatten
can_targets = [can_target].flatten can_targets = [can_target].flatten
possible_actions_for(original_action).each do |action| possible_actions_for(original_action).each do |action|
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_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) })
if can_block.nil? if can_block.nil?
return true return true
else else
@ -106,6 +106,12 @@ module CanCan
# action != :destroy # action != :destroy
# end # end
# #
# You can pass custom objects into this "can" method, this is usually done through a symbol
# and is useful if a class isn't available to define permissions on.
#
# can :read, :stats
# can? :read, :stats # => true
#
def can(action, target, &block) def can(action, target, &block)
@can_history ||= [] @can_history ||= []
@can_history << [action, target, block] @can_history << [action, target, block]

View File

@ -99,4 +99,11 @@ describe CanCan::Ability do
@ability.can?(:update, []).should be_true @ability.can?(:update, []).should be_true
@ability.can?(:update, 123).should be_false @ability.can?(:update, 123).should be_false
end end
it "should support custom objects in the can definition" do
@ability.can :read, :stats
@ability.can?(:read, :stats).should be_true
@ability.can?(:update, :stats).should be_false
@ability.can?(:read, :nonstats).should be_false
end
end end