From f360689586814c8e81f050e13b88bfd978ae69eb Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 8 Aug 2015 18:48:27 -0700 Subject: stop calling `scope` internally we need to get a grip on what `scope` actually does. This commit removes some of the internal calls to `scope`. Eventually we should add public facing methods that provide the API that `scope` is trying to accomplish. --- actionpack/lib/action_dispatch/routing/mapper.rb | 30 +++++++++++++++++----- .../lib/action_dispatch/routing/route_set.rb | 7 +---- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index d5f641a88a..52519188a9 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1397,7 +1397,7 @@ module ActionDispatch end with_scope_level(:collection) do - scope(parent_resource.collection_scope) do + path_scope(parent_resource.collection_scope) do yield end end @@ -1423,7 +1423,7 @@ module ActionDispatch if shallow? shallow_scope(parent_resource.member_scope) { yield } else - scope(parent_resource.member_scope) { yield } + path_scope(parent_resource.member_scope) { yield } end end end @@ -1434,7 +1434,7 @@ module ActionDispatch end with_scope_level(:new) do - scope(parent_resource.new_scope(action_path(:new))) do + path_scope(parent_resource.new_scope(action_path(:new))) do yield end end @@ -1464,9 +1464,10 @@ module ActionDispatch end def shallow - scope(:shallow => true) do - yield - end + @scope = @scope.new(shallow: true) + yield + ensure + @scope = @scope.parent end def shallow? @@ -1683,7 +1684,7 @@ module ActionDispatch @nesting.push(resource) with_scope_level(kind) do - scope(parent_resource.resource_scope) { yield } + controller_scope(parent_resource.resource_scope[:controller]) { yield } end ensure @nesting.pop @@ -1795,6 +1796,21 @@ module ActionDispatch def api_only? @set.api_only? end + private + + def controller_scope(controller) + @scope = @scope.new(controller: controller) + yield + ensure + @scope = @scope.parent + end + + def path_scope(path) + @scope = @scope.new(path: merge_path_scope(@scope[:path], path)) + yield + ensure + @scope = @scope.parent + end end # Routing Concerns allow you to declare common routes that can be reused diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index dd78b9d4fd..c97c8ba0f0 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -54,12 +54,7 @@ module ActionDispatch # 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) - if params.key?(:controller) - controller_param = params[:controller] - controller_reference(controller_param) - else - yield - end + controller_reference params.fetch(:controller) { yield } rescue NameError => e raise ActionController::RoutingError, e.message, e.backtrace if raise_on_name_error end -- cgit v1.2.3