This weekend during Rails Bugmash I stumbled across some nice posts about Rails 3 generators which motivated me to share them and add some comments!
First, David Trasbo wrote a nice guide about how to make your first Rails 3 generator, it covers up all the basic steps including setting it up in a gem. He also puts the deserved attention into Thor::Actions
, which contains most helpers you need in a generator, like copy_file
, template
, create_file
, empty_directory
and so on.
On another post, Ben Scofield talks about apply
method, which is also in Thor::Actions
, and how to use it to dry your application templates.
Wait… so Thor::Actions
is used both in generators and in Rails application templates? Exactly, Rails’ new generators unifies both application templates and generators API into one. While Thor::Actions
holds basic methods, all Rails specific methods like environment
, rakefile
, generator
are in Rails::Generators::Actions. If you already wrote an application template, you will feel more at home when writing a Rails 3 generator.
Paul Barry talks how easy it’s to customize your scaffold to use Rspec, Haml and Factory Girl instead of Test::Unit, Erb and Fixtures. This all works because scaffold is just a meta generator which provides hooks for template engine, test framework, ORM and so forth. A good way to see the hooks system working is by running script/generate scaffold --help
before and after Paul changes, so you can see exactly how generators options update depending on the configuration values you set. While I wrote Rspec generators used in the example, he implemented himself Haml and Factory Girl generators and they can all be used as example if you plan to build your own.
Finally, Zigzag Chen wrote about templates customization and how you can change your scaffold controller to use json instead of the xml format. New generators have source paths, so you can customize them simply by copying files to RAILS_ROOT/lib/templates.
Rails Bugmash was excellent to gather feedback and we also got some tickets on Lighthouse, mainly about how generators help can be improved for people starting with Rails. Many thanks to Rails Bridge and besides the posts linked above, there is a generator guide, which may help you get started and maybe write your own post as well. 🙂
[…] Discovering Rails 3 generators […]
[…] Discovering Rails 3 generators (01/18/10) — A few important points to know about Rails 3 generators. […]
[…] continued to be a stream of Rails 3 focussed blog entries. José Valim has a good round up of links regarding generators in Rails 3 – it looks like they and templates (now merged) could be much more useful than they have been […]
[…] Discovering Rails 3 generators […]
[…] Making Generators for Rails 3 With Thor – David Trasbo demonstrates how to use Thor (essentially a better Rake – sorta) to produce new generators for Rails 3 projects. This is a code heavy walkthrough. There's more on generators here. […]
[…] Criando geradores para Rails 3 com Thor – David Trasbo mostra como usar o Thor, que é uma espécie de Rake melhorado, para criar novos geradores para projetos em Rails 3. É um tutorial com muito código e você pode ler mais sobre geradores aqui. […]
[…] Making Generatoren für Rails 3 Mit Thor – David Trasbo veranschaulicht, wie Thor verwenden (im Wesentlichen eine bessere Rake – sorta) auf die neuen Generatoren für Rails produzieren 3 Projekte. Dies ist ein Code schwere Lösungsweg. Es gibt mehr auf Generatoren hier. […]
[…] Making Generatoren für Rails 3 Mit Thor – David Trasbo veranschaulicht, wie Thor verwenden (im Wesentlichen eine bessere Rake – sorta) auf die neuen Generatoren für Rails produzieren 3 Projekte. Dies ist ein Code schwere Lösungsweg. Es gibt mehr auf Generatoren hier. […]