Skip to content

Commit

Permalink
Merge pull request #1479 from Rosa/insert-around-error
Browse files Browse the repository at this point in the history
Support inserting middleware before/after anonymous classes in the middleware stack
  • Loading branch information
dblock authored Aug 29, 2016
2 parents 477e70d + 39ff0d0 commit 8709973
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

* Your contribution here.

#### Fixes

* [#1479](https://github.com/ruby-grape/grape/pull/1479): Support inserting middleware before/after anonymous classes in the middleware stack - [@rosa](https://github.com/rosa).

0.17.0 (7/29/2016)
==================

#### Features

* [#1393](https://github.com/ruby-grape/grape/pull/1393): Middleware can be inserted before or after default Grape middleware - [@ridiculous](https://github.com/ridiculous).
* [#1390](https://github.com/ruby-grape/grape/pull/1390): Allow inserting middleware at arbitrary points in the middleware stack - [@Rosa](https://github.com/Rosa).
* [#1390](https://github.com/ruby-grape/grape/pull/1390): Allow inserting middleware at arbitrary points in the middleware stack - [@rosa](https://github.com/rosa).
* [#1366](https://github.com/ruby-grape/grape/pull/1366): Store `message_key` on `Grape::Exceptions::Validation` - [@mkou](https://github.com/mkou).
* [#1398](https://github.com/ruby-grape/grape/pull/1398): Add `rescue_from :grape_exceptions` - allow Grape to use the built-in `Grape::Exception` handing and use `rescue :all` behavior for everything else - [@mmclead](https://github.com/mmclead).
* [#1443](https://github.com/ruby-grape/grape/pull/1443): Extend `given` to receive a `Proc` - [@glaucocustodio](https://github.com/glaucocustodio).
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def ==(other)
when Middleware
klass == other.klass
when Class
klass == other
klass == other || (name.nil? && klass.superclass == other)
end
end

Expand Down
20 changes: 20 additions & 0 deletions spec/grape/middleware/stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def initialize(&block)
expect(subject[1]).to eq(StackSpec::FooMiddleware)
end

it 'inserts a middleware before an anonymous class given by its superclass' do
subject.use Class.new(StackSpec::BlockMiddleware)

expect { subject.insert_before StackSpec::BlockMiddleware, StackSpec::BarMiddleware }
.to change { subject.size }.by(1)

expect(subject[1]).to eq(StackSpec::BarMiddleware)
expect(subject[2]).to eq(StackSpec::BlockMiddleware)
end

it 'raises an error on an invalid index' do
expect { subject.insert_before StackSpec::BlockMiddleware, StackSpec::BarMiddleware }
.to raise_error(RuntimeError, 'No such middleware to insert before: StackSpec::BlockMiddleware')
Expand All @@ -75,6 +85,16 @@ def initialize(&block)
expect(subject[0]).to eq(StackSpec::FooMiddleware)
end

it 'inserts a middleware after an anonymous class given by its superclass' do
subject.use Class.new(StackSpec::BlockMiddleware)

expect { subject.insert_after StackSpec::BlockMiddleware, StackSpec::BarMiddleware }
.to change { subject.size }.by(1)

expect(subject[1]).to eq(StackSpec::BlockMiddleware)
expect(subject[2]).to eq(StackSpec::BarMiddleware)
end

it 'raises an error on an invalid index' do
expect { subject.insert_after StackSpec::BlockMiddleware, StackSpec::BarMiddleware }
.to raise_error(RuntimeError, 'No such middleware to insert after: StackSpec::BlockMiddleware')
Expand Down

0 comments on commit 8709973

Please sign in to comment.