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/routes_reloader.rb') 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 From 13d66cdf2544af0d465d596383743b16b5005996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 23 Jan 2010 16:59:32 +0100 Subject: Extract Railtie load from application. --- railties/lib/rails/application/routes_reloader.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'railties/lib/rails/application/routes_reloader.rb') diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index 621bb1ccbd..6d61de2320 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -1,8 +1,8 @@ module Rails class Application class RoutesReloader - attr_reader :config - + # TODO Change config.action_dispatch.route_files to config.action_dispatch.routes_path + # TODO Write tests def initialize(config) @config, @last_change_at = config, nil end @@ -10,8 +10,8 @@ module Rails def changed_at routes_changed_at = nil - config.action_dispatch.route_files.each do |config| - config_changed_at = File.stat(config).mtime + files.each do |file| + config_changed_at = File.stat(file).mtime if routes_changed_at.nil? || config_changed_at > routes_changed_at routes_changed_at = config_changed_at @@ -26,7 +26,7 @@ module Rails routes.disable_clear_and_finalize = true routes.clear! - config.action_dispatch.route_files.each { |config| load(config) } + files.each { |file| load(file) } routes.finalize! nil @@ -41,6 +41,10 @@ module Rails reload! end end + + def files + @config.action_dispatch.route_files + end end end end \ No newline at end of file -- cgit v1.2.3 From 2fde9d774b322fc708990675671231c64c691a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 09:00:18 +0100 Subject: Solve some pendencies. --- railties/lib/rails/application/routes_reloader.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'railties/lib/rails/application/routes_reloader.rb') diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index 6d61de2320..d861d27465 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -1,8 +1,7 @@ module Rails class Application class RoutesReloader - # TODO Change config.action_dispatch.route_files to config.action_dispatch.routes_path - # TODO Write tests + # TODO Write tests for this behavior extracted from Application def initialize(config) @config, @last_change_at = config, nil end @@ -10,8 +9,8 @@ module Rails def changed_at routes_changed_at = nil - files.each do |file| - config_changed_at = File.stat(file).mtime + paths.each do |path| + config_changed_at = File.stat(path).mtime if routes_changed_at.nil? || config_changed_at > routes_changed_at routes_changed_at = config_changed_at @@ -26,7 +25,7 @@ module Rails routes.disable_clear_and_finalize = true routes.clear! - files.each { |file| load(file) } + paths.each { |path| load(path) } routes.finalize! nil @@ -42,8 +41,8 @@ module Rails end end - def files - @config.action_dispatch.route_files + def paths + @config.action_dispatch.route_paths end end end -- cgit v1.2.3 From 84ebfa4550b2325c6c89bc13aa6f904ff88d0db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 24 Jan 2010 14:48:00 +0100 Subject: Ensure metals and initializers in plugins are loaded. --- railties/lib/rails/application/routes_reloader.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'railties/lib/rails/application/routes_reloader.rb') diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb index d861d27465..fe0cfb7801 100644 --- a/railties/lib/rails/application/routes_reloader.rb +++ b/railties/lib/rails/application/routes_reloader.rb @@ -1,7 +1,11 @@ module Rails class Application + # TODO Write tests for this behavior extracted from Application class RoutesReloader - # TODO Write tests for this behavior extracted from Application + def self.paths + @paths ||= [] + end + def initialize(config) @config, @last_change_at = config, nil end @@ -9,7 +13,7 @@ module Rails def changed_at routes_changed_at = nil - paths.each do |path| + self.class.paths.each do |path| config_changed_at = File.stat(path).mtime if routes_changed_at.nil? || config_changed_at > routes_changed_at @@ -25,7 +29,7 @@ module Rails routes.disable_clear_and_finalize = true routes.clear! - paths.each { |path| load(path) } + self.class.paths.each { |path| load(path) } routes.finalize! nil @@ -40,10 +44,6 @@ module Rails reload! end end - - def paths - @config.action_dispatch.route_paths - end end end end \ No newline at end of file -- cgit v1.2.3