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.
Tags: gems, generators, plugins, rails, ruby, thor
Posted in English | 8 Comments »
Pratik lately is doing a great work refactoring ActiveRecord to make a full use of relations. Speaking in code language, this means that in Rails 3 you will be able to do:
User.where(:name => "Jose").order("birthday")
And it will return a relation. The relation can be further manipulated and the query is only triggered when you call all, first, to_a or some Enumerator method.
Besides that, he’s also doing some crazy experiments, which will probably become a plugin later. While discussing with Pratik some ways to implement equality and inequality, I discovered a neat ruby trick. Open up an irb session and do:
~2 #=> -3 ~42 #=> -43
And this method is actually documented.
The nice thing though, is that you can define this method in other classes as well. In the querying scenario for example, we could add this behavior:
class Symbol def ~ :"LOWER(#{self})" end end
And now we could actually do:
User.where(~:email => "jose@plataformatec.com.br") #=> SELECT * WHERE LOWER(email) = "jose@plataformatec.com.br"
Unary operators
Yehuda Katz later pointed to me that this can actually be done with any of the three unary operators in Ruby: +, – and ~.
So we could also do:
class String def +@ upcase end def -@ downcase end end
And now:
+"jose" #=> "JOSE" -"JOSE" #=> "jose"
You could even “go crazy” and append several unary operators:
+-+"jose" #=> "JOSE"
And you? Did you know such behavior in Ruby? Do you have any good use case for it?
Tags: operators, ruby, unary
Posted in English | 5 Comments »

All
English only
Em português apenas