ruby-on-rails 使用Packwerk和ActiveAdmin(移动模型文件夹后的单位化常数)

wnrlj8wa  于 11个月前  发布在  Ruby
关注(0)|答案(2)|浏览(134)

我在rails 6应用程序中使用packwerk来加强新组件之间的边界(但我认为问题更多地与将活动管理员使用的模型移出app/models文件夹有关)。
Rails项目的旧设置:

app/
 ...
 models/
  subscription.rb
 ...
 services/
  subscription_service.rb
bin
config
...

字符串
成为

app
bin
components/
  subscription/ 
    app/
      public/
        subscription_service.rb
  spec/
    subscription_service_spec.rb
  package.yml
config
...


然后,我自动加载config/application.rb中的公共文件

class Application < Rails::Application
  ...
  config.autoload_paths += Dir[Rails.root / "components/*/app/public"]
  ...
end


现在,当我运行bundle exec rspec components/subscriptions/spec/subscription_service_spec.rb时,测试通过了,一切似乎都很好。
当我添加订阅模型时,结构将如下所示:

app
bin
components/
  subscription/ 
    app/
      public/
        subscription_service.rb
  models/
    subscription.rb # no longer in app/models
  spec/
    subscription_service_spec.rb
  package.yml
config
...


我得到这个错误:

An error occurred while loading ./components/subscriptions/spec/subscription_service_spec.rb.
Failure/Error:
  ActiveAdmin.register Subscription do
    menu false

    includes :pauses, :invoices

    config.remove_action_item(:edit)
    config.remove_action_item(:destroy)

    filter :id
    filter :status

NameError:
  uninitialized constant Subscription


我认为这可能与ActiveAdmin如何自动加载有关,但我正在努力寻找答案-因此,任何帮助/线索都将非常感谢

ovfsdjhp

ovfsdjhp1#

啊,我想明白了,我忘记导入application.rb中的models文件夹,现在应该是这样的:

class Application < Rails::Application
  ...
  config.autoload_paths += Dir[Rails.root / "components/*/app/public"]
  config.autoload_paths += Dir[Rails.root / "components/*/models"]
  ...
end

字符串

kmbjn2e3

kmbjn2e32#

几年后,我使用packs-rails,但必须添加以下活动管理初始化程序:

config.load_paths << File.join(Rails.root, "packages", "*", "app", "admin")

字符串
config/application.rb

Rails.autoloaders.main.ignore("#{Rails.root}/packages/*/app/admin")


您可能需要将packages替换为您自己的顶级名称。

相关问题