Skip to content

Instantly share code, notes, and snippets.

@mikz
Created December 6, 2011 22:57
Show Gist options
  • Save mikz/1440449 to your computer and use it in GitHub Desktop.
Save mikz/1440449 to your computer and use it in GitHub Desktop.
rails 3.1 reloader - holds reloading in dev mode untill some file is modified
module Reloader
def run_callbacks(*args)
case args.first.to_sym
when :prepare
# allow call to ActionDispatch.prepare!
# or when source was updated
# cleanup before every preload
if @app.nil? or changed?
reset!
super(:cleanup)
super
end
when :cleanup
unless @app.nil? # skip call to ActionDispatch.cleanup!
# this is called after catching exception
# we need to force preloading in next request
force!
super
end
else
super
end
end
private
def changed?
force? or ActiveSupport::Dependencies.autoload_paths.map do |p|
Dir["#{p}/**/*.rb"].map{|f| File.mtime(f) }
end.flatten.max > timestamp
end
def timestamp
@timestamp or reset!
end
def reset!
@force = false
@timestamp = Time.now
end
def force!
@force = true
end
def force?
!!@force
end
end
ActiveSupport.on_load(:before_initialize) do
ActionDispatch::Reloader.send(:include, Reloader)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment