2010-01-23 09:05:13 -05:00
|
|
|
module Rails
|
|
|
|
class Application
|
|
|
|
class RoutesReloader
|
2010-01-27 11:39:35 -05:00
|
|
|
attr_reader :paths
|
2010-01-24 08:48:00 -05:00
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
def initialize
|
|
|
|
@paths, @last_change_at = [], nil
|
2010-01-23 09:05:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def changed_at
|
|
|
|
routes_changed_at = nil
|
|
|
|
|
2010-01-27 11:39:35 -05:00
|
|
|
paths.each do |path|
|
2010-01-24 03:00:18 -05:00
|
|
|
config_changed_at = File.stat(path).mtime
|
2010-01-23 09:05:13 -05:00
|
|
|
|
|
|
|
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!
|
2010-01-27 11:39:35 -05:00
|
|
|
paths.each { |path| load(path) }
|
2010-01-23 09:05:13 -05:00
|
|
|
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
|