Search code examples
ruby-on-railsruby-on-rails-4namespacesrails-enginesrails-generate

Rails 4 engine with nested namespace


I could not find a way to generate engines with nested namespaces under rails. Every time I do that, I basically have to edit and move around the generated files by hand. Is there really no support for nested namespaces in rails? Seems unlikely.

At the company we namespace everything like this: CompanyName::SerivceName::Module

So when I'm working on Service1, and making the engine which will be integrated into the app that customer support uses to play around with the users and data of that service on customer requests, I'd like to create that engine under CompanyName::Serive1::CustomerSupport

However rails seems to be unable to do that.

  • Using rails plugin new a::b::blah is not accepted:

    akovanm0:test avandra$ rails plugin new a::b::blah -T --dummy-path=spec/dummy --mountable --full --mountable

    Invalid plugin name a::b::blah. Please give a name which use only alphabetic or numeric or "_" characters.

  • Specifying rails plugin new a/b/blah generates an engine, but has the same output as rails plugin new blah

  • Specifying rails plugin new a_b_blah generates an engine with the literal name a_b_blah, not namespaced. (and the actual name is camelcased to ABBlah)

What I'd like to achieve is an engine whose controllers, models and views are generated under the a::b::blah namespace, and it is mountable the same way. I want all generated controllers to go under app/controllers/a/b/blah, the models to go under app/models/a/b/blah, and so on...

Is there a way to achieve this?


Solution

  • For anyone reading this in 2022, I believe later versions of Rails handle engine names with dashes as separate namespaces.

    rails plugin new parent_engine-sub_engine --mountable

    Will create the following engine.rb:

    module ParentEngine
      module SubEngine
        class Engine < ::Rails::Engine
          isolate_namespace ParentEngine::SubEngine
        end
      end
    end