Home > other >  Rails 6 Zeitwerk, loading files without class
Rails 6 Zeitwerk, loading files without class

Time:03-26

We are upgrading to Rails 6, and have done most of the work to make our application work with Zeitwerk.

We do have two files, which are query objects, existing under app/queries and we don't define a class for them. They exists with methods only.

However, Zeitwerk expects the file to define a constant, but we don't deem it necessary.

Is there any way to tell Zeitwerk that we want to load these files as is?

CodePudding user response:

You can tell the main autoloader to ignore them:

# config/initializers/zeitwerk.rb
Rails.autoloaders.main.ignore(
  Rails.root.join("app/queries/foo.rb"),
  Rails.root.join("app/queries/bar.rb")
)

And then the application is responsible for loading them using require or whatever.

If everything under app/queries should be ignored, you can ignore the directory itself:

# config/initializers/zeitwerk.rb
Rails.autoloaders.main.ignore(Rails.root.join("app/queries"))
  • Related