Rake

Rake is something I have been using since day one with Rails, but that I really haven’t gone any deeper with than the basic rails rake tasks. Today I had to write a rake tasks that ran some checks and updated records.

Here’s what I learned:

  1. This article by Jason Seifer was a great introduction to rake. The big idea is that rake has namespaces and inside those namespaces you define tasks.

     namespace :feed do
       desc "Feed dog" 
       task :dog => :environment do
         puts "fed the dog"
       end
    
       desc "Feed cat"
       task :cat => :environment do 
         puts "fed the cat"
       end
    
       desc "Feed dog and cat"
       task :all => [:dog, :cat]
     end
    
  2. Rake tasks should be tested. Thoughtbot provides a really nice shared context that makes testing very easy.

Comments