Dr Nic

Where art thou generator?

Summary: click to select

UPDATE: Rails Generators within RubyGems support is now in Rails Edge

Generators in Rails are awesome. No? So you don’t use the rails command to create a new rails app? Nor the model, migration, or plugin generators? Yeah, they are awesome. Hug the next one you meet.

On my part-time quest to replace the use of Plugins with RubyGems within Rails, it occurred to Geoffrey Grosenbach [1] and myself that there is no easy way to bundle rails generators within RubyGems. Patooey, we say!

Currently, there are several places you can store Rails generators:

  • Builtin ones – like model, scaffold etc – are stored in the rails gem.
  • ~/.rails/generators/ for your own personal generators.
  • Rubygems whose name ends with _generator, such as ajax_scaffold_generator
  • vendor/generators/
  • vendor/plugins/_some_plugin_/generators/ – the most common mechanism for deploying generators

But no way to bundle multiple generators in a RubyGem or have a gem without _generator in its name. And Rails has laid claim to the _generator namespace, which is a bit greedy.

This is outrageous!

Damn it, you’re right! Its now in Rails Edge!

rails newapp
cd newapp
rake rails:freeze:edge

So try out this new Rails Core patch. Never touched a raw patch before? Nevermind. To play with it, let’s create a new rails app:

cd vendor/rails/railties
wget http://dev.rubyonrails.org/attachment/ticket/8730/gem_generators.patch?format=txt -O gem_generators.patch
patch -p0 < gem_generators.patch
cd ../../..

The world’s first Generator-Enabled RubyGem is…

So you can play with this fabbo new feature and explore how to add generators to your RubyGems, Rick Olson’s restful_authentication plugin has been ported into a RubyGem. It is currently available for manual download and will soon be live on RubyForge.

wget http://drnicwilliams.com/wp-content/ruby/gems/restful_authentication-1.0.0.gem
gem install restful_authentication
ruby script/generate

And you’ll see the authenticated generator is discovered from the RubyGem. Neat. Note, we didn’t install the old plugin within the rails app.

So, now let’s play with it…

ruby script/generate authenticated user

It works exactly like it did from the original plugin. But now its a RubyGem, and you never ever need to reinstall this plugin for each new app, ever again. Ever.

Merge plugin generators into RubyGems

There exists a RubyGem daemons, and there exists a separate plugin, that makes it easy to use daemons via your rails app.

This separation will be no longer necessary!

Now the generator code can be moved into the daemons gem itself.

Have a look at the plugin, and note the generators folder. Simply copy this folder into the daemons gem. Then rename this folder to rails_generators. Repackage and release.


PS. To the owner of daemons repository, come on dude! Upgrade from CVS to SVN:)

Want to write your own generators?

There is some useful info on writing generators on the tubes. Start there.

In all cases, (for an example “foo” generator) the generator file must be called foo_generator.rb, and it must contain a class called FooGenerator.

Does it work with “vendor everything?”

Normal people living normal lives won’t know there exists a Rails Core ticket to support “vendor everything”, created by Marcel Molina Jr [2] (and his backing singer Dave Thomas).

This allows you to store gem files in a /vendor/gems folder, and it will be super magically exploded into the /vendor/gems/home folder. That is, you can freeze raw gems into your rails source structure.

To test this, first uninstall the restful_authentication gem and then reload it into the /vendor/gems folder:

gem uninstall restful_authentication
cd vendor/rails/railties
wget http://dev.rubyonrails.org/attachment/ticket/8511/gems_loaded_from_vendor_like_plugins.diff?format=txt -O gems_loaded.patch
patch -p0 < gems_loaded.patch
cd ../../..
ruby script/generate

Do this just to confirm that the authenticated generate is no longer available.

cd vendor
ls .

Note that you now have a gems folder, and within it you have a home folder, which itself stores lots more new folders. This is Marcel’s patch in action!

cd gems
wget http://drnicwilliams.com/wp-content/ruby/gems/restful_authentication-1.0.0.gem
cd ../..
ruby script/generate

And the authenticated generator is back! Discovered auto-magically from within your own rails source! Investigate the vendor/gems/home folders at your leisure.

So this article is about two things…

Herein layeth a tale of two tickets (first one is now in Rails Edge). Sneaky, nay?

Please try them out and comment within each ticket about any praise/thoughts/fixes/outright-damnations so the nice Rails Core team (esp. to DHH who is not sure if “vendor everything” is something that should be in Rails Core [3]) know this is an important direction for Rails to go.

[1] Normally I’d reference/link
Nuby On Rails or Peepcode, but I just love the “half a head” self-photo on his home page.

[2] More info about Marcel on
Rails Core page.

[3] Based on a chat in irc #rails-contrib the other day, when that ticket was first created.