mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Routes reloader knows how to reload external files
This commit is contained in:
parent
568ae222af
commit
363a06f351
3 changed files with 73 additions and 4 deletions
|
@ -3,12 +3,13 @@ require "active_support/core_ext/module/delegation"
|
||||||
module Rails
|
module Rails
|
||||||
class Application
|
class Application
|
||||||
class RoutesReloader
|
class RoutesReloader
|
||||||
attr_reader :route_sets, :paths
|
attr_reader :route_sets, :paths, :external_routes
|
||||||
delegate :execute_if_updated, :execute, :updated?, :to => :updater
|
delegate :execute_if_updated, :execute, :updated?, :to => :updater
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@paths = []
|
@paths = []
|
||||||
@route_sets = []
|
@route_sets = []
|
||||||
|
@external_routes = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload!
|
def reload!
|
||||||
|
@ -23,7 +24,12 @@ module Rails
|
||||||
|
|
||||||
def updater
|
def updater
|
||||||
@updater ||= begin
|
@updater ||= begin
|
||||||
updater = ActiveSupport::FileUpdateChecker.new(paths) { reload! }
|
dirs = @external_routes.inject({}) do |hash, dir|
|
||||||
|
hash.merge(dir.to_s => ["rb"])
|
||||||
|
end
|
||||||
|
|
||||||
|
updater = ActiveSupport::FileUpdateChecker.new(paths, dirs) { reload! }
|
||||||
|
|
||||||
updater.execute
|
updater.execute
|
||||||
updater
|
updater
|
||||||
end
|
end
|
||||||
|
|
|
@ -546,10 +546,12 @@ module Rails
|
||||||
|
|
||||||
initializer :add_routing_paths do |app|
|
initializer :add_routing_paths do |app|
|
||||||
paths = self.paths["config/routes.rb"].existent
|
paths = self.paths["config/routes.rb"].existent
|
||||||
|
external_paths = self.paths["config/routes"].paths
|
||||||
|
|
||||||
if routes? || paths.any?
|
if routes? || paths.any?
|
||||||
app.routes_reloader.paths.unshift(*paths)
|
app.routes_reloader.paths.unshift(*paths)
|
||||||
app.routes_reloader.route_sets << routes
|
app.routes_reloader.route_sets << routes
|
||||||
|
app.routes_reloader.external_routes.unshift(*external_paths)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -190,6 +190,67 @@ module ApplicationTests
|
||||||
assert_equal 'success!', last_response.body
|
assert_equal 'success!', last_response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
|
||||||
|
test "reloads routes when external configuration is changed in #{mode}" do
|
||||||
|
controller :foo, <<-RUBY
|
||||||
|
class FooController < ApplicationController
|
||||||
|
def bar
|
||||||
|
render :text => "bar"
|
||||||
|
end
|
||||||
|
|
||||||
|
def baz
|
||||||
|
render :text => "baz"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app_file 'config/routes.rb', <<-RUBY
|
||||||
|
AppTemplate::Application.routes.draw do
|
||||||
|
draw :external
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app_file 'config/routes/external.rb', <<-RUBY
|
||||||
|
get 'foo', :to => 'foo#bar'
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app(mode)
|
||||||
|
|
||||||
|
get '/foo'
|
||||||
|
assert_equal 'bar', last_response.body
|
||||||
|
|
||||||
|
app_file 'config/routes/external.rb', <<-RUBY
|
||||||
|
get 'foo', :to => 'foo#baz'
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
sleep 0.1
|
||||||
|
|
||||||
|
get '/foo'
|
||||||
|
assert_equal expected, last_response.body
|
||||||
|
|
||||||
|
app_file 'config/routes.rb', <<-RUBY
|
||||||
|
AppTemplate::Application.routes.draw do
|
||||||
|
draw :external
|
||||||
|
draw :other_external
|
||||||
|
end
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
app_file 'config/routes/other_external.rb', <<-RUBY
|
||||||
|
get 'win', :to => 'foo#baz'
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
sleep 0.1
|
||||||
|
|
||||||
|
get '/win'
|
||||||
|
|
||||||
|
if mode == "development"
|
||||||
|
assert_equal expected, last_response.body
|
||||||
|
else
|
||||||
|
assert_equal 404, last_response.status
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
|
{"development" => "baz", "production" => "bar"}.each do |mode, expected|
|
||||||
test "reloads routes when configuration is changed in #{mode}" do
|
test "reloads routes when configuration is changed in #{mode}" do
|
||||||
controller :foo, <<-RUBY
|
controller :foo, <<-RUBY
|
||||||
|
|
Loading…
Reference in a new issue