英文:
Zeitwerk? Ruby on Rails ::Domains::Article => /app/domains/domains/ariticle.rb
问题
我有存储在以下路径的领域逻辑代码
app/domains/domains/article.rb
类是
class Domains::Article
end
让我感到困扰的是正确的加载结构是 app/domains/domains/article.rb
而不是 app/domains/article.rb
有人知道正确的目录结构和相关的autoload_path是什么吗?
谢谢!
-daniel
英文:
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
得分: 1
可以通过以下方式完成:
# config/initializers/autoloading.rb
module Domains
end
Rails.autoloaders.main.push_dir(
"#{Rails.root}/app/domains",
namespace: Domains
)
# 下面的额外代码在 Rails < 7.1 中是必需的。
domains_dir = "#{Rails.root}/app/domains" # 必须是字符串
ActiveSupport::Dependencies.autoload_paths.delete(domains_dir)
Rails.application.config.watchable_dirs[domains_dir] = [:rb]
英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论