Skip to content

Commit ab2f0dc

Browse files
qcamdblock
authored andcommitted
Allow multiple before_each blocks
- Turn @before_each class variables to array. - Add specs. - Change rubocop config - Update CHANGELOG
1 parent b3c51df commit ab2f0dc

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Metrics/BlockNesting:
2222
# Offense count: 5
2323
# Configuration parameters: CountComments.
2424
Metrics/ClassLength:
25-
Max: 250
25+
Max: 253
2626

2727
# Offense count: 23
2828
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
* Your contribution here.
77

8+
* [#1196](https://github.com/ruby-grape/grape/pull/1196): Allow multiple `before_each` blocks - [@huynhquancam](https://github.com/huynhquancam).
89
* [#1190](https://github.com/ruby-grape/grape/putt/1190): Bypass formatting for statuses with no entity-body - [@tylerdooling](https://github.com/tylerdooling).
910
* [#1188](https://github.com/ruby-grape/grape/putt/1188): Allow parameters with more than one type - [@dslh](https://github.com/dslh).
1011
* [#1179](https://github.com/ruby-grape/grape/pull/1179): Allow all RFC6838 valid characters in header vendor - [@suan](https://github.com/suan).

lib/grape/endpoint.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ class Endpoint
1313

1414
class << self
1515
def before_each(new_setup = false, &block)
16+
@before_each ||= []
1617
if new_setup == false
1718
if block_given?
18-
@before_each = block
19+
@before_each << block
1920
else
2021
return @before_each
2122
end
2223
else
23-
@before_each = new_setup
24+
@before_each = [new_setup]
2425
end
2526
end
2627

@@ -226,7 +227,9 @@ def run(env)
226227

227228
cookies.read(@request)
228229

229-
self.class.before_each.call(self) if self.class.before_each
230+
self.class.before_each.each do |blk|
231+
blk.call(self) if blk.respond_to?(:call)
232+
end if self.class.before_each.any?
230233

231234
run_filters befores, :before
232235

spec/grape/endpoint_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ def app
1313
it 'should be settable via block' do
1414
block = ->(_endpoint) { 'noop' }
1515
Grape::Endpoint.before_each(&block)
16-
expect(Grape::Endpoint.before_each).to eq(block)
16+
expect(Grape::Endpoint.before_each.first).to eq(block)
1717
end
1818

1919
it 'should be settable via reference' do
2020
block = ->(_endpoint) { 'noop' }
2121
Grape::Endpoint.before_each block
22-
expect(Grape::Endpoint.before_each).to eq(block)
22+
expect(Grape::Endpoint.before_each.first).to eq(block)
2323
end
2424

2525
it 'should be able to override a helper' do
@@ -36,6 +36,28 @@ def app
3636
Grape::Endpoint.before_each(nil)
3737
expect { get '/' }.to raise_error(NameError)
3838
end
39+
40+
it 'should be able to stack helper' do
41+
subject.get('/') do
42+
authenticate_user!
43+
current_user
44+
end
45+
expect { get '/' }.to raise_error(NameError)
46+
47+
Grape::Endpoint.before_each do |endpoint|
48+
allow(endpoint).to receive(:current_user).and_return('Bob')
49+
end
50+
51+
Grape::Endpoint.before_each do |endpoint|
52+
allow(endpoint).to receive(:authenticate_user!).and_return(true)
53+
end
54+
55+
get '/'
56+
expect(last_response.body).to eq('Bob')
57+
58+
Grape::Endpoint.before_each(nil)
59+
expect { get '/' }.to raise_error(NameError)
60+
end
3961
end
4062

4163
describe '#initialize' do

0 commit comments

Comments
 (0)