bodacious
bodacious

Reputation: 6695

How can I reload files in /lib on every request?

Rails loads controllers, helpers and models on each request.

My controllers have a bunch of modules which include methods for shared actions

Each time I change the modules, I have to restart Rails for the changes in the actions to take effect

Any idea how I can tell rails to reload these modules too?


Update:

My directory structure is like so:

  app/
    controllers/
      app1/
        users_controller.rb
      app2/
        users_controller.rb

  lib/
    templates/
      controllers/
        users_controller_template.rb

Both App1::UsersController and App2::UsersController load UsersControllerTeplate like so:

# app/controllers/app1/users_controller.rb
class App1::UsersController < App1::ApplicationController
  require "templates/controllers/users_controller_template"
  include Templates::Controllers::UsersControllerTemplate
end

# templates/controllers/users_controller_template.rb
module Templates::Controllers::UsersControllerTemplate

  def self.included(base)
    base.class_eval do
      # some class macros called here
    end
  end
  
  # actions defined here
  def index
  end

end

In application.rb I've added:

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

But I still have to reload the server to see changes made in users_controller_template.rb

Any ideas?

Upvotes: 3

Views: 1748

Answers (2)

Jits
Jits

Reputation: 9728

In config/application.rb:

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

This will reload any Ruby files in your lib folder as well as any Ruby files within subdirectories of lib.

Upvotes: 2

Pan Thomakos
Pan Thomakos

Reputation: 34340

You can add them to your autoload_paths in your application.rb file and they will be re-loaded automatically along with the models and controllers.

config.autoload_paths << "#{config.root}/lib"

Upvotes: 3

Related Questions