Rails 3 Quicktip: Autoload lib directory including all subdirectories, avoid lazy loading

Rails 3 Quicktip: Autoload lib directory including all subdirectories, avoid lazy loading

Posted on September 22, 2010

Rails 3 doesn’t autoload files under the lib directory anymore (aka lazy loading). There was quite a discussion about this controversial decision, while lazy loading can be very good and useful, it is also convenient to not have to include every file/folder manually. Fortunately, there is an easy way to enable autoloading again. While most solutions on the Internet only show how to load one directory, here is a solution thatautoloads lib including all subdirectories on startup.
Put this in config/application.rb:

      
      
# application.rb

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config . autoload_paths += %W( #{ config . root } /lib)
config . autoload_paths += Dir [ " #{ config . root } /lib/**/" ]
view raw application.rb This Gist brought to you by  GitHub.

你可能感兴趣的:(Directory)