aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/dispatcher.rb
diff options
context:
space:
mode:
authorNicholas Seckar <nseckar@gmail.com>2006-08-06 02:51:53 +0000
committerNicholas Seckar <nseckar@gmail.com>2006-08-06 02:51:53 +0000
commitcbc3afb8786a9e6caa486fa2c97b17348c9eff51 (patch)
tree949c86a1371ea2d3aff00af4398581c4cf6f00cd /railties/lib/dispatcher.rb
parent000a8ed9c688afe167f1d4cd4b6327d350272444 (diff)
downloadrails-cbc3afb8786a9e6caa486fa2c97b17348c9eff51.tar.gz
rails-cbc3afb8786a9e6caa486fa2c97b17348c9eff51.tar.bz2
rails-cbc3afb8786a9e6caa486fa2c97b17348c9eff51.zip
Add Dispatcher.to_prepare and config.to_prepare to provide a pre-request hook.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4686 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib/dispatcher.rb')
-rw-r--r--railties/lib/dispatcher.rb47
1 files changed, 44 insertions, 3 deletions
diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb
index 32f286434f..a125018446 100644
--- a/railties/lib/dispatcher.rb
+++ b/railties/lib/dispatcher.rb
@@ -25,6 +25,7 @@
# to the appropriate controller and action. It also takes care of resetting
# the environment (when Dependencies.load? is true) after each request.
class Dispatcher
+
class << self
# Dispatch the given CGI request, using the given session options, and
@@ -54,11 +55,36 @@ class Dispatcher
# to restart the server (WEBrick, FastCGI, etc.).
def reset_application!
Dependencies.clear
- ActiveRecord::Base.reset
+ ActiveRecord::Base.reset if defined?(ActiveRecord)
Class.remove_class(*Reloadable.reloadable_classes)
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)
+ unless identifier.nil?
+ callback = preparation_callbacks.detect { |ident, _| ident == identifier }
+ if callback # Already registered: update the existing callback
+ callback[-1] = block
+ return
+ end
+ end
+ preparation_callbacks << [identifier, block]
+ nil
+ end
private
+
+ attr_accessor :preparation_callbacks, :preparation_callbacks_run
+ alias_method :preparation_callbacks_run?, :preparation_callbacks_run
+
# CGI.new plus exception handling. CGI#read_multipart raises EOFError
# if body.empty? or body.size != Content-Length and raises ArgumentError
# if Content-Length is non-integer.
@@ -67,10 +93,15 @@ class Dispatcher
end
def prepare_application
- ActionController::Routing::Routes.reload if Dependencies.load?
+ if Dependencies.load?
+ ActionController::Routing::Routes.reload
+ self.preparation_callbacks_run = false
+ end
+
prepare_breakpoint
require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
- ActiveRecord::Base.verify_active_connections!
+ ActiveRecord::Base.verify_active_connections! if defined?(ActiveRecord)
+ run_preparation_callbacks
end
def reset_after_dispatch
@@ -87,6 +118,12 @@ class Dispatcher
nil
end
+ def run_preparation_callbacks
+ return if preparation_callbacks_run?
+ preparation_callbacks.each { |_, callback| callback.call }
+ self.preparation_callbacks_run = true
+ end
+
# If the block raises, send status code as a last-ditch response.
def failsafe_response(output, status, exception = nil)
yield
@@ -117,4 +154,8 @@ class Dispatcher
end
end
end
+
+ self.preparation_callbacks = []
+ self.preparation_callbacks_run = false
+
end