diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d59f2874f..1a48d4fadb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/lib/grape/middleware/stack.rb b/lib/grape/middleware/stack.rb index 847e8b639e..7f3550c476 100644 --- a/lib/grape/middleware/stack.rb +++ b/lib/grape/middleware/stack.rb @@ -21,7 +21,7 @@ def ==(other) when Middleware klass == other.klass when Class - klass == other + klass == other || (name.nil? && klass.superclass == other) end end diff --git a/spec/grape/middleware/stack_spec.rb b/spec/grape/middleware/stack_spec.rb index a837c2123f..ff0062fba4 100644 --- a/spec/grape/middleware/stack_spec.rb +++ b/spec/grape/middleware/stack_spec.rb @@ -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') @@ -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')