Hot reload lib folder with Rails
Wanted to move some code inside lib folder and got frustrated by lack of hot-reloading?
Here it is an Oriol approved™️ code to solve it:
config/initializers/lib_hot_reload.rb
:
module LibHotReload
def self.track(libs)
files = libs.map { |lib| Dir["lib/#{lib}.rb"] + Dir["lib/#{lib}/*.rb"] }.flatten
reloader = ActiveSupport::FileUpdateChecker.new(files) do
files.each { |file| load file }
end
ActiveSupport::Reloader.to_prepare do
reloader.execute_if_updated
end
end
end
LibHotReload.track(%w[library1 library2])
Source 👉https://stackoverflow.com/questions/3282655/ruby-on-rails-3-reload-lib-directory-for-each-request/4368838#4368838
Happy reload! 🖖