Permalink
Browse files

adding manage action which applies to everything

  • Loading branch information...
1 parent 0cfb8c7 commit 6c89c32059267cc4208874565222542b351e508d @ryanb committed Nov 16, 2009
Showing with 30 additions and 13 deletions.
  1. +6 −12 lib/cancan/ability.rb
  2. +24 −1 spec/cancan/ability_spec.rb
View
@@ -6,21 +6,15 @@ def self.included(base)
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
@@ -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
+
+class AdminAbility
+ include CanCan::Ability
+ can :manage, :all do |action, object_class, object|
+ [action, object_class, object]
+ end
end
-describe CanCan::Ability do
+describe Ability do
before(:each) do
@ability = Ability.new
end
@@ -37,4 +47,17 @@ class Ability
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

0 comments on commit 6c89c32

Please sign in to comment.