Skip to content

Instantly share code, notes, and snippets.

@trcarden
Created July 30, 2012 06:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trcarden/3205221 to your computer and use it in GitHub Desktop.
Save trcarden/3205221 to your computer and use it in GitHub Desktop.
Autoreload gems in development
# Inside config/environments/development.rb
# Do the following after every request to the server in development mode
ActionDispatch::Callbacks.to_cleanup do
# If the gem's top level module is currently loaded, unload it
if Object.const_defined?(:MyCoolGem)
Object.send(:remove_const, :MyCoolGem)
end
# Instruct ruby to "unrequire" all of the gems files.
# CAREFUL: make sure this only matches your gems files.
$".delete_if {|s| s.include? "my_cool_gem"}
# Re-require your gem
# Note: because we removed all files previously required they will be reloaded
# even if you didn't use load/autoload in your gem.
require "my_cool_gem"
end
Copy link

ghost commented Aug 25, 2012

it does not work for me. I am sure I put the code in the development.rb, and I puts "something" inside this part of code, I found all of these code does not execute.

@oliverhuang
Copy link

It does not work for me neither. Same as mark-secondbureau

@pinge
Copy link

pinge commented Sep 28, 2012

try to use ActionDispatch::Callbacks.before instead of ActionDispatch::Callbacks.to_cleanup

ActionDispatch::Callbacks.to_cleanup is delegated to ActionDispatch::Reloader and is run after each request.

module ActionDispatch
  class Reloader

    include ActiveSupport::Callbacks
    ...
    # Add a cleanup callback. Cleanup callbacks are run after each request is
    # complete (after #close is called on the response body).
    def self.to_cleanup(*args, &block)
      set_callback(:cleanup, *args, &block)
    end
    ...

module ActionDispatch
  # Provide callbacks to be executed before and after the request dispatch.
  class Callbacks

    include ActiveSupport::Callbacks

    define_callbacks :call

    class << self
      delegate :to_prepare, :to_cleanup, :to => "ActionDispatch::Reloader"
    end
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment