首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用具有类冲突的ActiveSupport::Inflector

使用具有类冲突的ActiveSupport::Inflector
EN

Stack Overflow用户
提问于 2020-08-27 00:54:01
回答 1查看 90关注 0票数 0

SendGrid设置词形变化意味着我们可以为Rails的文件命名方案设置一个例外。我们可以将模型命名为SendGrid...,而不必将文件命名为send_grid_...。然而,它在这里引入了一个与依赖相关的问题。我使用的是sendgrid-actionmailer gem,它使用模块名称ActionMailbox::Ingresses::Sendgrid注册入口,但是前面的首字母缩写定义意味着查找需要SendGrid

config/initializers/inflections.rb

代码语言:javascript
复制
# frozen_string_literal: true

ActiveSupport::Inflector.inflections(:en) do |inflect|
  ...
  inflect.acronym 'SendGrid'
end

在生产环境中运行时,我使用sendgrid-actionmailer:

代码语言:javascript
复制
/app/vendor/bundle/ruby/2.7.0/gems/actionmailbox-6.0.3.2/app/controllers/action_mailbox/ingresses/sendgrid/inbound_emails_controller.rb:47:in `<module:ActionMailbox>': uninitialized constant ActionMailbox::Ingresses::Sendgrid (NameError)
Did you mean?  ActionMailbox::Ingresses::SendGrid
SendGrid

A maintainer suggests this problem relates more to ActiveSupport than sendgrid-actionmailer,我同意。他建议,临时解决这个问题的方法是删除缩写词声明,而使用Sendgrid。有没有更持久的解决方案,可以让我们把首字母缩写声明留在里面?

EN

回答 1

Stack Overflow用户

发布于 2020-08-27 20:56:14

ActiveSupport::Inflections上定义词形变化会影响全局加载它们的方式。多亏了Rails docs,我发现你可以在Rails的加载器上重写变形。我最终需要将以下代码添加到初始化器(config/initializers下的.rb文件):

代码语言:javascript
复制
# frozen_string_literal: true

Rails.autoloaders.each do |autoloader|
  autoloader.inflector.inflect(
    'sendgrid' => 'Sendgrid'
  )
end

这将恢复加载器所知道的默认值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63602277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档