Parse a WordPress RSS XML Feed with Hpricot

One cool thing about wordpress is that you can get a feed of a specific category by going to /category/category-name/feed or a specific tag by going to /tag/tag-name/feed

require 'open-uri'
require 'hpricot'

doc = Hpricot.parse(open("http://myawesomeblog.com/category/welcome/feed"))
(doc/:item).each do |xml_product|
  puts xml_product.search("/title").first.children.first.raw_string
  puts xml_product.search("/pubDate").first.children.first.raw_string
end
Group ActiveRecord operations in a transaction

Stolen from: 10 Awesome Ruby on Rails Techniques to Get You Started

Group operations in a transaction: ActiveRecord wraps the creation or update of a record in a single transaction. Multiple inserts will then generate many transactions (one for each insert). Grouping multiple inserts in one single transaction will speed things up.

Insead of:

diners_in_nj.each do |d|
  Diner.create({:disco_fries => d.disco_fries})
end

Use:

Diner.transaction do
  diners_in_nj.each do |d|
    Diner.create({:disco_fries => d.disco_fries})
  end
end

or for rolling back the whole transaction if any insert fails, use:

Diner.transaction do
  diners_in_nj.each do |d|
    diner = Diner.new({:disco_fries => d.disco_fries})
    diner.save!
  end
end
Continuous Testing With Unobtrusive System Notification using Watchr and Growl

I tried to set up ZenTest and autotest-rails today, but I kept on getting these annoying errors:

/usr/local/lib/ruby19/1.9.1/pathname.rb:270: warning: `*' interpreted as argument prefix
/usr/local/lib/ruby19/gems/1.9.1/gems/ZenTest-4.2.1/lib/unit_diff.rb:100: warning: shadowing outer local variable - line
/usr/local/lib/ruby19/gems/1.9.1/gems/ZenTest-4.2.1/lib/unit_diff.rb:104: warning: shadowing outer local variable - line

So I gave up and went with watchr:

Step 1. Run ‘sudo gem install watchr’ in your terminal

Step 2. Download test.watchr.rb file to the root of your application

Step 3. Download Growl

Step 4. Mount the growl .dmg and copy the file Extras/growlnotify/growlnotify to somewhere in your system path (like /usr/bin or /usr/local/bin)

Step 5. Run ‘watchr test.watchr.rb’ in the root of your application.

If you want custom icons, you have to change the image paths on line 6 of test.watchr.rb

Refactoring your config/database.yml file

A less annoying database.yml file when you use the same database for all three environments

defaults: &defaults
    adapter: mysql
    encoding: utf8
    reconnect: false
    pool: 5
    username: admin
    password: welcome1

development:
    database: friendster_dev
    <<: *defaults

test:
    database: friendster_test
    <<: *defaults

production:
    database: friendster
    <<: *defaults

How come its always so hard to find documentation for the database.yml file?

Fixtures for Ruby on Rails Testing

/test/fixtures/users.yml

<%
  def auto_increment
    @id ||= 0; @id += 1
  end
%>
tommy:
  id: <%= auto_increment %>
  email: tommy@thedocks.com
  created_at: <%= 6.months.ago.to_s(:db) %>
gina:
  id: <%= auto_increment %>
  email: gina@jerseydiner.com
  created_at: <%= 1.minute.ago.to_s(:db) %>

/test/unit/user_test.rb

fixtures :users

def test_can_access_tommy_fixture
  assert(users(:tommy) != nil)
end

Import your fixture records into the current environment: rake db:fixtures:load

Default Ruby on Rails Scaffold Code

Sometimes when things get crazy, you need to reference the default stuff.

class ArticlesController < ApplicationController
  # GET /articles
  # GET /articles.xml
  def index
    @articles = Article.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @articles }
    end
  end

  # GET /articles/1
  # GET /articles/1.xml
  def show
    @article = Article.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @article }
      format.json { render :json => @article }
    end
  end

  # GET /articles/new
  # GET /articles/new.xml
  def new
    @article = Article.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @article }
    end
  end

  # GET /articles/1/edit
  def edit
    @article = Article.find(params[:id])
  end

  # POST /articles
  # POST /articles.xml
  def create
    @article = Article.new(params[:article])

    respond_to do |format|
      if @article.save
        flash[:notice] = 'Article was successfully created.'
        format.html { redirect_to(@article) }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /articles/1
  # PUT /articles/1.xml
  def update
    @article = Article.find(params[:id])

    respond_to do |format|
      if @article.update_attributes(params[:article])
        flash[:notice] = 'Article was successfully updated.'
        format.html { redirect_to(@article) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.xml
  def destroy
    @article = Article.find(params[:id])
    @article.destroy

    respond_to do |format|
      format.html { redirect_to(articles_url) }
      format.xml  { head :ok }
    end
  end
end