Repository is archived
No commit activity in last 3 years
No release in over 3 years
Wraps a reloadable class to be used as Rails middleware.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.12

Runtime

 Project Readme

RailsMiddlewareDelegator

When using Rails middleware, a common problem is that middleware loaded from lib does not reload between requests. In order for changes to these middleware classes to be reflected in a running server, the development server process must be killed and restarted.

If one were to drop a middleware class into a subdirectory of app and rely on autoloading, however, changing any other autoloaded file and making a new server request results in an error, A copy of X has been removed from the module tree bus is still active. Upon starting the server, an instance of the class was loaded into memory. At the end of the request, the class was unloaded. On the second request upon running through the middleware stack, Rails detected that it was executing through an instance of an unloaded class. Boom!

This gem provides a delegator class that can wrap the unloadable middleware. Instead of registering the middleware itself, you can use an instance of the delegator, which being a gem managed by bundler will not be unloaded between requests.

In environments where cache_classes is true, the delegator steps out of the way and adds the delegated middleware directly onto the stack. When false, it holds onto any passed configuration and instantiates a new middleware instance for each request.

Usage

Rails.application.config.middleware.use RailsMiddlewareDelegator.new('MyMiddleware')

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_middleware_delegator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.