{"id":654,"date":"2010-01-18T15:50:25","date_gmt":"2010-01-18T17:50:25","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=654"},"modified":"2010-01-18T17:39:27","modified_gmt":"2010-01-18T19:39:27","slug":"discovering-rails-3-generators","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2010\/01\/discovering-rails-3-generators\/","title":{"rendered":"Discovering Rails 3 generators"},"content":{"rendered":"

This weekend during Rails Bugmash<\/a> I stumbled across some nice posts about Rails 3 generators which motivated me to share them and add some comments!<\/p>\n

First, David Trasbo wrote a nice guide about how to make your first Rails 3 generator<\/a>, it covers up all the basic steps including setting it up in a gem. He also puts the deserved attention into Thor::Actions<\/code><\/a>, which contains most helpers you need in a generator, like copy_file<\/code>, template<\/code>, create_file<\/code>, empty_directory<\/code> and so on.<\/p>\n

On another post, Ben Scofield talks about apply<\/code> method, which is also in Thor::Actions<\/code><\/a>, and how to use it to dry your application templates<\/a>.<\/p>\n

Wait… so Thor::Actions<\/code><\/a> is used both in generators and in Rails application templates? Exactly, Rails’ new generators unifies both application templates and generators API into one<\/strong>. While Thor::Actions<\/code><\/a> holds basic methods, all Rails specific methods like environment<\/code>, rakefile<\/code>, generator<\/code> are in Rails::Generators::Actions<\/a>. If you already wrote an application template, you will feel more at home when writing a Rails 3 generator.<\/p>\n

Paul Barry talks how easy it’s to customize your scaffold<\/a> 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<\/strong> 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<\/code> 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.<\/p>\n

Finally, Zigzag Chen wrote about templates customization<\/a> 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<\/strong>.<\/p>\n

Rails Bugmash<\/a> 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<\/a> and besides the posts linked above, there is a generator guide<\/a>, which may help you get started and maybe write your own post as well. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"

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. … \u00bb<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[67,10,23,7,60,9],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/654"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=654"}],"version-history":[{"count":10,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/654\/revisions"}],"predecessor-version":[{"id":668,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/654\/revisions\/668"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}