Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inserting middleware before/after anonymous classes in the middleware stack #1479

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the chance to change that to lowercase, I realised I had used uppercase when that PR had already been merged 😅

* [#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