From 67b2841fbeb6db33dd78a95dd61329a8a56fc7c0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 25 Aug 2015 16:25:52 -0700 Subject: [PATCH] adding a direct dispatch method to controller classes This saves a lambda and request allocation on each request. --- actionpack/lib/action_controller/metal.rb | 10 ++++++++++ actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- actionpack/test/abstract_unit.rb | 10 ++-------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb index d68fa16847..54980aa453 100644 --- a/actionpack/lib/action_controller/metal.rb +++ b/actionpack/lib/action_controller/metal.rb @@ -259,5 +259,15 @@ def self.action(name) lambda { |env| new.dispatch(name, ActionDispatch::Request.new(env)) } end end + + # Direct dispatch to the controller. Instantiates the controller, then + # executes the action named +name+. + def self.dispatch(name, req) + if middleware_stack.any? + middleware_stack.build(name) { |env| new.dispatch(name, req) }.call req.env + else + new.dispatch(name, req) + end + end end end diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index c026d0e2d9..3e3a424df3 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -44,7 +44,7 @@ def controller(req) end def dispatch(controller, action, req) - controller.action(action).call(req.env) + controller.dispatch(action, req) end end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 1a61139dfe..39ae8cf899 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -124,16 +124,10 @@ class DeadEndRoutes < ActionDispatch::Routing::RouteSet class NullController def initialize(controller_name) @controller = controller_name - @action = nil end - def action(action_name) - @action = action_name - self - end - - def call(env) - [200, {'Content-Type' => 'text/html'}, ["#{@controller}##{@action}"]] + def dispatch(action, req) + [200, {'Content-Type' => 'text/html'}, ["#{@controller}##{action}"]] end end -- GitLab