From 80130d1201c3bf9dc17b0e1fcd81c6b22e893b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 15:05:13 +0100 Subject: Extract routes reloading responsibilities from application and load them just upon a request. --- railties/lib/rails/application/routes_reloader.rb | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 railties/lib/rails/application/routes_reloader.rb (limited to 'railties/lib/rails/application') diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb new file mode 100644 index 0000000000..621bb1ccbd --- /dev/null +++ b/railties/lib/rails/application/routes_reloader.rb @@ -0,0 +1,46 @@ +module Rails + class Application + class RoutesReloader + attr_reader :config + + def initialize(config) + @config, @last_change_at = config, nil + end + + def changed_at + routes_changed_at = nil + + config.action_dispatch.route_files.each do |config| + config_changed_at = File.stat(config).mtime + + if routes_changed_at.nil? || config_changed_at > routes_changed_at + routes_changed_at = config_changed_at + end + end + + routes_changed_at + end + + def reload! + routes = Rails::Application.routes + routes.disable_clear_and_finalize = true + + routes.clear! + config.action_dispatch.route_files.each { |config| load(config) } + routes.finalize! + + nil + ensure + routes.disable_clear_and_finalize = false + end + + def reload_if_changed + current_change_at = changed_at + if @last_change_at != current_change_at + @last_change_at = current_change_at + reload! + end + end + end + end +end \ No newline at end of file -- cgit v1.2.3