Skip to content

Commit 886b13a

Browse files
authored
Revert PR #2577: Restore support for in endpoint blocks (#2641)
1 parent baab208 commit 886b13a

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#### Fixes
1515

1616
* [#2633](https://github.com/ruby-grape/grape/pull/2633): Fix cascade reading - [@ericproulx](https://github.com/ericproulx).
17+
* [#2641](https://github.com/ruby-grape/grape/pull/2641): Restore support for `return` in endpoint blocks - [@ericproulx](https://github.com/ericproulx).
1718
* [#2642](https://github.com/ruby-grape/grape/pull/2642): Fix array allocation in base_route.rb - [@ericproulx](https://github.com/ericproulx).
1819
* Your contribution here.
1920

lib/grape/endpoint.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ def run_before_each(endpoint)
3232
superclass.run_before_each(endpoint) unless self == Endpoint
3333
before_each.each { |blk| blk.try(:call, endpoint) }
3434
end
35+
36+
def block_to_unbound_method(block)
37+
return unless block
38+
39+
define_method :temp_unbound_method, block
40+
method = instance_method(:temp_unbound_method)
41+
remove_method :temp_unbound_method
42+
method
43+
end
3544
end
3645

3746
# Create a new endpoint.
@@ -70,7 +79,7 @@ def initialize(new_settings, **options, &block)
7079
@status = nil
7180
@stream = nil
7281
@body = nil
73-
@source = block
82+
@source = self.class.block_to_unbound_method(block)
7483
@before_filter_passed = false
7584
end
7685

@@ -190,10 +199,7 @@ def execute
190199
return unless @source
191200

192201
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: self) do
193-
instance_exec(&@source)
194-
rescue LocalJumpError => e
195-
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated. Use `next` instead.'
196-
return e.exit_value
202+
@source.bind_call(self)
197203
end
198204
end
199205

spec/grape/endpoint_spec.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -762,28 +762,12 @@ def memoized
762762
expect(last_response.body).to eq('yo')
763763
end
764764

765-
context 'when `return`' do
766-
it 'calls deprecator' do
765+
context 'when calling return' do
766+
it 'does not raise a LocalJumpError' do
767767
subject.get('/home') do
768768
return 'Hello'
769769
end
770770

771-
expect(Grape.deprecator).to receive(:warn).with('Using `return` in an endpoint has been deprecated. Use `next` instead.')
772-
773-
get '/home'
774-
expect(last_response.status).to eq(200)
775-
expect(last_response.body).to eq('Hello')
776-
end
777-
end
778-
779-
context 'when `next`' do
780-
it 'does not call deprecator' do
781-
subject.get('/home') do
782-
next 'Hello'
783-
end
784-
785-
expect(Grape.deprecator).not_to receive(:warn)
786-
787771
get '/home'
788772
expect(last_response.status).to eq(200)
789773
expect(last_response.body).to eq('Hello')

0 commit comments

Comments
 (0)