Class: ActionController::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/action_controller/dispatcher.rb

Overview

Dispatches requests to the appropriate controller and takes care of reloading the app after each request when Dependencies.load? is true.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, request = nil, response = nil) ⇒ Dispatcher

DEPRECATE: Remove arguments, since they are only used by CGI



80
81
82
83
# File 'lib/action_controller/dispatcher.rb', line 80

def initialize(output = $stdout, request = nil, response = nil)
  @output = output
  @app = @@middleware.build(lambda { |env| self.dup._call(env) })
end

Class Method Details

.cleanup_applicationObject



62
63
64
65
66
67
# File 'lib/action_controller/dispatcher.rb', line 62

def cleanup_application
  # Cleanup the application before processing the current request.
  ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
  ActiveSupport::Dependencies.clear
  ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
end

.define_dispatcher_callbacks(cache_classes) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/action_controller/dispatcher.rb', line 6

def define_dispatcher_callbacks(cache_classes)
  unless cache_classes
    unless self.middleware.include?(Reloader)
      self.middleware.insert_after(Failsafe, Reloader)
    end

    ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
  end

  if defined?(ActiveRecord)
    to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers }
  end

  after_dispatch :flush_logger if Base.logger && Base.logger.respond_to?(:flush)

  to_prepare do
    I18n.reload!
  end
end

.dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) ⇒ Object

DEPRECATE: Remove CGI support



27
28
29
# File 'lib/action_controller/dispatcher.rb', line 27

def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout)
  new(output).dispatch_cgi(cgi, session_options)
end

.reload_applicationObject



55
56
57
58
59
60
# File 'lib/action_controller/dispatcher.rb', line 55

def reload_application
  # Run prepare callbacks before every request in development mode
  run_prepare_callbacks

  Routing::Routes.reload
end

.run_prepare_callbacksObject



45
46
47
48
49
50
51
52
53
# File 'lib/action_controller/dispatcher.rb', line 45

def run_prepare_callbacks
  if defined?(Rails) && Rails.logger
    logger = Rails.logger
  else
    logger = Logger.new($stderr)
  end

  new(logger).send :run_callbacks, :prepare_dispatch
end

.to_prepare(identifier = nil, &block) ⇒ Object

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.



39
40
41
42
43
# File 'lib/action_controller/dispatcher.rb', line 39

def to_prepare(identifier = nil, &block)
  @prepare_dispatch_callbacks ||= ActiveSupport::Callbacks::CallbackChain.new
  callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier)
  @prepare_dispatch_callbacks.replace_or_append!(callback)
end

Instance Method Details

#_call(env) ⇒ Object



109
110
111
112
# File 'lib/action_controller/dispatcher.rb', line 109

def _call(env)
  @env = env
  dispatch
end

#call(env) ⇒ Object



105
106
107
# File 'lib/action_controller/dispatcher.rb', line 105

def call(env)
  @app.call(env)
end

#dispatchObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/action_controller/dispatcher.rb', line 85

def dispatch
  begin
    run_callbacks :before_dispatch
    Routing::Routes.call(@env)
  rescue Exception => exception
    if controller ||= (::ApplicationController rescue Base)
      controller.call_with_exception(@env, exception).to_a
    else
      raise exception
    end
  ensure
    run_callbacks :after_dispatch, :enumerator => :reverse_each
  end
end

#dispatch_cgi(cgi, session_options) ⇒ Object

DEPRECATE: Remove CGI support



101
102
103
# File 'lib/action_controller/dispatcher.rb', line 101

def dispatch_cgi(cgi, session_options)
  CGIHandler.dispatch_cgi(self, cgi, @output)
end

#flush_loggerObject



114
115
116
# File 'lib/action_controller/dispatcher.rb', line 114

def flush_logger
  Base.logger.flush
end