Thursday, June 26, 2008

Git: working with branches

The way we work with git is that for every remote pair programming session we create a separate branch. We give it a name after the feature we're working on.

git checkout -b feature1

It automatically switches to this branch.

During our work we tend to checkin our changes quite often. We do it with:

git checkin -a -m "created some specs for class A"

After we finish our session, we do two things.
First, we merge our branch to master:

git checkout master
git merge feature1

Then, we delete the branch we were working on:

git branch -D feature1

That's it.

If you happen to delete the branch BEFORE you merge it, don't panic, there is a solution.
In order to undelete a branch just after you deleted it, do:

git checkout -b new_branch_name HEAD@{1}

Tuesday, June 10, 2008

Andrzej's Rails tips #11

Today I'm going to show you two tips, both related to Rails controllers.

Use the current_user object whenever you access its data

Instead of

@order = Order.find(params[:order_id])

do this:

@order = current_user.orders.find(params[:order_id])

Thanks to that, you don't have to check whether this order belongs to the user, you just need to handle ActiveRecord::RecordNotFound exception.

Move all the logic from your controller to the model

I know you already read this statement many times, but I will repeat it anyway.
In your actions you shouldn't manipulate your model objects, do it in the model class itself. Here's a simple example:


class OrdersController < ApplicationController
def update
@order = current_user.orders.find(params[:order_id])
if params[:order][:amount] > 0


class OrdersController < ApplicationController
def update
@order = current_user.orders.find(params[:order_id])[:order][:amount])

class Order < ActiveRecord::Base
def pay(amount)
if amount > 0

Friday, June 6, 2008

Andrzej's Rails tips #10

form_for and namespace route

When you use a namespace route like the following:

map.namespace :admin do |a|
a.resources :users

then if you want to use form_for @user, this is the correct way:

<% form_for([:admin, @user]) do |f| %>

2 minutes with David Chelimsky and RSpec stories (video)

A short (2.34 minutes) description of RSpec stories.

David Chelimsky at Railsconf 2008 from Gregg Pollack on Vimeo.