From 6c89c32059267cc4208874565222542b351e508d Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Mon, 16 Nov 2009 14:42:41 -0800 Subject: [PATCH] adding manage action which applies to everything --- lib/cancan/ability.rb | 18 ++++++------------ spec/cancan/ability_spec.rb | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index a219d22..7ae8f23 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -6,21 +6,15 @@ module CanCan def can?(action, target) self.class.can_history.reverse.each do |can_action, can_target, can_block| - if can_action == action && (can_target == :all || can_target == target || target.kind_of?(can_target)) + if (can_action == :manage || can_action == action) && (can_target == :all || can_target == target || target.kind_of?(can_target)) if can_block.nil? return true else - if can_target == :all - if target.class == Class - return can_block.call(target, nil) - else - return can_block.call(target.class, target) - end - elsif can_target == target - return can_block.call(nil) - else - return can_block.call(target) - end + block_args = [] + block_args << action if can_action == :manage + block_args << (target.class == Class ? target : target.class) if can_target == :all + block_args << (target.class == Class ? nil : target) + return can_block.call(*block_args) end end end diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 1c6b6f7..c31d57e 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -9,9 +9,19 @@ class Ability can :preview, :all do |object_class, object| [object_class, object] end + can :manage, Array do |action, object| + [action, object] + end end -describe CanCan::Ability do +class AdminAbility + include CanCan::Ability + can :manage, :all do |action, object_class, object| + [action, object_class, object] + end +end + +describe Ability do before(:each) do @ability = Ability.new end @@ -37,4 +47,17 @@ describe CanCan::Ability do it "should pass class with no object if :all objects are accepted and class is passed directly" do @ability.can?(:preview, Hash).should == [Hash, nil] end + + it "should pass action and object for global manage actions" do + @ability.can?(:stuff, [1, 2]).should == [:stuff, [1, 2]] + @ability.can?(:stuff, Array).should == [:stuff, nil] + end +end + +describe AdminAbility do + it "should return block result for action, object_class, and object for any action" do + @ability = AdminAbility.new + @ability.can?(:foo, 123).should == [:foo, Fixnum, 123] + @ability.can?(:bar, Fixnum).should == [:bar, Fixnum, nil] + end end