Created
February 28, 2015 12:33
-
-
Save KamilLelonek/e0d48319579a5778926f to your computer and use it in GitHub Desktop.
Middleware for reloading routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/rails_routes_reloader.rb | |
class RailsRoutesReloader | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
reload_routes_if_changed | |
return *@app.call(env) | |
end | |
private | |
def reload_routes_if_changed | |
routes_reloader.execute_if_updated | |
end | |
def routes_reloader | |
@routes_reloader ||= file_update_checker.new(rails_routes_files) { reload_routes } | |
end | |
def file_update_checker | |
ActiveSupport::FileUpdateChecker | |
end | |
def rails_routes_files | |
Pathname.new(Rails.root.join('config/routes/')).children | |
end | |
def reload_routes | |
Rails.application.reload_routes! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment