1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #11352 from xaviershay/dispatcher-api

Allow a custom dispatcher to be provided to routing.
This commit is contained in:
Rafael Mendonça França 2015-08-08 12:22:21 -03:00
commit d919fd88ec
4 changed files with 23 additions and 21 deletions

View file

@ -62,13 +62,17 @@ module ActionDispatch
raise ActionController::RoutingError, e.message, e.backtrace if default_controller raise ActionController::RoutingError, e.message, e.backtrace if default_controller
end end
private protected
attr_reader :controller_class_names
def controller_reference(controller_param) def controller_reference(controller_param)
const_name = @controller_class_names[controller_param] ||= "#{controller_param.camelize}Controller" const_name = controller_class_names[controller_param] ||= "#{controller_param.camelize}Controller"
ActiveSupport::Dependencies.constantize(const_name) ActiveSupport::Dependencies.constantize(const_name)
end end
private
def dispatch(controller, action, req) def dispatch(controller, action, req)
controller.action(action).call(req.env) controller.action(action).call(req.env)
end end
@ -313,7 +317,7 @@ module ActionDispatch
attr_accessor :formatter, :set, :named_routes, :default_scope, :router attr_accessor :formatter, :set, :named_routes, :default_scope, :router
attr_accessor :disable_clear_and_finalize, :resources_path_names attr_accessor :disable_clear_and_finalize, :resources_path_names
attr_accessor :default_url_options attr_accessor :default_url_options, :dispatcher_class
attr_reader :env_key attr_reader :env_key
alias :routes :set alias :routes :set
@ -356,6 +360,7 @@ module ActionDispatch
@set = Journey::Routes.new @set = Journey::Routes.new
@router = Journey::Router.new @set @router = Journey::Router.new @set
@formatter = Journey::Formatter.new @set @formatter = Journey::Formatter.new @set
@dispatcher_class = Routing::RouteSet::Dispatcher
end end
def relative_url_root def relative_url_root
@ -414,7 +419,7 @@ module ActionDispatch
end end
def dispatcher(defaults) def dispatcher(defaults)
Routing::RouteSet::Dispatcher.new(defaults) dispatcher_class.new(defaults)
end end
module MountedHelpers module MountedHelpers

View file

@ -130,14 +130,10 @@ class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
end end
end end
def self.stub_controllers def self.stub_controllers(config = nil)
old_dispatcher = ActionDispatch::Routing::RouteSet::Dispatcher route_set = ActionDispatch::Routing::RouteSet.new(*[config].compact)
ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher } route_set.dispatcher_class = StubDispatcher
ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, StubDispatcher } yield route_set
yield ActionDispatch::Routing::RouteSet.new
ensure
ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, old_dispatcher }
end end
def with_routing(&block) def with_routing(&block)

View file

@ -19,6 +19,10 @@ module ActionDispatch
ActionDispatch::Request ActionDispatch::Request
end end
def dispatcher_class
RouteSet::Dispatcher
end
def add_route(*args) def add_route(*args)
routes << args routes << args
end end

View file

@ -168,12 +168,10 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end end
def test_session_singleton_resource_for_api_app def test_session_singleton_resource_for_api_app
self.class.stub_controllers do |_|
config = ActionDispatch::Routing::RouteSet::Config.new config = ActionDispatch::Routing::RouteSet::Config.new
config.api_only = true config.api_only = true
routes = ActionDispatch::Routing::RouteSet.new(config) self.class.stub_controllers(config) do |routes|
routes.draw do routes.draw do
resource :session do resource :session do
get :create get :create
@ -550,11 +548,10 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest
end end
def test_projects_for_api_app def test_projects_for_api_app
self.class.stub_controllers do |_|
config = ActionDispatch::Routing::RouteSet::Config.new config = ActionDispatch::Routing::RouteSet::Config.new
config.api_only = true config.api_only = true
routes = ActionDispatch::Routing::RouteSet.new(config) self.class.stub_controllers(config) do |routes|
routes.draw do routes.draw do
resources :projects, controller: :project resources :projects, controller: :project
end end