(Rails) Reloading "lib" files without having to restart server...? [duplicate]
Asked Answered
B

3

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?

Buhrstone answered 11/7, 2009 at 18:45 Comment(1)
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.Ruse
U
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>'
Underbelly answered 8/6, 2012 at 18:40 Comment(9)
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.Haplography
Hm. Could you put your code on pastie.org and share a link?Underbelly
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.Haplography
Worked great, though I removed the brackets from the String that's appended to 'explicitly_unloadable_constants'Alicealicea
This allows you to reload classes in the lib directory as well, confirmed for rails 4. Thanks!Aruwimi
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.Milklivered
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.Salesroom
txwikinger's answer has since been deletedRuse
Glad I finally found this, works under Rails 4.2.10.... but what it the purpose for explicitly_unloadable_constants << 'MyModuleName' ?Showiness
T
-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

Transmigrant answered 21/12, 2011 at 6:52 Comment(0)
J
-3

There's an easier way: just add

config.reload_plugins = true

to development.rb

Jacks answered 9/5, 2012 at 21:4 Comment(1)
This didn't work for me.Haplography

© 2022 - 2024 — McMap. All rights reserved.