How to exclude specific file from inside lib/ folder from eager_loading in ROR

129 views Asked by At

In my rails application inside lib/ folder I have many files I need to exclude one file from that lib folder to become eager_loading and remaining file should go eager_loading.

How can I achieve this?

  1. config.eager_load_paths += %W[#{config.root}/lib/]

    In point 1, load all files from lib folder,

  2. config.eager_load_paths -= %W[#{config.root}/lib/iq_analytics.rb]
    config.eager_load = true

    In point 2, I have changed this to exclude iq_analytics.rb this file from loading eager_load, but this line exclude all files from lib/ folder?

Any other way?

1

There are 1 answers

3
Berkan On

Rails >= 7.1

You can use config.autoload_lib(ignore:)

# config/application.rb

config.autoload_lib(ignore: %w(iq_analytics.rb))

Rails < 7.1 and zeitwerk enabled

# config/application.rb

lib_dir = Rails.root.join('lib')

config.autoload_paths << lib_dir.to_s
config.eager_load_paths << lib_dir.to_s

Rails.autoloaders.main.ignore(lib_dir.join('iq_analytics.rb').to_s)