Skip to content

Instantly share code, notes, and snippets.

@paneq
Created May 22, 2011 09:14
Show Gist options
  • Save paneq/985294 to your computer and use it in GitHub Desktop.
Save paneq/985294 to your computer and use it in GitHub Desktop.
Reload rails on demand instead of doing it after every request
# Rails.root/.gitignore
config/watchr.notify
# Rails.root/.watchr
# rails/railties/lib/rails/application/bootstrap.rb
initializer :set_clear_dependencies_hook do
unless config.cache_classes
changed_at = Proc.new{ File.new(File.join(Rails.root, '.watchr')).mtime }
last_change = changed_at.call
ActionDispatch::Callbacks.before do
change = changed_at.call
if change > last_change
Rails.logger.info("DETECTED CHANGES")
last_change = change
ActiveSupport::DescendantsTracker.clear
ActiveSupport::Dependencies.clear
end
end
end
end
# Add to:
# Rails.root/config/environments/development.rb
#
# Faster code reloading:
if defined?(Rails::Server) # `rails s` only (not in `rails console`)
fork do
exec("watchr", "script/touch.watchr")
end
end
# Rails.root/Gemfile
# Bundle faster Rails instead:
gem 'rails', :git => 'git://github.com/paneq/rails.git', :branch => '3-0-7-faster' # https://github.com/paneq/rails/commit/9d1b49b1d409d8320dd537a407f42d7af27172fc
gem 'watchr'
# Rails.root/script/touch.watchr
require 'fileutils'
rails_root = File.expand_path('../..', __FILE__)
watchr_file = File.join(rails_root, '.watchr')
watch( 'app/(.*)\.rb' ) do |md|
FileUtils.touch(watchr_file)
begin
`test -f ./config/watchr.notify && notify-send -u low "Changed: #{md}"` # Notify on Ubuntu
rescue Errno::ENOENT
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment