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.
Source Code
# File action_controller/dispatcher.rb, line 22 def to_prepare(identifier = nil, &block) @prepare_dispatch_callbacks ||= [] callback = ActiveSupport::Callbacks::Callback.new(:prepare_dispatch, block, :identifier => identifier) # Already registered: update the existing callback # TODO: Ruby one liner for Array#find returning index if identifier && callback_for_identifier = @prepare_dispatch_callbacks.find { |c| c.identifier == identifier } index = @prepare_dispatch_callbacks.index(callback_for_identifier) @prepare_dispatch_callbacks[index] = callback else @prepare_dispatch_callbacks.concat([callback]) end end
<code/>and<pre/>for code samples.