aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/dispatcher.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-17 23:49:03 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-04-18 13:05:43 +0100
commit986aec5dbbdfb578945e706cbe6a54c4f06640e5 (patch)
treed51a8d46628d08088bc1e9beb7c9305202cd991a /actionpack/lib/action_controller/dispatcher.rb
parentcf04e621270bb2e5e9e7971d2c59e73d6797482d (diff)
downloadrails-986aec5dbbdfb578945e706cbe6a54c4f06640e5.tar.gz
rails-986aec5dbbdfb578945e706cbe6a54c4f06640e5.tar.bz2
rails-986aec5dbbdfb578945e706cbe6a54c4f06640e5.zip
Refactor Dispatcher callbacks to remove unnecessary Dependencies checks in production environment.
Diffstat (limited to 'actionpack/lib/action_controller/dispatcher.rb')
-rw-r--r--actionpack/lib/action_controller/dispatcher.rb73
1 files changed, 33 insertions, 40 deletions
diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb
index 99e1f74c0a..6e1e7a261f 100644
--- a/actionpack/lib/action_controller/dispatcher.rb
+++ b/actionpack/lib/action_controller/dispatcher.rb
@@ -5,6 +5,30 @@ module ActionController
@@guard = Mutex.new
class << self
+ def define_dispatcher_callbacks(cache_classes)
+ unless cache_classes
+ # Development mode callbacks
+ before_dispatch :reload_application
+ after_dispatch :cleanup_application
+ end
+
+ # Common callbacks
+ to_prepare :load_application_controller do
+ begin
+ require_dependency 'application' unless defined?(::ApplicationController)
+ rescue LoadError => error
+ raise unless error.message =~ /application\.rb/
+ end
+ end
+
+ if defined?(ActiveRecord)
+ before_dispatch { ActiveRecord::Base.verify_active_connections! }
+ to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers }
+ end
+
+ after_dispatch :flush_logger if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:flush)
+ end
+
# Backward-compatible class method takes CGI-specific args. Deprecated
# in favor of Dispatcher.new(output, request, response).dispatch.
def dispatch(cgi = nil, session_options = CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout)
@@ -69,23 +93,9 @@ module ActionController
cattr_accessor :error_file_path
self.error_file_path = Rails.public_path if defined?(Rails.public_path)
- cattr_accessor :unprepared
- self.unprepared = true
-
include ActiveSupport::Callbacks
define_callbacks :prepare_dispatch, :before_dispatch, :after_dispatch
- before_dispatch :reload_application
- before_dispatch :prepare_application
- after_dispatch :flush_logger
- after_dispatch :cleanup_application
-
- if defined? ActiveRecord
- to_prepare :activerecord_instantiate_observers do
- ActiveRecord::Base.instantiate_observers
- end
- end
-
def initialize(output, request = nil, response = nil)
@output, @request, @response = output, request, response
end
@@ -114,40 +124,23 @@ module ActionController
end
def reload_application
- if Dependencies.load?
- Routing::Routes.reload
- self.unprepared = true
- end
- end
+ # Run prepare callbacks before every request in development mode
+ run_callbacks :prepare_dispatch
- def prepare_application(force = false)
- begin
- require_dependency 'application' unless defined?(::ApplicationController)
- rescue LoadError => error
- raise unless error.message =~ /application\.rb/
- end
-
- ActiveRecord::Base.verify_active_connections! if defined?(ActiveRecord)
-
- if unprepared || force
- run_callbacks :prepare_dispatch
- ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading
- self.unprepared = false
- end
+ Routing::Routes.reload
+ ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading
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(force = false)
- if Dependencies.load? || force
- ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
- Dependencies.clear
- ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
- end
+ def cleanup_application
+ ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord)
+ Dependencies.clear
+ ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord)
end
def flush_logger
- RAILS_DEFAULT_LOGGER.flush if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER.respond_to?(:flush)
+ RAILS_DEFAULT_LOGGER.flush
end
protected