Inflections: How to use custom acronyms when creating new application

Let’s say I’m working on a product called Friendly Organizational Operator, for which we use FOO as the acronym. I want to create a new engine to encapsulate a particular piece of functionality we’ll call Bar (I used all my creativity on that first one). Naturally, my engine will be called foo-bar with a namespaced name of foo/bar. However, the root module name will be Foo, when in reality I’d really like it to be FOO. For example:

$ rails plugin new foo-bar --mountable
  create  
  create  README.md
  create  Rakefile
  create  foo-bar.gemspec
  create  MIT-LICENSE
  create  .gitignore
  <...snip...>

$ cat foo-bar/lib/foo/bar/engine.rb 
module Foo
  module Bar
    class Engine < ::Rails::Engine
      isolate_namespace Foo::Bar
    end
  end
end

I realize that I need to actually tell Rails about this acronym. Generally I do that with an initializer in the engine:

module FOO
  module Bar
    class Engine < ::Rails::Engine
      isolate_namespace FOO::Bar

      initializer 'setup_inflections' do
        ActiveSupport::Inflector.inflections do |inflect|
          inflect.acronym 'FOO'
        end
      end
    end
  end
end

Then the rest falls into place just fine (e.g. autoloading). Of course, I need to run a big sed search/replace on the entire engine first.

So here’s my question: is there a way for me to generate that initial engine using the proper acronym off the bat instead of needing to sed after I’ve created it? As it happens I’m using application templates anyway, so I’m just doing the sed in there, but I’m not a huge fan. Can I tell Rails about my acronym before it generates the engine? Perhaps there is some functionality in the application template API that I’ve missed? A hook that might run before the actual generation, maybe (otherwise my template always runs after the engine is already generated)?

Thanks for your time!

2 Likes

I have the same question too. Does anyone know the answer?

2 Likes

Sure, this early acronym inflection is possible! Here are the steps:

  1. Find the path to your current rails command by running which rails:
% which rails
/Users/lorin/.rvm/gems/ruby-3.2.0/bin/rails
  1. Open this listed file in a text editor. If VSCode is in the path you can combine steps 1 and 2 like this – note the back-ticks:
code `which rails`
  1. Add these two lines before the require 'rubygems' line and then save:
require 'active_support'
ActiveSupport::Inflector.inflections { |inflect| inflect.acronym 'FOO' }
  1. You can now run (and note the casing):
rails plugin new FOO-bar --mountable
  1. The project will be built in the foo-bar folder, with module FOO. You would now probably want to go back and re-edit your rails executable to remove the acronym.

It works. But there is a more comfortable way without editing the rails bin file?

Thanks @Lorin_Thwaits, but I think my sed approach is cleaner :stuck_out_tongue: . I was hoping this might actually be supported in some way.