Kyle Fox
Kyle Fox

Reputation: 3313

How to configure Zeitwerk to work with acronym inflections?

My Rails app is using an engine that defines an acronym inflection:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym "UI"
end

This means (for example) that Rails will load UIHelper from helpers/ui_helper.rb rather than UiHelper.

Now I want to user a gem ("motor-admin") but I'm getting this error:

uninitialized constant Motor::UIController

This is because the gem's class name is Motor::UiController but the inflections are looking for Motor::UIController (note the Ui vs UI).

I've tried following the Customizing Inflections in the Rails Guides, but can't seem to make the error go away.

How can I configure Rails to correctly load Motor::UiController with the "UI" acronym inflection?

Upvotes: 1

Views: 959

Answers (1)

oneWorkingHeadphone
oneWorkingHeadphone

Reputation: 899

Can you try the following:

Rails.autoloaders.each do |autoloader|
  autoloader.inflector.inflect(
    'ui_controller' => 'UIController'
  )
end

edit to add: you might have to do this for the models as well. If you run bin/rails zeitwerk:check it will tell you the files that still need to be changed.

I haven't tried to inflect an included gems controller names, but I did do an upgrade from 5.2 to 7.0 and had to do this for my own controller names which were in the same form that Motor seems to be taking.

Upvotes: 3

Related Questions