giant robots smashing into other giant robots

Written by thoughtbot

dancroak

Limerick Rake

Limerick Rake is a collection of useful rake tasks for Rails apps. Some we wrote ourselves, many we’ve collected from others in the community. We’re always on the lookout for helpful rake tasks so if you have anything, please post them in the comments. To use in a Rails app:

script/plugin install git://github.com/thoughtbot/limerick_rake.git

Database

Read tasks/database.rake for details for configuration. Load initial database fixtures (in db/bootstrap/*.yml) into the current environment’s database. Load specific fixtures using FIXTURES=x,y:

rake db:bootstrap:load

Prints a list of unindexed foreign keys so you can index them:

rake db:indexes:missing

Run model validations on all model records in database:

rake db:validate_models

Git

Merge a branch into the origin/staging branch:

rake git:push:staging

Merge the staging branch into origin/production for launch:

rake git:push:production

Show the difference between current branch and origin/staging:

rake git:diff:staging

Show the difference between origin/staging and origin/production:

rake git:diff:production

Pull updates from suspenders, the thoughtbot rails template:

rake git:pull:suspenders

Branch origin/production into BRANCH locally:

rake git:branch:production

Backup

Backup the current database. Timestamped file is created as :rails_root/../db-name-timestamp.sql:

rake backup:db

Backup all assets under public/system. File is created as :rails_root/../system.tgz:

rake backup:assets

Haml & Sass

Convert all CSS files in public/stylesheets to Sass:

rake sass:all_css2sass

Convert all Sass files to CSS:

rake sass:all_sass2css

Convert all HTML files to Haml:

rake haml:all_html2haml

“Limerick Rake” - the name?

The Limerick Rake is a traditional Irish song.

I am a young fellow that’s easy and bold,
In Castletown conners I’m very well known.
In Newcastle West I spent many a note,
With Kitty and Judy and Mary.

My father rebuked me for being such a rake,
And spending me time in such frolicsome ways,
But I ne’er could forget the good nature of Jane,
Agus fágaimíd siúd mar atá sé.

My parents had reared me to shake and to mow,
To plough and to harrow, to reap and to sow.
But my heart being airy to drop it so low,
I set out on high speculation.
On paper and parchment they taught me to write,
In Euclid and grammar they opened my eyes,
And in multiplication in truth I was bright,
Agus fágaimíd siúd mar atá sé.

If I chance for to go to the town of Rathkeale, The girls all round me do flock on the square.
Some give me a bottle and others sweet cakes,
To treat me unknown to their parents.

There is one from Askeaton and one from the Pike,
Another from Arda, my heart was beguiled,
Tho’ being from the mountains her stockings are white,
Agus fágaimíd siúd mar atá sé.

To quarrel for riches I ne’er was inclined,
For the greatest of misers that must leave all behind.
I’ll purchase a cow that will never run dry,
And I’ll milk her by twisting her horn.

John Damer of Shronel had plenty of gold,
And Lord Devonshire’s treasures are twenty times more,
But he’s laid on his back among nettles and stones,
Agus fágaimíd siúd mar atá sé.

The old cow could be milked without clover or grass,
She’d be pampered with corn, good barley and hops.
She’s warm and stout, and she’s free in the paps,
And she’ll milk without spancil or halter.

The man that will drink it will cock his caubeen,
And if anyone laughs there’d be wigs on the green,
And the feeble old hag will get supple and free,
Agus fágaimíd siúd mar atá sé.

There’s some say I’m foolish and more say I’m wise,
But being fond of the women I think is no crime,
For the son of King David had ten hundred wives,
And his wisdom was highly regarded.

I’ll take a good garden and live at my ease,
And each woman and child can partake of the same,
If there’d be war in the cabin, themselves they’d be to blame,
Agus fágaimíd siúd mar atá sé.

And now for the future I mean to be wise,
And I’ll send for the women that acted so kind,
I’d marry them all on the morrow by and by,
If the clergy agree to the bargain.

And when I’d be old and my soul is at peace,
These women will crowd for to cry at my wake,
And their sons and their daughters will offer their prayer,
To the Lord for the soul of their father.

dancroak

Fashion Runway: ERb vs. Haml

With Rails 3 around the corner, choosing a templating framework (and testing frameworks and ORMs) will be a decision Rails developers can make on even ground. ERb and Haml will be equally supported in places such as generators.

So how do we choose?

Looks

A lot of people think Haml is hot.

It’s true. Haml looks very clean in the examples. I think the template code in Boston.rb is quite beautiful:

#events
  .ribbon
    .row
      .crud
        %p= link_to 'New event', new_event_path
      %h2 Recurring Events
      - @recurring_events.each do |event|
        .quarter>
          %p.date
            %span= event.date.to_s(:fancy_date)
            %span= event.date.to_s(:time)
          %h3= link_to h(event.title), event
          %p= event.cached_description_html

Very readable. All those noisy <% end %>s and closing HTML tags are gone.

ERb is beautiful, too

ERb, I imagine you must take all this Haml attention personally. Don’t feel down. Your <body> is a wonderland.

For example, when I want to go vertical when I want to, such as in this Formtastic example from one of our apps:

<%= form.input :logo,
      :input_html => { :class => "upload_field",
                       :style => "width: 340px;" },
      :label      => 'Logo',
      :size       => '30' %>

Or this example of a typical Clearance sign up form:

<h2>Sign In</h2>

<div id="signin">
  <% semantic_form_for :session, :url => session_path do |form| %>
    <% form.inputs do %>
      <%= form.input :email %>
      <%= form.input :password,    :as => :password %>
      <%= form.input :remember_me, :as => :boolean, :required => false %>
    <% end %>
    <% form.buttons do %>
      <%= form.commit_button "Sign In" %>
    <% end %>
  <% end %>
</div>

This style is in line with a few of thoughtbot’s internal coding standards:

  • indent two spaces regardless of HTML or ERb tags
  • align hash keys and hash rockets when going vertical

The first standard is exactly the same as Haml but the second is intentionally awkward in Haml using the pipe character. Hamlistas will say: move it into a helper.

Good point. Then you can go vertical in normal Ruby code.

Is beauty only skin deep?

The argument for either framework on looks alone is unfortunately steeped in personal preference. Let’s go deeper.

One fewer thing to learn

I worked on a Rails project with a PHP developer. I wanted her to learn Ruby, testing with Shoulda and Factory Girl, git, REST… Haml was a lower priority. ERb and immediately felt familiar to her.

Lesson: ERb’s interpolated tags are familiar for ASP, PHP, and JSP developers.

Yet the same “one fewer thing to learn” argument can be made for Haml. Look at that Boston.rb example again. Now see how our JQuery code deals with .crud inside #events:

jQuery(document).ready(function($) {
  $('#events, #jobs, #projects, #presentations, #companies, #apps, #show, #edit').hover(
    function() {
      $(this).find('.crud').show();
    },
    function() {
      $(this).find('.crud').hide();
    }
  );
});

Lesson: Haml’s re-use of CSS selectors are familiar for CSS and Javascript developers.

Active development

Haml is under active development. For example, the Haml team recently responded to newer competitor, Less by altering Sass to become a superset of CSS. New ideas and features are being shared and improved upon.

ERb isn’t capturing the imaginations of the open source community right now, but is also older and stable. It is working software that you can wrap around yourself like a security blanket.

The only thing that matters: institutional standardization

thoughtbot standardized on ERb. Hoptoad was on Haml for a long time but we converted it to Erb.

Hashrocket announced they standardized on Haml.

Both are examples of companies that work on a lot of apps. I believe the gains from institutional standardization, such as code re-use and developers mastering a common set of tools, outweigh any other pro or con and likely leads to cost savings.