如何忽略Zeitwerk for Rails 6中的文件夹?

时间:2019-09-26 23:21:37

标签: ruby-on-rails ruby ruby-on-rails-6

一个简单的问题,但是不知何故,答案就逃不过我了。

在与Zeitwerk一起使用Rails 6时,我得到:

export interface AddSchemaPageProps {
  componentId: string;
}

const AddSchemaPage: NextPage<AddSchemaPageProps> = function ({
  componentId
}) {
  const dispatch = useDispatch();

  const handleSave = useCallback(
    async (data: Config6CreateSchemaDto) => {
      const schema = await dispatch(addSchema(componentId, data));

      if (schema) {
        Router.push(
          '/config/components/[componentId]/schemas/[schemaId]',
          `/config/components/${componentId}/schemas/${schema.id}`
        );
      }
    },
    [dispatch]
  );

  return (
    <Layout
      variant="config"
    >
      <h3>Add Schema</h3>
      <SchemaBuilder 
        onSave={handleSave}
      />
    </Layout>
  );
};

这似乎很棒:这是一个垃圾文件夹,永远不要加载,因此忽略它是很有意义的。

位于https://github.com/fxn/zeitwerk的Zeitwerk文档说

Please, check the "Autoloading and Reloading Constants" guide for solutions.
 (called from <top (required)> at APP_ROOT/config/environment.rb:7)
rails aborted!
Zeitwerk::NameError: wrong constant name Enforce-calls-to-come-from-aws inferred by Module from directory

  APP_ROOT/app/junkyard/enforce-calls-to-come-from-aws

Possible ways to address this:

  * Tell Zeitwerk to ignore this particular directory.
  * Tell Zeitwerk to ignore one of its parent directories.
  * Rename the directory to comply with the naming conventions.

是您忽略文件夹的方式。很公平。但是如何找到tests = "#{__dir__}/**/*_test.rb" loader.ignore(tests) loader.setup ?关于Zeitwerk自动加载(https://guides.rubyonrails.org/autoloading_and_reloading_constants.html)的Rails指南没有提到如何直接忽略文件夹,但是提到了存放在loader上的自动加载器,因此我认为

Rails.autoloaders.main

Rails.autoloaders.main.ignore("#{__dir__}/junkyard/**/*.rb")

将是要走的路。没运气。我尝试将其放在Rails.autoloaders.main.ignore("#{__dir__}/app/junkyard/**/*.rb") application.rb中,但都没有用。

有什么想法在Rails中在哪里以及如何忽略Zeitwerk的文件夹?

PS:是的,我知道我应该将其从initializers/zeitwerk.rb中删除,我会的。但是问题仍然存在。

1 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,事实证明它在抱怨文件夹名称。

将其添加到application.rb可能对您有用:

Rails.autoloaders.main.ignore(Rails.root.join('app/junkyard'))