rails: unable to re-insert middleware that was previously deleted with middleware.delete
Occasionally there may be a need to change the order of the middleware stack. In Rails 4.2 I did this by deleting a given middleware and adding it back in the new desired location. In Rails 5, it is not possible to add a previously deleted middleware, since the delete operations run after all other operations, regardless of the actual order the operations are specified. I believe this is an unintended consequence from #18994.
Steps to reproduce
- Delete a middleware:
config.middleware.delete SomeMiddleware
- Re-insert the middleware somewhere else:
config.middleware.use SomeMiddleware
Expected behavior
SomeMiddleware
should be part of the middleware stack.
Actual behavior
SomeMiddleware
is not part of the middleware stack.
System configuration
Rails version: 5.0.0.1
Ruby version: 2.2.5
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 1
- Comments: 15 (8 by maintainers)
Commits related to this issue
- Make sure it is possible to re-insert a delete middleware Add test case for #26303 — committed to rafaelfranca/omg-rails by rafaelfranca 7 years ago
@Sirupsen
Yeah that looks like a viable workaround.
Regarding the pass-through middleware I mentioned, it turns out you can substitute any object you want (even
nil
) as long you delete it from the stack. In other words, it doesn’t actually have to be a special pass-through middleware because you can remove it:I think this issue (at least the original intention) has been solved after https://github.com/rails/rails/pull/38169 introduced the
move_before
andmove_after
methods?FWIW my work-around for this issue to get Rails 4 behaviour:
This avoids the pass-through middleware @naw suggested, although that’s a pretty clever hack 😃