Archive for the ‘Rake’ Category

Configuring gems for your rails app

Friday, May 8th, 2009

Until I found this (recently), I have been guilty of not configuring my gems for my rails apps.  Apparently these features have been around for a while and can be really helpful when deploying or doing team development with rails and gems.

What it does

  • Requires gems automatically
  • Documents the gems you used
  • Allows you to specify gems by environment.   This is key so you don’t have to require testing gems for a production environment.
  • Provides rake tasks for freezing,  installing and checking the status of the required gems.
  • helps to ensure that functionality you depend on gems for will be working.

Rake Tasks

Here is a listing of gem related rake tasks.

 rake gems                             # List the gems that this rails application depends on
 rake gems:build                       # Build any native extensions for unpacked gems
 rake gems:install                     # Installs all required gems.
 rake gems:refresh_specs               # Regenerate gem specifications in correct format.
 rake gems:unpack                      # Unpacks all required gems into vendor/gems.
 rake gems:unpack:dependencies         # Unpacks all required gems and their dependencies into vendor/gems.
 rake rails:freeze:gems                # Lock this application to the current gems (by unpacking them into vendor/rails)
 rake rails:unfreeze                   # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
  • rake gems:unpack variants are helpful to make sure that you deploy the same gems from your development environment to your production environment, even if it’s on a shared host.
  • rake rails:freeze:gems does the same for the rails gems.
  • A simple rake gems will give you an overview of your gems like this:
rubyyot@atlas:$ rake gems
(in /home/rubyyot/working/examples)
 - [F] ruby-openid
 - [F] paperclip
 - [I] RedCloth
 - [F] sqlite3-ruby
 - [F] ruby-openid >= 2.0.4
I = Installed
F = Frozen
R = Framework (loaded before rails starts)

Example

Here are my configured gems from a project I’m currently working on.

config/enviroment.rb
  config.gem "ruby-openid", :lib => 'openid'
  config.gem "paperclip"
  config.gem "RedCloth", :lib => 'redcloth'
config/environments/development.rb
  config.gem "sqlite3-ruby", :lib => "sqlite3"
config/environments/test.rb
 config.gem "notahat-machinist", :lib > 'machinist', :version => '0.3.1'
 config.gem "faker", :version => '0.3.1'
 config.gem "sqlite3-ruby", :lib => "sqlite3"
config/environments/production.rb
config.gem "mysql", :lib => "mysql"

More

If you are interested, there is a Railscast episode on this topic.

GTD on Rails with Annotations

Thursday, May 7th, 2009

You are in the zone.

Beautiful code is flowing from your fingertips and forming on screen, resolving into a true work of art.

Your tests cover just the right amount of your code, and the few tests that don’t pass are failures rather than errors; regardless they are corrected within seconds.

The design has taken on a life of it’s own, and it is spreading tendrils like wisps of smoke outwards in all directions.

It’s clear that this project will be ready for it’s first release ahead of schedule, when it happens.

Your thoughts diverge in multiple directions.  You cannot decide which task to persue.  You choose to complete the code that will make your current failing test pass.  But, those other things were pretty important too.  You freeze.  Concentration is lost.  It all falls apart.

Recently I found some rails rake tasks that will help in this situation or anywhere else that you want to place a marker to come back to later; perfect for code reviews as well.

rake notes                                # Enumerate all annotations                                                                                       
rake notes:custom                         # Enumerate a custom annotation, specify with ANNOTATION=WTFHAX                                                   
rake notes:fixme                          # Enumerate all FIXME annotations                                                                                 
rake notes:optimize                       # Enumerate all OPTIMIZE annotations                                                                              
rake notes:todo                           # Enumerate all TODO annotations

simply mark the code you are intersted in remembering such as the following

class ExampleController
  def new
    @design = Design.new
    #TODO wookie
  end

  def create
    #OPTIMIZE this needs help
    #FALAFEL yummy in sandwiches.
    @design = Design.new(params[:design])
    @design.profile = Identity.find(current_identity).profile

    #FIXME This was supposed to support xml!
    if @design.save
      flash[:message] = "Design added."
      redirect_to design_url(@design)
    else
      flash[:error] = "Error saving Design."
      render :action => :new
    end
  end
end

when you run rake notes you get

rubyyot@atlas:$ rake notes
(in /home/rubyyot/working/example)
app/controllers/example_controller.rb:
  * [ 10] [TODO] wookie
  * [ 14] [OPTIMIZE] this needs help
  * [ 19] [FIXME] This was supposed to support xml!

Notice you get the exact location of the note along with the type and the text of the note.

Alternatively you could use custom tags with this like this.

rubyyot@atlas:$ rake notes:custom ANNOTATION=FALAFEL
(in /home/rubyyot/working/example)
app/controllers/example_controller.rb:
  * [ 15] yummy in sandwiches.