Zeitwerk? Ruby on Rails ::Domains::Article => /app/domains/domains/ariticle.rb

100 Views Asked by At

I have domain logic code stored in

app/domains/domains/article.rb

The class is

class Domains::Article
end

It bothers me that the proper loading structure is app/domains/domains/article.rb and not app/domains/article.rb

Does anyone know what the proper directory structure and related autoload_path is?

Thank you!

-daniel

1

There are 1 best solutions below

2
Xavier Noria On

It can be accomplished this way:

# config/initializers/autoloading.rb

module Domains
end

Rails.autoloaders.main.push_dir(
  "#{Rails.root}/app/domains",
  namespace: Domains
)

# The next additional lines are necessary in Rails < 7.1.
domains_dir = "#{Rails.root}/app/domains" # has to be a string
ActiveSupport::Dependencies.autoload_paths.delete(domains_dir)
Rails.application.config.watchable_dirs[domains_dir] = [:rb]