即将推出的 Rails 7 代表了自动加载的一个里程碑。
将有两项重要更改
Zeitwerk 已经成为默认的自动加载器,已经 两年多了。Rails 6.0 和 Rails 6.1 支持 zeitwerk 和 classic 模式,以帮助项目过渡。这一时期随着 Rails 7 结束:classic 模式将不再可用。
初始化器可以在 to_prepare 块中包装以自动加载可重新加载的常量,但除此之外它们不再可以。
也许您的 6.x 应用程序已准备好应对这些更改。否则,您可以提前准备以简化升级。让我们简要探讨一下它们的影响。
zeitwerk 模式下仍运行在 classic 模式下的应用程序必须切换到 zeitwerk 模式。
不要害怕,许多非简单的 Rails 应用程序报告了非常顺利的切换。您很可能只需要切换开关,也许配置一些 inflector,然后就完成了。请查阅 Rails 6.0 升级指南了解详情。
如果您发现任何意外情况,我很乐意提供帮助,只需 打开一个 issue 并标记 @fxn。
在 Rails 7 中,没有配置点来设置自动加载模式,config.autoloader= 已被删除。
您不会宣布对内部 API 的更改,但由于 classic 自 Rails 发布以来一直存在,因此值得在此文中提及。
ActiveSupport::Dependencies 实现的 classic 自动加载器,随着它的移除,许多内部方法也级联删除,例如 hook!, unhook!, depend_on, require_or_load, mechanism, qualified_name_for, warnings_on_first_load, logger, verbose,以及许多其他。
辅助内部类或模块也已消失,例如 Reference, ClassCache, ModuleConstMissing, Blamable 等。
active_support/dependencies.rb 大约 90% 的内容已被删除。您可以将 edge 中的版本与 6.1 中的版本进行比较。
在初始化期间,在 to_prepare 块外部自动加载可重新加载的常量的应用程序,自 Rails 6.0 以来,这些常量已被卸载并发出此警告。
DEPRECATION WARNING: Initialization autoloaded the constant User.
Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.
Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload User, for example,
the expected changes won't be reflected in that stale Class object.
This autoloaded constant has been unloaded.
In order to autoload safely at boot time, please wrap your code in a reloader
callback this way:
Rails.application.reloader.to_prepare do
# Autoload classes and modules needed at boot time here.
end
That block runs when the application boots, and every time there is a reload.
For historical reasons, it may run twice, so it has to be idempotent.
Check the "Autoloading and Reloading Constants" guide to learn more about how
Rails autoloads and reloads.
(called from ...)
如果您仍然收到此警告,请在 自动加载指南中查看有关应用程序启动时自动加载的部分。否则,您会在 Rails 7 中遇到 NameError。
希望支持 Rails 6.x 的引擎可以检查
Rails.autoloaders.zeitwerk_enabled?
以了解父应用程序是否运行在 zeitwerk 模式下。此谓词在 Rails 7 中仍然存在,用于此用例。