Skip to content

Instantly share code, notes, and snippets.

@mkhairi
Created June 7, 2017 09:48
Show Gist options
  • Save mkhairi/adcdc47fe374f49fd7e677aa45664f57 to your computer and use it in GitHub Desktop.
Save mkhairi/adcdc47fe374f49fd7e677aa45664f57 to your computer and use it in GitHub Desktop.
class RoutesReloader
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(routes_files) { reload_routes }
end
def file_update_checker
ActiveSupport::FileUpdateChecker
end
def routes_files
Pathname.new(Rails.root.join('config/routes/')).children
end
def reload_routes
Rails.application.reload_routes!
end
end
class ActionDispatch::Routing::Mapper
def draw(routes_name, sub_path=nil)
path = Rails.root.join('config/routes', sub_path.to_s, "#{routes_name}.rb")
instance_eval(File.read(path))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment