23

Is there any way in Rails to have the ENV reload "lib" files without having to restart the server? I'm working with some classes that I have inside a module in "lib". However, in order to see my changes I must restart the server each time. I'm guessing this is the way Rails is intended to work, but it is quite tedious when developing library files and/or plugins.

Surely I'm going about this wrong....?

Best

EDIT 1

Neither answer 1 nor 2 worked for me. Instead I was presented with errors from the controllers that made use of the Module. FYI, I have 3 files in my "lib/xmlitems" directory. I attempted to load that subdirectory then I referenced the single file that "requires" all other files. Am I to individually load all files?

Community
  • 1
  • 1
humble_coder
  • 2,777
  • 7
  • 34
  • 46
  • Looking at the duplicate target, none of the answers are particularly good. As duplicates require the question to be answered in the target, I'm voting to re-open. – Andrew Grimm Oct 11 '17 at 02:20

3 Answers3

14

For Rails 3 and Rails 4.0, vary the instructions given in @txwikinger's answer. In your environments/development.rb file, add the lines:

ActiveSupport::Dependencies.autoload_paths << File::join( Rails.root, 'lib')
ActiveSupport::Dependencies.explicitly_unloadable_constants << '<my modules in lib>'
JellicleCat
  • 28,480
  • 24
  • 109
  • 162
  • When I did this, I was using a module called ::Utils. When I ran my code, it gave me an error, `uninitialized constant ActionView::CompiledTemplates::Utils`. – Tyler Collier Aug 06 '12 at 02:52
  • Hm. Could you put your code on pastie.org and share a link? – JellicleCat Aug 06 '12 at 14:30
  • Turns out that was my own fault, although I thought I had isolated it using a git diff. Sorry JellicleCat, unfortunately Stackoverflow locked in my -1. :( I still can't verify your answer though. – Tyler Collier Aug 09 '12 at 05:18
  • 1
    Worked great, though I removed the brackets from the String that's appended to 'explicitly_unloadable_constants' – Alex Soto Dec 13 '12 at 20:03
  • 2
    This allows you to reload classes in the lib directory as well, confirmed for rails 4. Thanks! – random-forest-cat Nov 29 '13 at 16:23
  • *Thank you!* I've spent the last four hours scouring SO and google, trying so many different possibilities. Probably spent more time than I'll save with this working, but I persevered on principle -- autoreloading files between requests should be doable and easy! Your answer accomplishes just that on 3.2.13. Thanks again. – Sherwin Yu Dec 06 '13 at 08:05
  • 1
    Note that `explicitly_unloadable_constants` array is an array of constants (class names) but not file names. And one more trouble: on second reload I got an exception: `Circular dependency detected while autoloading constant MyClass`. – Paul May 14 '14 at 05:31
  • 1
    txwikinger's answer has since been deleted – Andrew Grimm Oct 11 '17 at 02:19
  • Glad I finally found this, works under Rails 4.2.10.... but what it the purpose for explicitly_unloadable_constants << 'MyModuleName' ? – jpw Mar 15 '19 at 09:21
-1
module ActsAsReloadable
  def self.included(base)
    ActiveSupport::Dependencies.explicitly_unloadable_constants << base.name if Rails.env == 'development'
  end
end

To use it, simply include ActsAsReloadable in your lib/* files and add config.autoload_paths += %W(#{config.root}/lib) in config/application.rb

choonkeat
  • 5,557
  • 2
  • 26
  • 19
-3

There's an easier way: just add

config.reload_plugins = true

to development.rb

novalis
  • 1,112
  • 6
  • 12