3

I'm confused.

A gem that I used, called railsy_backbone, provides sweet namespaced generators. From the command-line I can invoke them thus,

$ rails g backbone:model widget

Now, it is time for me to make sweet generators, but I'm having trouble with the namespacing. I've done my research, I've read this, and this, and even this. The structure I ended up with is this:

lib/generators/
└── marionette
    └── model
        ├── model_generator.rb
        ├── templates
        └── USAGE

The contents of model_generator.rb are,

module Marionette
  module Generators
    class ModelGenerator < Rails::Generators::NamedBase
      source_root File.expand_path('../templates', __FILE__)
    end
  end
end

Which mirrors all examples I've read. For comparison, here is the lib/generators a partial tree of the railsy_backbone gem:

generators/
└── backbone
    ├── bob
    │   ├── bob_generator.rb
    │   └── templates
    │       └── model.coffee
    ├── helpers.rb
    ├── model
    │   ├── model_generator.rb
    │   └── templates
    │       └── model.coffee

I actually added backbone:bob myself, which subsequently shows up in the list of generators under the namespace backbone when I run rails g sans arguments. Here, as well, are the contents of model_generator.rb,

require 'generators/backbone/helpers'

module Backbone
  module Generators
    class ModelGenerator < Rails::Generators::NamedBase
      include Backbone::Generators::Helpers

      source_root File.expand_path("../templates", __FILE__)

      // the implementation

    end
  end
end

The only difference I can see between my code and theirs, is that they have packaged their generators in a gem. My generators live in the rails app that I'm working on for the time-being. For now I've given up, and am just going to make a bunch of top-level generators. Eventually, when I'm ready to use them, I will package them as a gem and install it. I expect that will work. However, I'd really like to know how to namespace generators without having them live in a gem. Anyone have any relevant experience?

Community
  • 1
  • 1
Ziggy
  • 21,845
  • 28
  • 75
  • 104
  • I would also love to know how to do this, because I've got a lot of generators on my platform that are all separate right now. – kddeisz Dec 02 '13 at 14:35
  • Ziggy, this might be useful: https://github.com/lastobelus/cleanthor. You can also look in devcloudcoder. – Michael Johnston Dec 02 '13 at 20:11

1 Answers1

2

Use:

$ rails generate generator your_gem_name/generator_name

Then check:

$ rails generate your_gem_name:generator_name --help

papar
  • 1,023
  • 9
  • 17