diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-25 18:35:44 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-08-25 18:35:44 -0700 |
commit | 51c7ac142d31095d4c699f44cc44ddea627da1eb (patch) | |
tree | 920c40c749272ebec57be8b26164829660781dff /actionpack/lib/action_controller/metal.rb | |
parent | 85a78d9358aa728298cd020cdc842b55c16f9549 (diff) | |
download | rails-51c7ac142d31095d4c699f44cc44ddea627da1eb.tar.gz rails-51c7ac142d31095d4c699f44cc44ddea627da1eb.tar.bz2 rails-51c7ac142d31095d4c699f44cc44ddea627da1eb.zip |
provide a request and response to all controllers
Controllers should always have a request and response when responding.
Since we make this The Rule(tm), then controllers don't need to be
somewhere in limbo between "asking a response object for a rack
response" or "I, myself contain a rack response". This duality leads to
conditionals spread through the codebase that we can delete:
* https://github.com/rails/rails/blob/85a78d9358aa728298cd020cdc842b55c16f9549/actionpack/lib/action_controller/metal.rb#L221-L223
Diffstat (limited to 'actionpack/lib/action_controller/metal.rb')
-rw-r--r-- | actionpack/lib/action_controller/metal.rb | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/actionpack/lib/action_controller/metal.rb b/actionpack/lib/action_controller/metal.rb index 54980aa453..129e0bbd3c 100644 --- a/actionpack/lib/action_controller/metal.rb +++ b/actionpack/lib/action_controller/metal.rb @@ -132,6 +132,12 @@ module ActionController @controller_name ||= name.demodulize.sub(/Controller$/, '').underscore end + def self.make_response!(request) + ActionDispatch::Response.new.tap do |res| + res.request = request + end + end + # Delegates to the class' <tt>controller_name</tt> def controller_name self.class.controller_name @@ -143,11 +149,10 @@ module ActionController # and response object available. You might wish to control the # environment and response manually for performance reasons. - attr_internal :headers, :response, :request - delegate :session, :to => "@_request" + attr_internal :response, :request + delegate :session, :headers, :to => "@_request" def initialize - @_headers = {"Content-Type" => "text/html"} @_status = 200 @_request = nil @_response = nil @@ -168,7 +173,7 @@ module ActionController # in Renderer and Redirector. def content_type=(type) - headers["Content-Type"] = type.to_s + response.content_type = type end def content_type @@ -199,6 +204,7 @@ module ActionController def response_body=(body) body = [body] unless body.nil? || body.respond_to?(:each) + response.body = body super end @@ -207,12 +213,17 @@ module ActionController response_body || (response && response.committed?) end - def dispatch(name, request) #:nodoc: + def dispatch(name, request, response) #:nodoc: set_request!(request) + set_response!(response) process(name) to_a end + def set_response!(response) # :nodoc: + @_response = response + end + def set_request!(request) #:nodoc: @_request = request @_request.controller_instance = self @@ -253,20 +264,26 @@ module ActionController def self.action(name) if middleware_stack.any? middleware_stack.build(name) do |env| - new.dispatch(name, ActionDispatch::Request.new(env)) + req = ActionDispatch::Request.new(env) + res = make_response! req + new.dispatch(name, req, res) end else - lambda { |env| new.dispatch(name, ActionDispatch::Request.new(env)) } + lambda { |env| + req = ActionDispatch::Request.new(env) + res = make_response! req + new.dispatch(name, req, res) + } end end # Direct dispatch to the controller. Instantiates the controller, then # executes the action named +name+. - def self.dispatch(name, req) + def self.dispatch(name, req, res) if middleware_stack.any? - middleware_stack.build(name) { |env| new.dispatch(name, req) }.call req.env + middleware_stack.build(name) { |env| new.dispatch(name, req, res) }.call req.env else - new.dispatch(name, req) + new.dispatch(name, req, res) end end end |