diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-01-30 11:30:27 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-01-30 11:30:27 -0800 |
commit | 3030bc90c95e335d726f06fd7a61ed96055e9109 (patch) | |
tree | 5b079250b368f0e8af6d2f72a4278fdab3382b26 /actionpack/lib/action_controller/dispatch/dispatcher.rb | |
parent | ae42163bf5497849e4fcbb736505910c17640459 (diff) | |
parent | 85750f22c90c914a429116fb908990c5a2c68379 (diff) | |
download | rails-3030bc90c95e335d726f06fd7a61ed96055e9109.tar.gz rails-3030bc90c95e335d726f06fd7a61ed96055e9109.tar.bz2 rails-3030bc90c95e335d726f06fd7a61ed96055e9109.zip |
Merge commit 'rails/3-0-unstable'
Conflicts:
actionpack/lib/action_controller/base.rb
actionpack/lib/action_dispatch/http/mime_type.rb
actionpack/lib/action_dispatch/http/request.rb
actionpack/lib/action_view/base.rb
actionpack/lib/action_view/paths.rb
actionpack/test/controller/session/cookie_store_test.rb
actionpack/test/dispatch/rack_test.rb
actionpack/test/dispatch/request_test.rb
Diffstat (limited to 'actionpack/lib/action_controller/dispatch/dispatcher.rb')
-rw-r--r-- | actionpack/lib/action_controller/dispatch/dispatcher.rb | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/dispatch/dispatcher.rb b/actionpack/lib/action_controller/dispatch/dispatcher.rb new file mode 100644 index 0000000000..e205245f13 --- /dev/null +++ b/actionpack/lib/action_controller/dispatch/dispatcher.rb @@ -0,0 +1,116 @@ +module ActionController + # Dispatches requests to the appropriate controller and takes care of + # reloading the app after each request when Dependencies.load? is true. + class Dispatcher + class << self + def define_dispatcher_callbacks(cache_classes) + unless cache_classes + # Development mode callbacks + before_dispatch :reload_application + after_dispatch :cleanup_application + + ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false + end + + if defined?(ActiveRecord) + after_dispatch :checkin_connections + 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 + + # DEPRECATE: Remove CGI support + def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) + new(output).dispatch_cgi(cgi, session_options) + 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. + 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 + end + + cattr_accessor :middleware + self.middleware = ActionDispatch::MiddlewareStack.new do |middleware| + middlewares = File.join(File.dirname(__FILE__), "middlewares.rb") + middleware.instance_eval(File.read(middlewares)) + end + + include ActiveSupport::Callbacks + define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch + + # DEPRECATE: Remove arguments, since they are only used by CGI + def initialize(output = $stdout, request = nil, response = nil) + @output = output + @app = @@middleware.build(lambda { |env| self.dup._call(env) }) + end + + 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 + + # DEPRECATE: Remove CGI support + def dispatch_cgi(cgi, session_options) + CGIHandler.dispatch_cgi(self, cgi, @output) + end + + def call(env) + @app.call(env) + end + + def _call(env) + @env = env + dispatch + end + + def reload_application + # Run prepare callbacks before every request in development mode + run_callbacks :prepare_dispatch + + Routing::Routes.reload + end + + # Cleanup the application by clearing out loaded classes so they can + # be reloaded on the next request without restarting the server. + def cleanup_application + ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) + ActiveSupport::Dependencies.clear + ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) + end + + def flush_logger + Base.logger.flush + end + + def checkin_connections + # Don't return connection (and peform implicit rollback) if this request is a part of integration test + return if @env.key?("rack.test") + ActiveRecord::Base.clear_active_connections! + end + end +end |