Move controller namespace tracking into route set so it gets
reloaded in dev mode
This commit is contained in:
parent
4dee277a9b
commit
8db038227c
|
@ -265,51 +265,10 @@ module ActionDispatch
|
|||
|
||||
SEPARATORS = %w( / . ? )
|
||||
HTTP_METHODS = [:get, :head, :post, :put, :delete, :options]
|
||||
CONTROLLER_REGEXP = /[_a-zA-Z0-9]+/
|
||||
|
||||
# The root paths which may contain controller files
|
||||
mattr_accessor :controller_paths
|
||||
self.controller_paths = []
|
||||
|
||||
# A helper module to hold URL related helpers.
|
||||
module Helpers
|
||||
include ActionController::PolymorphicRoutes
|
||||
end
|
||||
|
||||
class << self
|
||||
def controller_constraints
|
||||
@controller_constraints ||= begin
|
||||
source = controller_namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" }
|
||||
source << CONTROLLER_REGEXP.source
|
||||
Regexp.compile(source.sort.reverse.join('|'))
|
||||
end
|
||||
end
|
||||
|
||||
def clear_controller_cache!
|
||||
@controller_constraints = nil
|
||||
end
|
||||
|
||||
private
|
||||
def controller_namespaces
|
||||
namespaces = Set.new
|
||||
|
||||
# Find any nested controllers already in memory
|
||||
ActionController::Base.subclasses.each do |klass|
|
||||
controller_name = klass.underscore
|
||||
namespaces << controller_name.split('/')[0...-1].join('/')
|
||||
end
|
||||
|
||||
# Find namespaces in controllers/ directory
|
||||
controller_paths.each do |load_path|
|
||||
load_path = File.expand_path(load_path)
|
||||
Dir["#{load_path}/**/*_controller.rb"].collect do |path|
|
||||
namespaces << File.dirname(path).sub(/#{load_path}\/?/, '')
|
||||
end
|
||||
end
|
||||
|
||||
namespaces.delete('')
|
||||
namespaces
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -113,7 +113,7 @@ module ActionDispatch
|
|||
end
|
||||
end
|
||||
|
||||
requirements[:controller] ||= Routing.controller_constraints
|
||||
requirements[:controller] ||= @set.controller_constraints
|
||||
|
||||
if defaults[:controller]
|
||||
defaults[:action] ||= 'index'
|
||||
|
|
|
@ -383,7 +383,7 @@ module ActionDispatch
|
|||
constraints.reject! { |k, v| segment_keys.include?(k.to_s) }
|
||||
conditions.merge!(constraints)
|
||||
|
||||
requirements[:controller] ||= Routing.controller_constraints
|
||||
requirements[:controller] ||= @set.controller_constraints
|
||||
|
||||
if via = options[:via]
|
||||
via = Array(via).map { |m| m.to_s.upcase }
|
||||
|
|
|
@ -202,10 +202,11 @@ module ActionDispatch
|
|||
end
|
||||
end
|
||||
|
||||
attr_accessor :routes, :named_routes, :configuration_files
|
||||
attr_accessor :routes, :named_routes, :configuration_files, :controller_paths
|
||||
|
||||
def initialize
|
||||
self.configuration_files = []
|
||||
self.controller_paths = []
|
||||
|
||||
self.routes = []
|
||||
self.named_routes = NamedRouteCollection.new
|
||||
|
@ -252,7 +253,7 @@ module ActionDispatch
|
|||
|
||||
def load!
|
||||
# Clear the controller cache so we may discover new ones
|
||||
Routing.clear_controller_cache!
|
||||
@controller_constraints = nil
|
||||
|
||||
load_routes!
|
||||
end
|
||||
|
@ -297,6 +298,37 @@ module ActionDispatch
|
|||
routes_changed_at
|
||||
end
|
||||
|
||||
CONTROLLER_REGEXP = /[_a-zA-Z0-9]+/
|
||||
|
||||
def controller_constraints
|
||||
@controller_constraints ||= begin
|
||||
source = controller_namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" }
|
||||
source << CONTROLLER_REGEXP.source
|
||||
Regexp.compile(source.sort.reverse.join('|'))
|
||||
end
|
||||
end
|
||||
|
||||
def controller_namespaces
|
||||
namespaces = Set.new
|
||||
|
||||
# Find any nested controllers already in memory
|
||||
ActionController::Base.subclasses.each do |klass|
|
||||
controller_name = klass.underscore
|
||||
namespaces << controller_name.split('/')[0...-1].join('/')
|
||||
end
|
||||
|
||||
# Find namespaces in controllers/ directory
|
||||
controller_paths.each do |load_path|
|
||||
load_path = File.expand_path(load_path)
|
||||
Dir["#{load_path}/**/*_controller.rb"].collect do |path|
|
||||
namespaces << File.dirname(path).sub(/#{load_path}\/?/, '')
|
||||
end
|
||||
end
|
||||
|
||||
namespaces.delete('')
|
||||
namespaces
|
||||
end
|
||||
|
||||
def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil)
|
||||
route = Route.new(app, conditions, requirements, defaults, name)
|
||||
@set.add_route(*route)
|
||||
|
|
|
@ -387,7 +387,7 @@ module Rails
|
|||
initializer :initialize_routing do
|
||||
next unless configuration.frameworks.include?(:action_controller)
|
||||
|
||||
ActionController::Routing.controller_paths += configuration.controller_paths
|
||||
ActionController::Routing::Routes.controller_paths += configuration.controller_paths
|
||||
ActionController::Routing::Routes.add_configuration_file(configuration.routes_configuration_file)
|
||||
ActionController::Routing::Routes.reload!
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue