Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Rails label helper for forms

When creating forms I get tired of creating labels for each field so I like to shorten my typing by using this application helper.

def l(id,label)
    "<label for='#{id}'>#{label}</label>"
end


then later on in your view you type
<%= l('my_field_id','My Label') %>

Javascript open new window with all params

function pop_window()
{
window.open ("http://url.com","Window Title","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1, scrollbars=0,height=0,width=0");
}


Rails link to helper
<%= link_to "link text", {:action => "help"}, :popup => ['Terms and Conditions','width=400,height=400'] %>

YM4R configuration with multiple domains

Need to put domains in config/gmaps_api_key.yml
production:
 host.com: longapikeythatmakesnosense
 anotherhost.com: jsdkfljskfljsgklsjgkl


When printing out the header information you need to pass the param :host. request.host should do the job.
<%= GMap.header(:host => request.host) %>

Rails button helper

I use this for the admin section of my sites. It's good for your basic buttons. I use the tango icon library as well (http://tango.freedesktop.org/Tango_Icon_Gallery).

  def button(options = {})
    image_source = case options[:type]
      when 'edit':          "edit-find-replace.png"
      when 'preview':       "document-print-preview.png"
      when 'delete':        "edit-delete.png"
      else "list-add.png"
    end
    
    button = "<img src='/images/icons/#{image_source}' />"
    
    if options[:link]
      target = params[:target].blank? ? ">" : " target='#{options[:target]}'>"
      "<a href='#{options[:link]}'" + target + button + "</a>"
    else
      button
    end
  end


Example usage:
<%= button %>
# <img src='/images/icons/list-add.png' />


<%= button :type => "edit", :link => "/edit/5" %>

Mongrel cluster starting after restart on Ubuntu GG

sudo mkdir /etc/mongrel_cluster
sudo ln -s /mnt/app/ticketsolve/shared/config/mongrel_cluster_new_production.yml /etc/mongrel_cluster/mongrel_cluster_new_production.yml
sudo cp /usr/bin/mongrel_cluster_ctl /etc/init.d/
sudo chmod +x /etc/init.d/mongrel_cluster_ctl
cd /etc/init.d/
sudo /usr/sbin/update-rc.d -f mongrel_cluster_ctl defaults

Rails CSV Export



# require 'rubygems' if using this outside of Rails
require 'fastercsv'

def dump_csv
  @users = User.find(:all, :order => "lastname ASC")
  @outfile = "members_" + Time.now.strftime("%m-%d-%Y") + ".csv"
  
  csv_data = FasterCSV.generate do |csv|
    csv << [
    "Last Name",
    "First Name",
    "Username",
    "Email",
    "Company",
    "Phone",
    "Fax",
    "Address",
    "City",
    "State",
    "Zip Code"
    ]
    @users.each do |user|
      csv << [
      user.lastname,
      user.firstname,
      user.username,
      user.email,
      user.company,
      user.phone,
      user.fax,
      user.address + " " + user.cb_addresstwo,
      user.city,
      user.state,
      user.zip
      ]
    end
  end

  send_data csv_data,
    :type => 'text/csv; charset=iso-8859-1; header=present',
    :disposition => "attachment; filename=#{@outfile}"

  flash[:notice] = "Export complete!"
end

ActiveRecord reconnect to database

// my Merb consoles drop the db connection after the mysql timeout; I'd rather not reload the whole environment

ActiveRecord::Base.connection.reconnect!

doctype for standard layout

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  <head> 
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> 
  <title>Events: <%= controller.action_name %></title> 
  <%= stylesheet_link_tag 'style' %> 
</head> 
<body> 

  <p style="color: green"><%= flash[:notice] %></p> 
  <%= yield %> 
</body> 
</html> 

find conditions via hash

Instead of simply adding a list of things at the end of the array, you can also pass in a hash and change the question marks to actual named replacements. This can help you keep the order of your arguments straight.

Event.find(
  :all,
  :conditions => [ "title like :search or description like :search",
                   {:search => "%Tiki%"}]
)

Subversion Configuration Script for Your Rails Application

// Configure SVN for your rails app.

#!/bin/sh
svn remove log/*
svn commit -m"removing log files" 
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m 'Ignoring all files in /log/ ending in .log'
svn move config/database.yml config/database.example
svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'
svn propset svn:ignore "database.yml" config/
svn update config/
svn commit -m 'Ignoring database.yml'
svn remove tmp/*
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m "ignore tmp/ content from now" 
svn propset svn:ignore ".htaccess" config/
svn update config/
svn commit -m 'Ignoring .htaccess'
svn propset svn:ignore "dispatch.fcgi" config/
svn update config/
svn commit -m 'Ignoring dispatch.fcgi'