Namespace
Methods
T
Constants
Reloader = ActionDispatch::Reloader
 

ActionDispatch::Reloader provides prepare and cleanup callbacks, intended to assist with code reloading during development.

Prepare callbacks are run before each request, and cleanup callbacks after each request. In this respect they are analogs of ActionDispatch::Callback's before and after callbacks. However, cleanup callbacks are not called until the request is fully complete – that is, after close has been called on the response body. This is important for streaming responses such as the following:

self.response_body = lambda { |response, output|
  # code here which refers to application models
}

Cleanup callbacks will not be called until after the response_body lambda is evaluated, ensuring that it can refer to application models and other classes before they are unloaded.

By default, ActionDispatch::Reloader is included in the middleware stack only in the development environment; specifically, when config.cache_classes is false. Callbacks may be registered even when it is not included in the middleware stack, but are executed only when ActionDispatch::Reloader.prepare! or ActionDispatch::Reloader.cleanup! are called manually.

Instance Public methods
test_cleanup_callbacks_are_called_on_exceptions()
# File actionpack/test/dispatch/reloader_test.rb, line 151
def test_cleanup_callbacks_are_called_on_exceptions
  cleaned = false
  Reloader.to_cleanup { cleaned  = true }

  begin
    call_and_return_body do
      raise "error"
    end
  rescue
  end

  assert cleaned
end
test_cleanup_callbacks_are_called_when_body_is_closed()
# File actionpack/test/dispatch/reloader_test.rb, line 105
def test_cleanup_callbacks_are_called_when_body_is_closed
  cleaned = false
  Reloader.to_cleanup { cleaned = true }

  body = call_and_return_body
  assert !cleaned

  body.close
  assert cleaned
end
test_condition_specifies_when_to_reload()
# File actionpack/test/dispatch/reloader_test.rb, line 56
def test_condition_specifies_when_to_reload
  i, j = 0, 0, 0, 0
  Reloader.to_prepare { |*args| i += 1 }
  Reloader.to_cleanup { |*args| j += 1 }
  app = Reloader.new(lambda { |env| [200, {}, []] }, lambda { i < 3 })
  5.times do
    resp = app.call({})
    resp[2].close
  end
  assert_equal 3, i
  assert_equal 3, j
end
test_it_calls_close_on_underlying_object_when_close_is_called_on_body()
# File actionpack/test/dispatch/reloader_test.rb, line 83
def test_it_calls_close_on_underlying_object_when_close_is_called_on_body
  close_called = false
  body = call_and_return_body do
    b = MyBody.new do
      close_called = true
    end
    [200, { "Content-Type" => "text/html" }, b]
  end
  body.close
  assert close_called
end
test_manual_reloading()
# File actionpack/test/dispatch/reloader_test.rb, line 127
def test_manual_reloading
  prepared = cleaned = false
  Reloader.to_prepare { prepared = true }
  Reloader.to_cleanup { cleaned  = true }

  Reloader.prepare!
  assert prepared
  assert !cleaned

  prepared = cleaned = false
  Reloader.cleanup!
  assert !prepared
  assert cleaned
end
test_prepare_callbacks()
# File actionpack/test/dispatch/reloader_test.rb, line 6
def test_prepare_callbacks
  a = b = c = nil
  Reloader.to_prepare { |*args| a = b = c = 1 }
  Reloader.to_prepare { |*args| b = c = 2 }
  Reloader.to_prepare { |*args| c = 3 }

  # Ensure to_prepare callbacks are not run when defined
  assert_nil a || b || c

  # Run callbacks
  call_and_return_body

  assert_equal 1, a
  assert_equal 2, b
  assert_equal 3, c
end
test_prepare_callbacks_arent_called_when_body_is_closed()
# File actionpack/test/dispatch/reloader_test.rb, line 116
def test_prepare_callbacks_arent_called_when_body_is_closed
  prepared = false
  Reloader.to_prepare { prepared = true }

  body = call_and_return_body
  prepared = false

  body.close
  assert !prepared
end
test_prepend_prepare_callback()
# File actionpack/test/dispatch/reloader_test.rb, line 142
def test_prepend_prepare_callback
  i = 10
  Reloader.to_prepare { i += 1 }
  Reloader.to_prepare(:prepend => true) { i = 0 }

  Reloader.prepare!
  assert_equal 1, i
end
test_returned_body_object_always_responds_to_close()
# File actionpack/test/dispatch/reloader_test.rb, line 41
def test_returned_body_object_always_responds_to_close
  body = call_and_return_body
  assert_respond_to body, :close
end
test_returned_body_object_always_responds_to_close_even_if_called_twice()
# File actionpack/test/dispatch/reloader_test.rb, line 46
def test_returned_body_object_always_responds_to_close_even_if_called_twice
  body = call_and_return_body
  assert_respond_to body, :close
  body.close

  body = call_and_return_body
  assert_respond_to body, :close
  body.close
end
test_returned_body_object_behaves_like_underlying_object()
# File actionpack/test/dispatch/reloader_test.rb, line 69
def test_returned_body_object_behaves_like_underlying_object
  body = call_and_return_body do
    b = MyBody.new
    b << "hello"
    b << "world"
    [200, { "Content-Type" => "text/html" }, b]
  end
  assert_equal 2, body.size
  assert_equal "hello", body[0]
  assert_equal "world", body[1]
  assert_equal "foo", body.foo
  assert_equal "bar", body.bar
end
test_returned_body_object_responds_to_all_methods_supported_by_underlying_object()
# File actionpack/test/dispatch/reloader_test.rb, line 95
def test_returned_body_object_responds_to_all_methods_supported_by_underlying_object
  body = call_and_return_body do
    [200, { "Content-Type" => "text/html" }, MyBody.new]
  end
  assert_respond_to body, :size
  assert_respond_to body, :each
  assert_respond_to body, :foo
  assert_respond_to body, :bar
end