Adds several additional operations useful for customizing your Rails middleware stack.
gem install rails-middleware-extensions
Just add this gem to your gemfile like so:
gem 'rails-middleware-extensions'
Currently two extensions are supported: move
and insert_unless_exists
.
As the name implies, the given middleware will only be inserted if it has not already been added to the middleware stack. For example, here's how we might insert our custom MyCustomMiddleware
middleware before Rails::Logger
:
config.middleware.insert_unless_exists(Rails::Logger, MyCustomMiddleware)
#insert_unless_exists
supports all the arguments regular 'ol #insert
supports, meaning you can also pass an index to insert before. Here's how to insert our custom middleware at the very beginning of the stack:
config.middleware.insert_unless_exists(0, MyCustomMiddleware)
Finally, use #insert_after_unless_exists
to insert a particular middleware after another one. For example, to insert MyCustomMiddleware
after Rails::Logger
:
config.middleware.insert_after_unless_exists(Rails::Logger, MyCustomMiddleware)
As of Rails 5.0, it's not possible to delete a middleware and then re-add it (see this issue) because delete operations are always applied last. Instead, consider using #move
. For example, to move our custom middleware before Rails::Logger
:
config.middleware.move(Rails::Logger, MyCustomMiddleware)
Consider #move
's cousin, #move_after
, to move a piece of middleware after another:
config.middleware.move_after(Rails::Logger, MyCustomMiddleware)
As with #insert_unless_exists
, #move
and #move_after
also support numeric indices:
config.middleware.move(0, MyCustomMiddleware)
bundle exec rspec
should do the trick :) Rails-middleware extensions supports a number of Rails versions, hence all the Gemfiles. There is one Gemfile per supported Rails version. If you'd like to run tests against a particular version, use the BUNDLE_GEMFILE
environment variable like so:
BUNDLE_GEMFILE=Gemfile-rails-4.1.x bundle exec rspec
By default tests will run against Rails 5.2.x.
Licensed under the MIT license. See LICENSE for details.
- Cameron C. Dutro: http://github.com/camertron