diff --git a/CHANGELOG.md b/CHANGELOG.md index 93fc9304..e5bac240 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/cancan/conditions_matcher.rb b/lib/cancan/conditions_matcher.rb index caaf61df..c8611ae0 100644 --- a/lib/cancan/conditions_matcher.rb +++ b/lib/cancan/conditions_matcher.rb @@ -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) diff --git a/spec/cancan/ability_spec.rb b/spec/cancan/ability_spec.rb index 5e845b10..ed14578c 100644 --- a/spec/cancan/ability_spec.rb +++ b/spec/cancan/ability_spec.rb @@ -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