aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/dispatch/dispatcher.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-04-22 15:26:03 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-04-22 15:26:03 +0100
commit5f3f100ce2d689480da85abc88e5e940cf90189e (patch)
tree15c1a05a5308a9eea56d7f0889ac46d9cac5b57c /actionpack/lib/action_controller/dispatch/dispatcher.rb
parentd758d996d1b66e2a65640f79f01ce2ac674d7ed5 (diff)
parentca49299434bc764b667cd86846d892e91a150ef3 (diff)
downloadrails-5f3f100ce2d689480da85abc88e5e940cf90189e.tar.gz
rails-5f3f100ce2d689480da85abc88e5e940cf90189e.tar.bz2
rails-5f3f100ce2d689480da85abc88e5e940cf90189e.zip
Merge branch 'master' into active_model
Conflicts: activeresource/lib/active_resource/validations.rb
Diffstat (limited to 'actionpack/lib/action_controller/dispatch/dispatcher.rb')
-rw-r--r--actionpack/lib/action_controller/dispatch/dispatcher.rb96
1 files changed, 96 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..bb9d8bd063
--- /dev/null
+++ b/actionpack/lib/action_controller/dispatch/dispatcher.rb
@@ -0,0 +1,96 @@
+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
+ unless self.middleware.include?(ActionDispatch::Reloader)
+ self.middleware.insert_after(ActionDispatch::Failsafe, ActionDispatch::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
+
+ # 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
+
+ def run_prepare_callbacks
+ new.send :run_callbacks, :prepare_dispatch
+ end
+
+ def reload_application
+ # Run prepare callbacks before every request in development mode
+ run_prepare_callbacks
+
+ Routing::Routes.reload
+ end
+
+ 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
+ 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
+
+ def initialize
+ @app = @@middleware.build(lambda { |env| self._call(env) })
+ freeze
+ end
+
+ def call(env)
+ @app.call(env)
+ end
+
+ def _call(env)
+ 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
+
+ def flush_logger
+ Base.logger.flush
+ end
+ end
+end