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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#### Fixes

* [#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).
* Your contribution here.

Expand Down
16 changes: 11 additions & 5 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ def run_before_each(endpoint)
superclass.run_before_each(endpoint) unless self == Endpoint
before_each.each { |blk| blk.try(:call, endpoint) }
end

def block_to_unbound_method(block)
return unless block

define_method :temp_unbound_method, block
method = instance_method(:temp_unbound_method)
remove_method :temp_unbound_method
method
end
end

# Create a new endpoint.
Expand Down Expand Up @@ -70,7 +79,7 @@ def initialize(new_settings, **options, &block)
@status = nil
@stream = nil
@body = nil
@source = block
@source = self.class.block_to_unbound_method(block)
@before_filter_passed = false
end

Expand Down Expand Up @@ -190,10 +199,7 @@ def execute
return unless @source

ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: self) do
instance_exec(&@source)
rescue LocalJumpError => e
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated. Use `next` instead.'
return e.exit_value
@source.bind_call(self)
end
end

Expand Down
20 changes: 2 additions & 18 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -762,28 +762,12 @@ def memoized
expect(last_response.body).to eq('yo')
end

context 'when `return`' do
it 'calls deprecator' do
context 'when calling return' do
it 'does not raise a LocalJumpError' do
subject.get('/home') do
return 'Hello'
end

expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated. Use `next` instead.')

get '/home'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello')
end
end

context 'when `next`' do
it 'does not call deprecator' do
subject.get('/home') do
next 'Hello'
end

expect(Grape.deprecator).not_to receive(:warn)

get '/home'
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('Hello')
Expand Down
Loading