From 69f7a659145355dbcc88635acd6e1b722432d2f3 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Thu, 15 Apr 2010 11:21:44 -0700 Subject: [PATCH] support additional arguments to can? which get passed to the block - closes #48 --- CHANGELOG.rdoc | 5 +++++ lib/cancan/ability.rb | 7 ++++--- spec/cancan/ability_spec.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 76fde95..4d73c53 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,8 @@ +1.1.0 (not released) + +* Support additional arguments to can? which get passed to the block - see issue #48 + + 1.0.2 (Dec 30, 2009) * Adding clear_aliased_actions to Ability which removes previously defined actions including defaults - see issue #20 diff --git a/lib/cancan/ability.rb b/lib/cancan/ability.rb index 702075e..b3ffa09 100644 --- a/lib/cancan/ability.rb +++ b/lib/cancan/ability.rb @@ -40,12 +40,12 @@ module CanCan # assert ability.cannot?(:destroy, Project.new) # end # - def can?(action, noun) + def can?(action, noun, *extra_args) (@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block| defined_actions = expand_actions(defined_action) defined_nouns = [defined_noun].flatten if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun) - result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block) + result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block, extra_args) return base_behavior ? result : !result end end @@ -190,7 +190,7 @@ module CanCan end.flatten end - def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block) + def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block, extra_args) if defined_block.nil? true else @@ -198,6 +198,7 @@ module CanCan block_args << action if defined_actions.include?(:manage) block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all) block_args << (noun.class == Class ? nil : noun) + block_args += extra_args return defined_block.call(*block_args) end end diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 20bc1a9..e436b79 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -132,4 +132,12 @@ describe CanCan::Ability do @ability.clear_aliased_actions @ability.aliased_actions[:modify].should be_nil end + + it "should pass additional arguments to block from can?" do + @ability.can :read, Integer do |int, x| + int > x + end + @ability.can?(:read, 2, 1).should be_true + @ability.can?(:read, 2, 3).should be_false + end end