Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix method reference block regression #583

Merged
merged 1 commit into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.0.1

* [#583](https://github.com/CanCanCommunity/cancancan/pull/583): Fix regression when using a method reference block. ([@coorasse][])

## 3.0.0

Please read the [guide on migrating from CanCanCan 2.x to 3.0](https://github.com/CanCanCommunity/cancancan/wiki/Migrating-from-CanCanCan-2.x-to-3.0)
Expand Down
2 changes: 1 addition & 1 deletion lib/cancan/conditions_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def subject_class?(subject)
def matches_block_conditions(subject, *extra_args)
return @base_behavior if subject_class?(subject)

@block.call(subject, *extra_args)
@block.call(subject, *extra_args.compact)
end

def matches_non_block_conditions(subject)
Expand Down
32 changes: 32 additions & 0 deletions spec/cancan/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,38 @@ class Container < Hash
expect(@ability.attributes_for(:new, Range)).to eq(foo: 'foo', bar: 123, baz: 'baz')
end

# rubocop:disable Style/SymbolProc
describe 'different usages of blocks and procs' do
class A
def active?
true
end
end
it 'can use a do...end block' do
@ability.can :read, A do |a|
a.active?
end
expect(@ability).to be_able_to(:read, A.new)
end

it 'can use a inline block' do
@ability.can(:read, A) { |a| a.active? }
expect(@ability).to be_able_to(:read, A.new)
end

it 'can use a method reference' do
@ability.can :read, A, &:active?
expect(@ability).to be_able_to(:read, A.new)
end

it 'can use a Proc' do
proc = Proc.new(&:active?)
@ability.can :read, A, &proc
expect(@ability).to be_able_to(:read, A.new)
end
end
# rubocop:enable Style/SymbolProc

describe '#authorize!' do
describe 'when ability is not authorized to perform an action' do
it 'raises access denied exception' do
Expand Down