Permalink
Browse files

Refactor to remove controller class from route to request

This refactoring moves the controller class name that was on the route
set to the request. The purpose of this refactoring is for changes we
need to move controller tests to integration tests, mainly being able to
access the controller on the request instead of having to go through
the router.

[Eileen M. Uchitelle & Aaron Patterson]
  • Loading branch information...
eileencodes committed Aug 22, 2015
1 parent 0885a5c commit 4276b214f8a13a38ac7dc4911e90d295a8e40d5a
@@ -67,6 +67,18 @@ def check_path_parameters!
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
@@ -27,56 +27,25 @@ def initialize(raise_on_name_error)
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
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 @@ def recognize_path(path, environment = {})
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
@@ -122,7 +122,7 @@ def self.build_app(routes = nil)
class StubDispatcher < ::ActionDispatch::Routing::RouteSet::Dispatcher
protected
def controller_reference(controller_param)
- controller_param
+ controller_param.params[:controller]
end
def dispatch(controller, action, env)

1 comment on commit 4276b21

@bogdan

This comment has been minimized.

Show comment
Hide comment
@bogdan

bogdan Aug 22, 2015

Contributor

good job... tried to refactor that long time ago... with no luck... It is great that you've found a way.

Contributor

bogdan commented on 4276b21 Aug 22, 2015

good job... tried to refactor that long time ago... with no luck... It is great that you've found a way.

Please sign in to comment.