Rails 7.1 adds the ability to autoload the lib directory

railsNovember 22, 2023Dotby Alkesh Ghorpade

The lib directory in a Rails application is used for storing custom modules, utility classes, and other reusable code that doesn't belong in the standard application directories like app, config, or test.

Prior to Rails 7.1, the lib directory was not automatically included in the autoload paths. Hence, you need to explicitly require any classes or modules defined in this directory before you can use them.

Before Rails 7.1

To autoload files from the lib directory in Rails versions before 7.1, use the eager_load_paths or autoload_paths configuration setting. This setting allows you to specify a list of directories that Rails should eagerly load, which will be loaded before the application boots up.

To use eager_load_paths or autoload_paths, add the following line to your config/application.rb file:

# config/application.rb

config.autoload_paths << Rails.root.join('lib')
# or
config.eager_load_paths << Rails.root.join('lib')

If you want to ignore a few subdirectories, use the Rails.autoloaders.main.ignore method. Let's say you have generators, assets, and tasks under the lib directory; you can exclude these from autoloading as below:

# config/application.rb

["generators", "assets", "tasks"].each do |subdirectory|
  Rails.autoloaders.main.ignore("#{Rails.root}/lib/#{subdirectory}")
end

In Rails 7.1

In Rails 7.1, a new configuration method was called config.autoload_lib is introduced, simplifying the autoloading files from the lib directory. This method takes an optional ignore: argument, which allows you to specify specific subdirectories within the lib directory that should not be autoloaded.

To enable autoloading for the entire lib directory, you can add the following line to your config/application.rb file:

config.autoload_lib

If you want to exclude certain subdirectories from autoloading, you can specify them using the ignore: option:

config.autoload_lib(ignore: %w[tasks assets])

This will prevent Rails from autoloading files from the tasks and assets subdirectories within the lib directory.

The config.autoload_lib method provides a more convenient and flexible way to manage autoloading for the lib directory, making it easier to keep your code organized and maintainable.

To know more about this feature, please refer to this PR.

Closing Remark

Could your team use some help with topics like this and others covered by ShakaCode's blog and open source? We specialize in optimizing Rails applications, especially those with advanced JavaScript frontends, like React. We can also help you optimize your CI processes with lower costs and faster, more reliable tests. Scraping web data and lowering infrastructure costs are two other areas of specialization. Feel free to reach out to ShakaCode's CEO, Justin Gordon, at [email protected] or schedule an appointment to discuss how ShakaCode can help your project!
Are you looking for a software development partner who can
develop modern, high-performance web apps and sites?
See what we've doneArrow right