views:

554

answers:

4

Hey guys,

I'm creating a new engine for a rails 3 application. As you can guess, this engine is in the lib directory of my application.

However, i have some problems developing it. Indeed, I need to restart my server each time I change something in the engine.

Is there a way to avoid this ?

Can I force rails to completely reload the lib directory or a specific file and his requirements for each request ?

Thanks for your help :)

A: 

Is your development environment really the dev environment? I'm assuming you're having to edit files on a host that's in a production environment?

In a production environment, change:

config/environments/production.rb

 config.cache_classes = false

But in dev, it should reload your code each request. Plugins are different, but code you write is reloaded each time.

Jesse Wolgamott
Yes, I'm using the dev environment :) cache_classes is effectively false. It does reload the models, controllers and view but not the files in the lib directory ! This is the Rails 2 way to reload the lib directory : http://www.williambharding.com/blog/rails/automatically-reload-modules-on-change-in-rails/ but i haven't been able to make it work with rails 3 !
Niklaos
ahhh, ok, now I understand. Adding a new answer below
Jesse Wolgamott
A: 

In RAILS 3, here's the secret sauce to auto-reload lib files. The code below's a bit overkill, for the example, but it's what I did to make it work. You can change the message in YoYo#gogo and see it on the screen each page load. Remove out the initializer and it stays the same.

/config/initializers/lib_reload.rb (new file)

ActiveSupport::Dependencies.explicitly_unloadable_constants << 'YoYo'
ActiveSupport::Dependencies.load_once_paths.delete(File.expand_path(File.dirname(__FILE__))+'/lib')

/lib/yo_yo.rb

class YoYo
  def gogo
    "OH HAI THERE"
  end
end

/app/controllers/home_controller

require 'yo_yo'
class HomeController < ApplicationController
  def index
    @message = YoYo.new.gogo
  end
end
Jesse Wolgamott
+1  A: 

You have to add

config.autoload_paths += %W(#{config.root}/lib)

to your Application class in config/application.rb

https://rails.lighthouseapp.com/projects/8994/tickets/5218-rails-3-rc-does-not-autoload-from-lib

dishod
A: 

note that in Rails 3 "load_once_paths" becomes "autoload_once_paths."

Also, it appears that it should be empty unless you explicitly put something in it.

Avram Dorfman