Rails 6 +,zeitwerk自动加载器和命名空间常量 [英] Rails 6+, zeitwerk autoloader and namedspaced constants

查看:190
本文介绍了Rails 6 +,zeitwerk自动加载器和命名空间常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 6+默认自动装带器是zeitwerk,这似乎比以前的方法有了很大的改进.

The Rails 6+ default autoloader is zeitwerk, which seems like a great improvement over previous approaches.

但是,zeitwork遵循Rails项目的约定,app/*中的任何内容都是自动加载的,不需要命名空间.

However, zeitwork follows the convention for Rails projects that anything in app/* is autoloaded and doesn't need to be namespaced.

这对于app/models/user.rb非常有用,因为您不必使用Models::User,而只需引用User.

This works great for app/models/user.rb because you don't have to use Models::User but can just reference User.

但是,我添加了自己的app/services目录,并将服务对象命名为Services::Users::Create,这会映射到app/services/users/create.rb.

However, I added my own app/services directory and I namespace my service objects as Services::Users::Create, which would map to app/services/users/create.rb.

Zeitwork抛出了我的类常量不存在的错误,因为它期望的是Users::Create(不带Services::前缀).

Zeitwork is throwing errors that my class constants don't exist, since it is expecting Users::Create (without the Services:: prefix).

在这些实例中,是否仍然需要配置zeitwork以要求Services::命名空间?我认为,将代码读取为Services::Users::Create并知道您正在查找app/services/users/create.rb文件是一种更加清洁的方法.

Is there anyway to configure zeitwork to require the Services:: namespace in these instances? In my opinion, it is a much cleaner to read the code as Services::Users::Create and know that you're looking in the app/services/users/create.rb file.

如果您只有Users::Create,则一般的Rails开发人员可能会寻找app/models/users/create.rb文件.

If you just had Users::Create, an average Rails developer would probably look for the app/models/users/create.rb file.

我不喜欢使用Users::CreateService命名的方法,对我来说似乎很不雅.

I don't like the approach of naming it Users::CreateService, it just seems very inelegant to me.

我不是唯一使用这种约定的人.还有其他人遇到解决方案吗?我仍在浏览所有zeitwerk文档以寻找解决方案,但尚未找到解决方案.

I can't be the only one who uses conventions like this; has anyone else come across a solution? I'm still going through all the zeitwerk documentation looking for a solution but haven't found one yet.

推荐答案

https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#having-app-in-the-autoload-paths

一些项目想要app/api/base.rb之类的东西来定义API :: Base,然后将app添加到自动加载路径中以经典模式完成该任务.由于Rails会自动将应用程序的所有子目录添加到自动加载路径中,因此我们还有另一种情况,即存在嵌套的根目录,因此设置不再起作用.我们在上文中曾解释过类似的原则,但很令人担忧.

Some projects want something like app/api/base.rb to define API::Base, and add app to the autoload paths to accomplish that in classic mode. Since Rails adds all subdirectories of app to the autoload paths automatically, we have another situation in which there are nested root directories, so that setup no longer works. Similar principle we explained above with concerns.

如果要保留该结构,则需要从初始化程序的自动加载路径中删除该子目录:

If you want to keep that structure, you'll need to delete the subdirectory from the autoload paths in an initializer:

ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/api")

这篇关于Rails 6 +,zeitwerk自动加载器和命名空间常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