diff options
-rw-r--r-- | actionpack/lib/action_dispatch/http/request.rb | 12 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 48 | ||||
-rw-r--r-- | actionpack/test/abstract_unit.rb | 2 |
3 files changed, 21 insertions, 41 deletions
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index 1f480eec73..1189111f20 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -67,6 +67,18 @@ module ActionDispatch end end + def controller_class + check_path_parameters! + params = path_parameters + controller_param = params[:controller].underscore if params.key?(:controller) + params[:action] ||= 'index' + + yield unless controller_param + + const_name = "#{controller_param.camelize}Controller" + ActiveSupport::Dependencies.constantize(const_name) + end + def key?(key) @env.key?(key) end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index c298080ac8..35385a5d9b 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -27,41 +27,18 @@ module ActionDispatch 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 + raise ActionController::RoutingError, e.message, e.backtrace if @raise_on_name_error end protected - - def controller_reference(controller_param) - const_name = "#{controller_param.camelize}Controller" - ActiveSupport::Dependencies.constantize(const_name) + def controller_reference(req, &block) + req.controller_class(&block) end private @@ -69,14 +46,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 @@ -756,14 +725,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 diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 60e2cea8a2..fa0b6087ba 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -122,7 +122,7 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase class StubDispatcher < ::ActionDispatch::Routing::RouteSet::Dispatcher protected def controller_reference(controller_param) - controller_param + controller_param.params[:controller] end def dispatch(controller, action, env) |