diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index d44c541f3d..a97db37bcf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -22,7 +22,7 @@ Metrics/BlockNesting: # Offense count: 5 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 250 + Max: 253 # Offense count: 23 Metrics/CyclomaticComplexity: diff --git a/CHANGELOG.md b/CHANGELOG.md index 866302f7e1..8636f04e90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Your contribution here. +* [#1196](https://github.com/ruby-grape/grape/pull/1196): Allow multiple `before_each` blocks - [@huynhquancam](https://github.com/huynhquancam). * [#1190](https://github.com/ruby-grape/grape/putt/1190): Bypass formatting for statuses with no entity-body - [@tylerdooling](https://github.com/tylerdooling). * [#1188](https://github.com/ruby-grape/grape/putt/1188): Allow parameters with more than one type - [@dslh](https://github.com/dslh). * [#1179](https://github.com/ruby-grape/grape/pull/1179): Allow all RFC6838 valid characters in header vendor - [@suan](https://github.com/suan). diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 0291b27e17..812fa2a804 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -13,14 +13,15 @@ class Endpoint class << self def before_each(new_setup = false, &block) + @before_each ||= [] if new_setup == false if block_given? - @before_each = block + @before_each << block else return @before_each end else - @before_each = new_setup + @before_each = [new_setup] end end @@ -226,7 +227,9 @@ def run(env) cookies.read(@request) - self.class.before_each.call(self) if self.class.before_each + self.class.before_each.each do |blk| + blk.call(self) if blk.respond_to?(:call) + end if self.class.before_each.any? run_filters befores, :before diff --git a/spec/grape/endpoint_spec.rb b/spec/grape/endpoint_spec.rb index e894333f54..ed50057604 100644 --- a/spec/grape/endpoint_spec.rb +++ b/spec/grape/endpoint_spec.rb @@ -13,13 +13,13 @@ def app it 'should be settable via block' do block = ->(_endpoint) { 'noop' } Grape::Endpoint.before_each(&block) - expect(Grape::Endpoint.before_each).to eq(block) + expect(Grape::Endpoint.before_each.first).to eq(block) end it 'should be settable via reference' do block = ->(_endpoint) { 'noop' } Grape::Endpoint.before_each block - expect(Grape::Endpoint.before_each).to eq(block) + expect(Grape::Endpoint.before_each.first).to eq(block) end it 'should be able to override a helper' do @@ -36,6 +36,28 @@ def app Grape::Endpoint.before_each(nil) expect { get '/' }.to raise_error(NameError) end + + it 'should be able to stack helper' do + subject.get('/') do + authenticate_user! + current_user + end + expect { get '/' }.to raise_error(NameError) + + Grape::Endpoint.before_each do |endpoint| + allow(endpoint).to receive(:current_user).and_return('Bob') + end + + Grape::Endpoint.before_each do |endpoint| + allow(endpoint).to receive(:authenticate_user!).and_return(true) + end + + get '/' + expect(last_response.body).to eq('Bob') + + Grape::Endpoint.before_each(nil) + expect { get '/' }.to raise_error(NameError) + end end describe '#initialize' do