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

huangapple go评论64阅读模式
英文:

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(
  &quot;#{Rails.root}/app/domains&quot;,
  namespace: Domains
)

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

huangapple
  • 本文由 发表于 2023年5月15日 05:20:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定