Skip to content

Commit 18bda7a

Browse files
committed
Revert PR #2577: Restore support for in endpoint blocks
1 parent ea7bbb7 commit 18bda7a

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

lib/grape/endpoint.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ 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+
define_method :temp_unbound_method, block
38+
method = instance_method(:temp_unbound_method)
39+
remove_method :temp_unbound_method
40+
method
41+
end
3542
end
3643

3744
# Create a new endpoint.
@@ -70,7 +77,7 @@ def initialize(new_settings, **options, &block)
7077
@status = nil
7178
@stream = nil
7279
@body = nil
73-
@source = block
80+
@source = self.class.block_to_unbound_method(block) if block
7481
end
7582

7683
# Update our settings from a given set of stackable parameters. Used when
@@ -188,10 +195,7 @@ def execute
188195
return unless @source
189196

190197
ActiveSupport::Notifications.instrument('endpoint_render.grape', endpoint: self) do
191-
instance_exec(&@source)
192-
rescue LocalJumpError => e
193-
Grape.deprecator.warn 'Using `return` in an endpoint has been deprecated. Use `next` instead.'
194-
return e.exit_value
198+
@source.bind_call(self)
195199
end
196200
end
197201

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)