Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 69f7a65

Browse files
committed
support additional arguments to can? which get passed to the block - closes #48
1 parent f027b2e commit 69f7a65

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

CHANGELOG.rdoc

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
1.1.0 (not released)
2+
3+
* Support additional arguments to can? which get passed to the block - see issue #48
4+
5+
16
1.0.2 (Dec 30, 2009)
27

38
* Adding clear_aliased_actions to Ability which removes previously defined actions including defaults - see issue #20

lib/cancan/ability.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ module Ability
4040
# assert ability.cannot?(:destroy, Project.new)
4141
# end
4242
#
43-
def can?(action, noun)
43+
def can?(action, noun, *extra_args)
4444
(@can_definitions || []).reverse.each do |base_behavior, defined_action, defined_noun, defined_block|
4545
defined_actions = expand_actions(defined_action)
4646
defined_nouns = [defined_noun].flatten
4747
if includes_action?(defined_actions, action) && includes_noun?(defined_nouns, noun)
48-
result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
48+
result = can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block, extra_args)
4949
return base_behavior ? result : !result
5050
end
5151
end
@@ -190,14 +190,15 @@ def expand_actions(actions)
190190
end.flatten
191191
end
192192

193-
def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block)
193+
def can_perform_action?(action, noun, defined_actions, defined_nouns, defined_block, extra_args)
194194
if defined_block.nil?
195195
true
196196
else
197197
block_args = []
198198
block_args << action if defined_actions.include?(:manage)
199199
block_args << (noun.class == Class ? noun : noun.class) if defined_nouns.include?(:all)
200200
block_args << (noun.class == Class ? nil : noun)
201+
block_args += extra_args
201202
return defined_block.call(*block_args)
202203
end
203204
end

spec/cancan/ability_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,12 @@
132132
@ability.clear_aliased_actions
133133
@ability.aliased_actions[:modify].should be_nil
134134
end
135+
136+
it "should pass additional arguments to block from can?" do
137+
@ability.can :read, Integer do |int, x|
138+
int > x
139+
end
140+
@ability.can?(:read, 2, 1).should be_true
141+
@ability.can?(:read, 2, 3).should be_false
142+
end
135143
end

0 commit comments

Comments
 (0)