diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 56 |
1 files changed, 12 insertions, 44 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 20926012b4..af6f0de556 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -1,6 +1,5 @@ require 'action_dispatch/journey' require 'forwardable' -require 'thread_safe' require 'active_support/concern' require 'active_support/core_ext/object/to_query' require 'active_support/core_ext/hash/slice' @@ -23,49 +22,27 @@ module ActionDispatch class Dispatcher < Routing::Endpoint def initialize(raise_on_name_error) @raise_on_name_error = raise_on_name_error - @controller_class_names = ThreadSafe::Cache.new end def dispatcher?; true; end def serve(req) - req.check_path_parameters! params = req.path_parameters - - prepare_params!(params) - - controller = controller(params, @raise_on_name_error) do + controller = controller_reference(req) do return [404, {'X-Cascade' => 'pass'}, []] end - dispatch(controller, params[:action], req) - end - - def prepare_params!(params) - normalize_controller!(params) - merge_default_action!(params) - end - - # If this is a default_controller (i.e. a controller specified by the user) - # we should raise an error in case it's not found, because it usually means - # a user error. However, if the controller was retrieved through a dynamic - # segment, as in :controller(/:action), we should simply return nil and - # delegate the control back to Rack cascade. Besides, if this is not a default - # controller, it means we should respect the @scope[:module] parameter. - def controller(params, raise_on_name_error=true) - controller_reference params.fetch(:controller) { yield } rescue NameError => e - raise ActionController::RoutingError, e.message, e.backtrace if raise_on_name_error - yield + if @raise_on_name_error + raise ActionController::RoutingError, e.message, e.backtrace + else + return [404, {'X-Cascade' => 'pass'}, []] + end end protected - - attr_reader :controller_class_names - - def controller_reference(controller_param) - const_name = controller_class_names[controller_param] ||= "#{controller_param.camelize}Controller" - ActiveSupport::Dependencies.constantize(const_name) + def controller_reference(req, &block) + req.controller_class(&block) end private @@ -73,14 +50,6 @@ module ActionDispatch def dispatch(controller, action, req) controller.action(action).call(req.env) end - - def normalize_controller!(params) - params[:controller] = params[:controller].underscore if params.key?(:controller) - end - - def merge_default_action!(params) - params[:action] ||= 'index' - end end # A NamedRouteCollection instance is a collection of named routes, and also @@ -760,14 +729,13 @@ module ActionDispatch req.path_parameters = old_params.merge params app = route.app if app.matches?(req) && app.dispatcher? - dispatcher = app.app - - dispatcher.controller(params, false) do + begin + req.controller_class + rescue NameError raise ActionController::RoutingError, "A route matches #{path.inspect}, but references missing controller: #{params[:controller].camelize}Controller" end - dispatcher.prepare_params!(params) - return params + return req.path_parameters end end |