Skip to content

Commit

Permalink
Allow multiple before_each blocks
Browse files Browse the repository at this point in the history
- Turn @before_each class variables to array.
- Add specs.
- Change rubocop config
- Update CHANGELOG
  • Loading branch information
qcam authored and dblock committed Nov 6, 2015
1 parent b3c51df commit ab2f0dc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Metrics/BlockNesting:
# Offense count: 5
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 250
Max: 253

# Offense count: 23
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
9 changes: 6 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
26 changes: 24 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ab2f0dc

Please sign in to comment.