Class | ActionController::Dispatcher |
In: |
vendor/rails/actionpack/lib/action_controller/dispatcher.rb
|
Parent: | Object |
Dispatches requests to the appropriate controller and takes care of reloading the app after each request when Dependencies.load? is true.
Declare a block to be called after each dispatch. Run in reverse of the order declared.
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 21 21: def after_dispatch(*method_names, &block) 22: callbacks[:after].concat method_names 23: callbacks[:after] << block if block_given? 24: end
Declare a block to be called before each dispatch. Run in the order declared.
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 14 14: def before_dispatch(*method_names, &block) 15: callbacks[:before].concat method_names 16: callbacks[:before] << block if block_given? 17: end
Backward-compatible class method takes CGI-specific args. Deprecated in favor of Dispatcher.new(output, request, response).dispatch.
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 8 8: def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) 9: new(output).dispatch_cgi(cgi, session_options) 10: end
If the block raises, send status code as a last-ditch response.
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 48 48: def failsafe_response(fallback_output, status, originating_exception = nil) 49: yield 50: rescue Exception => exception 51: begin 52: log_failsafe_exception(status, originating_exception || exception) 53: body = failsafe_response_body(status) 54: fallback_output.write "Status: #{status}\r\nContent-Type: text/html\r\n\r\n#{body}" 55: nil 56: rescue Exception => failsafe_error # Logger or IO errors 57: $stderr.puts "Error during failsafe response: #{failsafe_error}" 58: $stderr.puts "(originally #{originating_exception})" if originating_exception 59: end 60: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 109 109: def initialize(output, request = nil, response = nil) 110: @output, @request, @response = output, request, response 111: end
Add a preparation callback. Preparation callbacks are run before every request in development mode, and before the first request in production mode.
An optional identifier may be supplied for the callback. If provided, to_prepare may be called again with the same identifier to replace the existing callback. Passing an identifier is a suggested practice if the code adding a preparation block may be reloaded.
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 34 34: def to_prepare(identifier = nil, &block) 35: # Already registered: update the existing callback 36: if identifier 37: if callback = callbacks[:prepare].assoc(identifier) 38: callback[1] = block 39: else 40: callbacks[:prepare] << [identifier, block] 41: end 42: else 43: callbacks[:prepare] << block 44: end 45: end
Cleanup the application by clearing out loaded classes so they can be reloaded on the next request without restarting the server.
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 156 156: def cleanup_application(force = false) 157: if Dependencies.load? || force 158: ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) 159: Dependencies.clear 160: ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) 161: end 162: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 113 113: def dispatch 114: run_callbacks :before 115: handle_request 116: rescue Exception => exception 117: failsafe_rescue exception 118: ensure 119: run_callbacks :after, :reverse_each 120: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 122 122: def dispatch_cgi(cgi, session_options) 123: if cgi ||= self.class.failsafe_response(@output, '400 Bad Request') { CGI.new } 124: @request = CgiRequest.new(cgi, session_options) 125: @response = CgiResponse.new(cgi) 126: dispatch 127: end 128: rescue Exception => exception 129: failsafe_rescue exception 130: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 164 164: def flush_logger 165: RAILS_DEFAULT_LOGGER.flush if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:flush) 166: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 139 139: def prepare_application(force = false) 140: begin 141: require_dependency 'application' unless defined?(::ApplicationController) 142: rescue LoadError => error 143: raise unless error.message =~ /application\.rb/ 144: end 145: 146: ActiveRecord::Base.verify_active_connections! if defined?(ActiveRecord) 147: 148: if unprepared || force 149: run_callbacks :prepare 150: self.unprepared = false 151: end 152: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 132 132: def reload_application 133: if Dependencies.load? 134: Routing::Routes.reload 135: self.unprepared = true 136: end 137: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 185 185: def failsafe_rescue(exception) 186: self.class.failsafe_response(@output, '500 Internal Server Error', exception) do 187: if @controller ||= defined?(::ApplicationController) ? ::ApplicationController : Base 188: @controller.process_with_exception(@request, @response, exception).out(@output) 189: else 190: raise exception 191: end 192: end 193: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 169 169: def handle_request 170: @controller = Routing::Routes.recognize(@request) 171: @controller.process(@request, @response).out(@output) 172: end
# File vendor/rails/actionpack/lib/action_controller/dispatcher.rb, line 174 174: def run_callbacks(kind, enumerator = :each) 175: callbacks[kind].send!(enumerator) do |callback| 176: case callback 177: when Proc; callback.call(self) 178: when String, Symbol; send!(callback) 179: when Array; callback[1].call(self) 180: else raise ArgumentError, "Unrecognized callback #{callback.inspect}" 181: end 182: end 183: end