Flowdock

Recent notes

RSS feed
July 25, 2008 - (v1.0.0 - v2.1.0)
4 thanks

haml, an alternative to ERb

Want something nicer looking (and currently, faster!) than using ERb for your views? Have a look at haml (and it’s companion, sass, for stylesheets). It will make you feel all fuzzy on the inside, I promise :P.

ERb example

<div id="profile">
  <div class="left column">
    <div id="date"><%= print_date %></div>
    <div id="address"><%= current_user.address %></div>
  </div>
</div>

haml equivalent

#profile
  .left.column
    #date= print_date
    #address= current_user.address

Shifting to haml from ERb feels strange at first, but after about 20 minutes it starts to feel nice. A little longer and you’ll really start to notice your productivity (and of course, happiness) increase! :). I’ve starting shifting all new projects developed at our work office over to using haml (and sass), it’s been fantastic!

At first I came across a few things that I couldn’t do in haml, though every time a quick read of the overview doc page would show me a simple syntax for overcoming that issue! :) (which out of interest, is located here: http://haml.hamptoncatlin.com/docs/rdoc/classes/Haml.html)

Give the tutorial a shot if you’re interested: http://haml.hamptoncatlin.com/tutorial

July 25, 2008 - (v1.0.0 - v2.1.0)
0 thanks

render template file different from your action (method) name - alternative

Alternative ways to render templates for actions

# Renders the template for foo (foo.html.erb, foo.haml.erb, foo.text.html.haml, whatever :P)
def foo
end

# Renders the template that would be rendered in foo
# (but without the foo controller action being invoked)
def bar
  render :action => 'foo'
end

# Similar to what bar does, but render's a specifically named template
def roar
  render :template => 'foo'
end

# Similar to what roar does, but render's a template
# from outside of the current controller's views directory
def boo
  render :template => 'global/something'
end
July 25, 2008
2 thanks

If you need something more

Use the brilliant chronic gem: http://chronic.rubyforge.org/

require 'chronic'
Time.now # => Fri Jul 25 00:00:25 0200 2008
Chronic.parse 'tomorrow 8 in the evening'  # => Sat Jul 26 20:00:00 0200 2008
Chronic.parse 'next Monday noon'           # => Mon Jul 28 12:00:00 0200 2008
Chronic.parse 'first Wednesday of Aug'     # => Wed Aug 06 12:00:00 0200 2008
Chronic.parse 'first Wednesday of Aug 7pm' # => Wed Aug 06 19:00:00 0200 2008
July 24, 2008
3 thanks

render_collection

You can wrap render in helpers. For example, render_collection. In app/helpers/application.rb:

module ApplicationHelper
  def render_collection(name, collection)
    render :partial => "shared/#{name}", :collection => collection
  end
end

In views:

<h2>Comments</h2>
<%= render_collection :comments, @photo.comments %>
July 24, 2008
8 thanks

render template file different from your action (method) name

In some cases you have to avoid rails magic that uses template names named as your ActionMailer method.

rails magic

def daily_notification
  # ...
end
# will look for daily_notification.erb

def weekly_notification
  # ...
end
# will look for weekly_notification.erb

your case

Just give necessary value to @template instance variable.

def setup
  # ...
  @template = 'notification'
end

def daily_notification
  # ...
end
# will look for notification.erb

def weekly_notification
  # ...
end
# will look for notification.erb
July 24, 2008 - (v1.0.0 - v2.1.0)
0 thanks

Extract plain text body from TMail parsed email

Here’s a monkey patch for TMail::Mail I wrote to recurse through a message and extract all plain text body components of that message, returning an Array. For most use cases, the resulting Array will contain one String element.

Currently I put this code in a file called lib/tmail_extensions.rb and require ‘tmail_extensions’ in environment.rb

module TMail
  class Mail
    def plain_text_body
      gather_plain_text_parts(self).flatten
    end

  private
    def gather_plain_text_parts(part)    
      returning [] do |message|
        message << part.body.strip if part.content_type == 'text/plain'        
        part.parts.each { |p| message << gather_plain_text_parts(p) }
      end
    end
  end
end
July 24, 2008 - (v2.1.0)
20 thanks

automatically generate scopes for model states

or better known as “throw on some more tasty meta-programming” :). Given an example of a model which has a state (String) which must from a set of defined values, e.g. pending, approved, denied.

