Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload gem for every request in Rails 3.2? [duplicate]

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.

like image 481
Vadim Avatar asked Feb 12 '12 02:02

Vadim


2 Answers

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'
like image 155
james2m Avatar answered Nov 10 '22 00:11

james2m


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.

like image 32
aceofspades Avatar answered Nov 10 '22 00:11

aceofspades