Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

raise an error when trying to make a rule with both hash conditions a…

…nd a block - closes #269
  • Loading branch information...
commit 0de43c445b0b81bf59ca1737edf38f6f38e30621 1 parent f9b181a
Ryan Bates authored
Showing with 9 additions and 0 deletions.
  1. +1 −0  lib/cancan/rule.rb
  2. +8 −0 spec/cancan/ability_spec.rb
1  lib/cancan/rule.rb
@@ -11,6 +11,7 @@ class Rule # :nodoc:
# and subject respectively (such as :read, @project). The third argument is a hash
# 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?
@match_all = action.nil? && subject.nil?
@base_behavior = base_behavior
@actions = [action].flatten
8 spec/cancan/ability_spec.rb
@@ -357,6 +357,14 @@ class Container < Hash; end
@ability.model_adapter(model_class, :read).should == :adapter_instance
+ it "should raise an error when attempting to use a block with a hash condition since it's not likely what they want" do
+ lambda {
+ @ability.can :read, Array, :published => true do
+ false
+ 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.")
+ end
describe "unauthorized message" do
after(:each) do
I18n.backend = nil
Please sign in to comment.
Something went wrong with that request. Please try again.