Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

* Your contribution here.
* [#2655](https://github.com/ruby-grape/grape/pull/2655): Fix `before_each` method to handle `nil` parameter correctly - [@ericproulx](https://github.com/ericproulx).

### 3.1.0 (2026-01-25)

Expand All @@ -33,6 +34,7 @@
* [#2633](https://github.com/ruby-grape/grape/pull/2633): Fix cascade reading - [@ericproulx](https://github.com/ericproulx).
* [#2641](https://github.com/ruby-grape/grape/pull/2641): Restore support for `return` in endpoint blocks - [@ericproulx](https://github.com/ericproulx).
* [#2642](https://github.com/ruby-grape/grape/pull/2642): Fix array allocation in base_route.rb - [@ericproulx](https://github.com/ericproulx).
* Fix `before_each` method to handle `nil` parameter correctly - [@ericproulx](https://github.com/ericproulx).

### 3.0.1 (2025-11-24)

Expand Down
4 changes: 3 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def before_each(new_setup = false, &block)
return @before_each unless block

@before_each << block
else
elsif new_setup
@before_each = [new_setup]
else
@before_each.clear
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def app

it 'is able to override a helper' do
subject.get('/') { current_user }
expect { get '/' }.to raise_error(NameError)
expect { get '/' }.to raise_error(NameError, /undefined local variable or method [`']current_user'/)

described_class.before_each do |endpoint|
allow(endpoint).to receive(:current_user).and_return('Bob')
Expand All @@ -34,15 +34,15 @@ def app
expect(last_response.body).to eq('Bob')

described_class.before_each(nil)
expect { get '/' }.to raise_error(NameError)
expect { get '/' }.to raise_error(NameError, /undefined local variable or method [`']current_user'/)
end

it 'is able to stack helper' do
subject.get('/') do
authenticate_user!
current_user
end
expect { get '/' }.to raise_error(NameError)
expect { get '/' }.to raise_error(NoMethodError, /undefined method [`']authenticate_user!' for/)

described_class.before_each do |endpoint|
allow(endpoint).to receive(:current_user).and_return('Bob')
Expand All @@ -56,7 +56,7 @@ def app
expect(last_response.body).to eq('Bob')

described_class.before_each(nil)
expect { get '/' }.to raise_error(NameError)
expect { get '/' }.to raise_error(NoMethodError, /undefined method [`']authenticate_user!' for/)
end
end

Expand Down