提交 8db03822 编写于 作者: J Joshua Peek

Move controller namespace tracking into route set so it gets

reloaded in dev mode
上级 4dee277a
......@@ -265,51 +265,10 @@ module Routing
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 @@ def connect(path, options = {})
end
end
requirements[:controller] ||= Routing.controller_constraints
requirements[:controller] ||= @set.controller_constraints
if defaults[:controller]
defaults[:action] ||= 'index'
......
......@@ -383,7 +383,7 @@ def match(*args)
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 @@ def formatted_#{selector}(*args) #
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 @@ def configuration_file
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 @@ def routes_changed_at
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 @@ def call(env)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册