1
require 'action_dispatch/middleware/body_proxy'
4
# ActionDispatch::Reloader provides prepare and cleanup callbacks,
5
# intended to assist with code reloading during development.
7
# Prepare callbacks are run before each request, and cleanup callbacks
8
# after each request. In this respect they are analogs of ActionDispatch::Callback's
9
# before and after callbacks. However, cleanup callbacks are not called until the
10
# request is fully complete -- that is, after #close has been called on
11
# the response body. This is important for streaming responses such as the
14
# self.response_body = lambda { |response, output|
15
# # code here which refers to application models
18
# Cleanup callbacks will not be called until after the response_body lambda
19
# is evaluated, ensuring that it can refer to application models and other
20
# classes before they are unloaded.
22
# By default, ActionDispatch::Reloader is included in the middleware stack
23
# only in the development environment; specifically, when config.cache_classes
24
# is false. Callbacks may be registered even when it is not included in the
25
# middleware stack, but are executed only when +ActionDispatch::Reloader.prepare!+
26
# or +ActionDispatch::Reloader.cleanup!+ are called manually.
29
include ActiveSupport::Callbacks
31
define_callbacks :prepare, :scope => :name
32
define_callbacks :cleanup, :scope => :name
34
# Add a prepare callback. Prepare callbacks are run before each request, prior
35
# to ActionDispatch::Callback's before callbacks.
36
def self.to_prepare(*args, &block)
37
set_callback(:prepare, *args, &block)
40
# Add a cleanup callback. Cleanup callbacks are run after each request is
41
# complete (after #close is called on the response body).
42
def self.to_cleanup(*args, &block)
43
set_callback(:cleanup, *args, &block)
46
# Execute all prepare callbacks.
51
# Execute all cleanup callbacks.
56
def initialize(app, condition=nil)
58
@condition = condition || lambda { true }
63
@validated = @condition.call
65
response = @app.call(env)
66
response[2] = ActionDispatch::BodyProxy.new(response[2]) { cleanup! }
74
run_callbacks :prepare if validated?
78
run_callbacks :cleanup if validated?
85
def validated? #:nodoc: