Adding a namespaced Rails generator to a standalone Ruby gem

I wanted to add a generator to the SugarCRM ruby gem, but couldn’t find any straightforward resources on doing just that: taking a standalone ruby gem, and adding a generator that is available from within a Rails environment. To make matters more interesting, I wanted this generator to be namespaced: instead of calling `config`, I wanted to call `sugarcrm:config`.

Before diving into this short explanation of how I implemented the namespaced generator within a ruby gem, it would be a good idea to read this rails guide on generators to have a general understanding of how the basic case works.

(For those of you who want just the code, here it is: https://github.com/chicks/sugarcrm/commit/183c1b193e6620431826c3b594c568d4592fb0af)

File storage

First things first: where should the generator files go? As explained in the generator rails guide, they should go into a `rails/generators` folder. But since I wanted a namespaced generator, I needed to used a slightly different path. I wanted the generator to be called with `rails generate sugarcrm:config`, so my path ended up being `rails/generators/sugarcrm/config/config_generator.rb` (note: the source files for the gem were in a `lib` folder, and all of these files and paths are also within that `lib` folder).

The templates for this generator were put in `rails/generators/sugarcrm/config/templates/`.

In other words, the base folder for the generator is `namespace/generator`, and will contain the actual generator and the templates folder.

Namespacing the generator

To namespace the generator, you need to put it within a module, like standard ruby code:

module Sugarcrm
module Generators
class ConfigGenerator < Rails::Generators::Base ... end end end [/code] And that all there is to it. You'll now be able to call `rails g sugarcrm:config` to execute the generator! (Make sure to check out the actual code to see the parts I didn’t cover in sufficient detail.)

This entry was posted in Rails, Ruby, SugarCRM Ruby gem. Bookmark the permalink.

2 Responses to Adding a namespaced Rails generator to a standalone Ruby gem

  1. Jakanapes says:

    I have been wrestling with this for days. I’ve tried all the various solutions I found on the web and cannot get a generator going in a gem.

    I generated my gem with bundler and have a simple generator:

    module Lorem
    module Generators
    class InstallGenerator < Rails::Generators::Base
    desc 'just a test'

    def create_initializer_file
    create_file Rails.root.join("config", "initializers", "test.rb"), "This is a test"
    end
    end
    end
    end

    It’s inside lib/rails/generators/lorem/install

    I rake install the gem, add it to the gemfile of a rails app and run rails g and I don’t see it at all.

    I feel like I’m missing some basic, vital piece of information in the various tutorial and examples.

  2. david says:

    It looks pretty good at a glance. Two things you might want to try:
    1. I’m not sure about the “install” portion of your path. Unless I’m mistaken, the file’s complete path should be lib/rails/generators/lorem/install_generator.rb. This is likely to be your issue.
    2. You probably don’t need to specify a source root since you’re not working with templates, but it’s worth a shot to exclude that being the problem

Comments are closed.