class User < ActiveRecord::Base
  STATES = [ 'pending', 'approved', 'denied' ]

  validates_inclusion_of :state, :in => STATES

  # Define a named scope for each state in STATES
  STATES.each { |s| named_scope s, :conditions => { :state => s } }
end

This automatically defines a named_scope for each of the model states without having to define a named_scope manually for each state (nice and DRY).

July 24, 2008 - (v1.0.0 - v2.1.0)
1 thank

Custom environment constants

Custom environment level constants can be passed in to your rails application (server, console, whatever) like this):

# bash, tcsh, whatever shell
GAMEMODE=pregame script/server

Within rails this constant can be accessed by

ENV['GAMEMODE']
=> "pregame"

ENV['DOES-NOT-EXIST']
=> nil
July 23, 2008
3 thanks

options_for_select further example (using a collection and with a default value)

In this example, we are editing a collection of region records, each with its own select list of countries. (Region belongs_to :country.) If the region doesn’t have a country associated, then we want a default message of “unassigned”. Of course, if the region does have a country associated then we want that country displayed:

<% name = "region[" + region.id.to_s + "][country_id]" %>
<% id = "region_" + region.id.to_s %>

<%= select_tag(id, options_for_select([["unassigned" , "0" ]] +
                     Country.to_dropdown, region.country_id),

{:name => name} ) %> This give us:

<select id="region_3" name="region[3][country_id]">
  <option value="0">unassigned</option>
  <option selected="selected" value="12">England</option>
</select>

