Permalink
Browse files

Move routes_reloader to a class: RoutesReloader

  • Loading branch information...
drogus committed Sep 28, 2010
1 parent ec5d846 commit 57aa79e6bedeffcff899688546ef0a508948b4a5
Showing with 57 additions and 23 deletions.
  1. +2 −22 railties/lib/rails/application.rb
  2. +1 −1 railties/lib/rails/engine.rb
  3. +54 −0 railties/lib/rails/routes_reloader.rb
@@ -1,6 +1,7 @@
require 'active_support/core_ext/hash/reverse_merge'
require 'active_support/file_update_checker'
require 'fileutils'
+require 'rails/routes_reloader'
require 'rails/plugin'
require 'rails/engine'
@@ -81,28 +82,7 @@ def eager_load! #:nodoc:
end
def routes_reloader
- @routes_reloader ||= ActiveSupport::FileUpdateChecker.new([]){ reload_routes! }
- end
-
- def reload_routes!
- routes_to_reload.each do |_routes, draw_block|
- _routes = self.routes
- _routes.disable_clear_and_finalize = true
- _routes.clear!
- _routes.draw(&draw_block) if draw_block
- end
- routes_reloader.paths.each { |path| load(path) }
- routes_to_reload.each do |_routes, draw_block|
- ActiveSupport.on_load(:action_controller) { _routes.finalize! }
- end
- ensure
- routes_to_reload.each do |_routes, draw_block|
- _routes.disable_clear_and_finalize = false
- end
- end
-
- def routes_to_reload
- @routes_to_reload ||= {}
+ @routes_reloader ||= Rails::RoutesReloader.new
end
def initialize!
@@ -449,7 +449,7 @@ def load_seed
end
initializer :add_routing_paths do |app|
- app.routes_to_reload[self.routes] = routes_draw_block
+ app.routes_reloader.blocks[routes] = routes_draw_block
paths.config.routes.to_a.each do |route|
app.routes_reloader.paths.unshift(route) if File.exists?(route)
end
@@ -0,0 +1,54 @@
+module Rails
+ class RoutesReloader < ::ActiveSupport::FileUpdateChecker
+ def initialize
+ super([]) { reload! }
+ end
+
+ def blocks
+ @blocks ||= {}
+ end
+ private
+ def reload!
+ clear!
+ load_blocks
+ load_paths
+ finalize!
+ ensure
+ revert
+ end
+
+ def clear!
+ routers.each do |routes|
+ routes.disable_clear_and_finalize = true
+ routes.clear!
+ end
+ end
+
+ def load_blocks
+ blocks.each do |routes, block|
+ routes.draw(&block) if block
+ end
+ end
+
+ def load_paths
+ paths.each { |path| load(path) }
+ end
+
+ def finalize!
+ routers.each do |routes|
+ ActiveSupport.on_load(:action_controller) { routes.finalize! }
+ end
+ end
+
+ def revert
+ routers.each do |routes|
+ routes.disable_clear_and_finalize = false
+ end
+ end
+
+ def routers
+ blocks.keys
+ end
+ end
+end
+

0 comments on commit 57aa79e

Please sign in to comment.