How is it possible to force reloading a gem for every request?
I develop a gem and would like to reload my gem's code every time when I refresh the page in a browser. My Gemfile:
gem "my_gem", :path => "../my_gem"
To solve the problem I tried every suggestion listed in stakoverflow. Nothing helped. Also have found two Rails config parameters: watchable_dirs and watchable_files. Tried to use them but they also don't work for me.
You should mark the classes you want to reload as unloadable using ActiveSupport::Dependencies unloadable method;
class YourClass
unloadable
end
http://apidock.com/rails/ActiveSupport/Dependencies/Loadable/unloadable and http://robots.thoughtbot.com/post/159805560/tips-for-writing-your-own-rails-engine
should give you some background. Alternatively you can do your own reloading like this;
Object.send(:remove_const, 'YOUR_CLASS') if Object.const_defined?('YOUR_CLASS')
GC.start
load 'path/to/your_file.rb'
I’ve done quote a bit of hunting around for this, but in the end it took some trial and error.
lib/my_gem/my_gem.rb:
require 'active_support/dependencies'
ActiveSupport::Dependencies.autoload_paths += [File.expand_path("..", __FILE__)]
module MyGem
include ActiveSupport::Dependencies
unloadable
end
Be sure to add “unloadable” to all of your classes as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With