NB: we’re using the handy acts_as_dropdown plugin (http://delynnberry.com/projects/acts-as-dropdown/) but we could just as easily prepare the select list with map / collect as above.

July 23, 2008 - (v1.2.0 - v2.1.0)
10 thanks

:only, :except and passing in multiple parameters

To specify that the filter should be applied to or excluded from given controller actions, use the :only and :except parameters. To pass in multiple controller actions use an array:

before_filter :authorize, :except => [:index, :show]
before_filter :authorize, :only => :delete
July 23, 2008
1 thank

Using .map(&:item)

You can only use .map(&:item) with find(:all, not find(:first. For example; the first works, but the second does not.

@person = Person.find(:all, :conditions => {
  :id => @person.id}, :select => "name").map(&:name)

@person = Person.find(:first, :conditions => {
  :id => @person.id}, :select => "name").map(&:name)
July 23, 2008
4 thanks

Keep your controllers clear

When you use redirect_to or render with flash[:notice] or flash[:error], you can define some helper methods in your ApplicationController (or somewhere you want):

class ApplicationController < ActionController::Base

  protected

    %w(notice error).each do |message|
      class_eval <<-END_EVAL
        def redirect_#{message}(url, message)
          flash[:#{message}] = message
          redirect_to url
        end

        def render_#{message}(action, message)
          flash[:#{message}] = message
          render :action => action
        end
      END_EVAL
    end
end

Now you have four methods - redirect_notice, redirect_error, render_notice and render_error.

July 23, 2008
5 thanks

Custom annotation types

For group work you may need something more than FIXME, OPTIMIZE and TODO. Just create new rake file and place it to lib/tasks:

require 'source_annotation_extractor'

task :notes do
  SourceAnnotationExtractor.enumerate "WTF|OMG", :tag => true
end

namespace :notes do
  desc "Enumerate all WTF annotations"
  task :wtf do
    SourceAnnotationExtractor.enumerate "WTF"
  end

  desc "Enumerate all OMG annotations"
  task :omg do
    SourceAnnotationExtractor.enumerate "OMG"
  end
end

or create an array of new types and generate tasks dynamicaly.

July 23, 2008
11 thanks

Pass id collections with check box tags

It can be useful to pass a collection of ids to a controller, especially in the case of a has_many relationship, for example:

User has_many Roles

In your view you could have something like:

<ul>
  <% @roles.each do |role| %>
      <li>
        <%= check_box_tag 'role_ids[]', role.id -%>
        <%= h role.name -%>
      </li>
  <% end %>
</ul>

Note the square brackets after role_ids - this is important for passing a collection through to the controller.

If you place this in a form and submit it, you can expect to see a param passed into the controller that looks like:

"role_ids"=>["1", "2", "3"]
July 23, 2008 - (v1.0.0 - v2.1.0)
2 thanks

subdomains from request

Useful for discovering what domain/subdomain(s) the current request came from (Rails application may operate differently depending on which subdomain is passed in, this is a great way to segment functionality using the route).

request.host.split('.')

Doesn’t get much simpler than that :).

Likewise if you want to see only the subdomain component(s). Given a domain, example.com

request.host.gsub('example.com', '').split('.')
July 23, 2008 - (v2.0.0 - v2.1.0)
9 thanks

Easy and effective admin authentication

Great for use within an AdminController (in which all other administrative controllers inherit from AdminController).

class AdminController < ApplicationController
  before_filter :authenticate

  def authenticate
    authenticate_or_request_with_http_basic('Administration') do |username, password|
      username == 'admin' && password == 'password'
    end
  end
end
July 23, 2008 - (v2.0.0 - v2.1.0)
9 thanks

perform update_all scoped within a has_many collection

For example: having two models, User and Message (user has_many messages, each message has a boolean flag called ‘read’). You want to mark all messages as read for a particular user.

Mark all messages as read for a particular user

Message.update_all({:read => true}, {:id => user.messages})
July 23, 2008
6 thanks

Loading fixtures in migrations

This helper is wrapper around Fixtures#create_fixtures and just load fixtures from specified directory (db/migrate/data by default):

class ActiveRecord::Migration
  def self.load_data(filename, dir = 'db/migrate/data')
    Fixtures.create_fixtures(File.join(RAILS_ROOT, dir), filename)
  end
end

It is usefull for tables with data like country list:

class CreateCountries < ActiveRecord::Migration
  def self.up
    create_table :countries do |t|
      t.string :name, :code, :null => false
      t.timestamps
    end
    load_data :countries
  end

  def self.down
    drop_table :countries
  end
end
July 22, 2008
1 thank

Full Select

The full select using this would be something like:

Code Example

<%= select_tag(“job[state_id]”, options_from_collection_for_select(State.find(:all), “id”, “name”)) %>

July 22, 2008
4 thanks

Example

In your migration:

def self.up
  add_column :accounts, :is_admin, :boolean, :default => 0
end
July 22, 2008
1 thank

update_attribute!

I don’t know why, but method update_attribute! is missing. May be it would be useful for somebody:

class ActiveRecord::Base
  def update_attribute!(name, value)
    send(name.to_s + '=', value)
    save!
  end
end
July 22, 2008
4 thanks

:conditions examples

:conditions => {:login => login, :password => password}

:conditions => [‘subject LIKE :foo OR body LIKE :foo’, {:foo => ‘woah’}]

(from the book “The Rails Way”)

July 22, 2008 - (>= v2.1.0)
11 thanks

Migration helpers

You can add your own migration helpers as references:

Code example

class ActiveRecord::ConnectionsAdapters::TableDefinition
  def counter_caches(*args)
    args.each { |col| column("#{col}_count", :integer, :default => 0) }
  end
end

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :first_name, :last_name, :email
      t.counter_caches :photos, :messages
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end
July 22, 2008
18 thanks

select_tag with options_for_select example

An example of using options_for_select with select_tag

select_tag 'user_id', options_for_select(@users.collect{ |u| [u.name, u.id] })

This would generate something like:

<select id="user_id" name="user_id">
  <option value="1">Brad</option>
  <option value="2">Angie</option>
  <option value="3">Jenny</option>
</select>
July 22, 2008
18 thanks

Nested resources in form_for

If you like doing things RESTfully and have a model relationship like:

Post_ has many Comments_

Then you can construct a form_for within your view to mirror this relationship when creating comments:

form_for [@post, @comment] do |f|
  ...
end

You also need to make sure your routes reflect this relationship:

map.resources :post, :has_many => [:comments]
July 22, 2008
6 thanks

selected

select :languages, :language, [‘en’, ‘de’, ‘fr’], {:selected=> ‘en’}

July 22, 2008
15 thanks

Demo: select onchange invoke an ajax

select(“order”, “customer_id”, o.customer.collect {|c| [ c.label, c.id ] },

{:include_blank => true, :selected => o.customer_id }, 
:onchange => remote_function(:update => "message_id",
:method => "put",
:with => "'item=' + value",  
:url => { :controller => :orders, :action => :set_customer_id, :id => order.id}))
July 22, 2008
6 thanks

Documentation

Allows for reverse merging where its the keys in the calling hash that wins over those in the other_hash. This is particularly useful for initializing an incoming option hash with default values:

def setup(options = {})
  options.reverse_merge! :size => 25, :velocity => 10
end

The default :size and :velocity is only set if the options passed in doesn‘t already have those keys set.