Browse files

Default to :all when action is provided

Previously, it silently failed and threw an AccessDenied exception in the controller. This provides a sensible default.
  • Loading branch information...
1 parent b4285ae commit 582ef5f1045b6be54577e805fafb40461014726c @mikepack committed Sep 26, 2012
Showing with 9 additions and 0 deletions.
  1. +2 −0 lib/cancan/rule.rb
  2. +7 −0 spec/cancan/ability_spec.rb
@@ -12,6 +12,8 @@ class Rule # :nodoc:
# of conditions and the last one is the block passed to the "can" call.
def initialize(base_behavior, action, subject, conditions, block)
raise Error, "You are not able to supply a block with a hash of conditions in #{action} #{subject} ability. Use either one." if conditions.kind_of?(Hash) && !block.nil?
+ subject = :all if !action.nil? && subject.nil?
@match_all = action.nil? && subject.nil?
@base_behavior = base_behavior
@actions = [action].flatten
@@ -394,6 +394,13 @@ class Container < Hash; end
}.should raise_error(CanCan::Error, "You are not able to supply a block with a hash of conditions in read Array ability. Use either one.")
+ it "should default subjects to :all if action is provided" do
+ @ability.can :count
+ @ability.can?(:count, 123).should be_true
+ @ability.can?(:count, String).should be_true
+ @ability.can?(:count, :anything).should be_true
+ end
describe "unauthorized message" do
after(:each) do
I18n.backend = nil

0 comments on commit 582ef5f

Please sign in to comment.