简体   繁体   English

覆盖来自另一个引擎的模型的 Rails 引擎未通过 zeitwerk 检查

[英]A Rails Engine that overrides models from another engine isn't passing zeitwerk checks

I am creating two engines.我正在创建两个引擎。 Engine Two builds on Engine One .引擎二建立在引擎一之上。 I am using both of these Engines in an application I'm building.我在我正在构建的应用程序中使用这两个引擎。 I used the class_eval Engine overriding technique mentioned in the rails guide.我使用了 rails 指南中提到的class_eval引擎覆盖技术

# app/models/active_stix/tool_override.rb
ActiveStix::Tool.class_eval do
# Model override code here
end

The override is required from lib/engine_name/engine.rb as follows lib/engine_name/engine.rb 需要覆盖,如下所示

    initializer "active_attack.update_stix" do |app|
      engine_root = File.expand_path("../../..", __FILE__)
      Dir.glob(engine_root + "/app/overrides/**/*_override*.rb").each do |c|
        require_dependency(c)
      end
    end

Everything works as expected in development, but when I run rails zeitwerk:check or try to deploy the application to Heroku, I get the following error在开发中一切正常,但是当我运行rails zeitwerk:check或尝试将应用程序部署到 Heroku 时,我收到以下错误

expected file ~/.rvm/gems/ruby-2.7.0/gems/active_attack-0.1.14/app/overrides/models/active_stix/tool_override.rb to define constant Models::ActiveStix::ToolOverride预期文件 ~/.rvm/gems/ruby-2.7.0/gems/active_attack-0.1.14/app/overrides/models/active_stix/tool_override.rb 来定义常量 Models::ActiveStix::ToolOverride

This seems to be related to Rails issue 36100 , but that issue seems to describe how the main Rails app can override Engine models.这似乎与Rails 问题 36100有关,但该问题似乎描述了主要的 Rails 应用程序如何覆盖引擎模型。 It doesn't seem to work for one Engine overriding another Engine's models.它似乎不适用于一个引擎覆盖另一个引擎的模型。

Answered in Github IssueGithub 问题中回答

Rails.autoloaders.main.ignore(absolute/path/to/app/overrides) ignores the overrides directory. Rails.autoloaders.main.ignore(absolute/path/to/app/overrides)忽略覆盖目录。

config.autoloader =:classic uses previous Rails autoloader instead of zeitwerk. config.autoloader =:classic使用以前的 Rails 自动加载器而不是 zeitwerk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM