Wednesday, May 13, 2009

Display RAILS_ENV in rails console

This post is related to this lighthouse ticket.

If you want to have some clue about the current RAILS_ENV (because you need some sleep, or like me you have several terminal instances, with severals rails consoles), just add this to your ~/.irbrc file :

# Prompts
IRB.conf[:PROMPT][:CUSTOM] = {
:PROMPT_N => "[#{ENV["RAILS_ENV"].capitalize}]>> ",
:PROMPT_I => "[#{ENV["RAILS_ENV"].capitalize}]>> ",
:PROMPT_S => nil,
:PROMPT_C => "?> ",
:RETURN => "=> %s\n"
}

# Set default prompt
IRB.conf[:PROMPT_MODE] = :CUSTOM

You will end with a console like mine :

[Development]>> 1+1
=> 2
Enjoy !

Thursday, March 5, 2009

Shoulda 2.10.0 and should_respond_to_xml

Yesterday, after upgrading Shoulda to 2.10.0, I had the bad surprise of having undefined methods for my tests. Especially, should_respond_to_xml has purely disappear from the macros.

If, like me, you don't want to change your tests (because they make sense, of course), simple create a file in RAILS_ROOT/test/shoulda_macros and put this gist into it : http://gist.github.com/74287

Wednesday, March 4, 2009

Let's color Git

A must-have if you're using git, and especially through command line, put this in your ~/.gitconfig :
[color]
diff = auto
status = auto
branch = auto
This will put some (very) useful colors in diffs, status, and branches.
This is particulary useful when using git status, git log -p and git diff.

Thursday, January 22, 2009

Upgrading clearance to 0.4.X

If you were using clearance 0.3.X and want to upgrade to 0.4.X, the generator should help a little. Nevertheless, you should use this migration instead :

class CreateOrUpdateUsersWithClearanceColumns < ActiveRecord::Migration
def self.up
change_table(:users) do |t|
t.rename :crypted_password, :encrypted_password
t.rename :confirmed, :email_confirmed
end
remove_index :users, :name => "index_users_on_email_and_crypted_password"
add_index :users, [:email, :encrypted_password], :name => "index_users_on_email_and_encrypted_password"
end

def self.down
change_table(:users) do |t|
t.rename :encrypted_password, :crypted_password
t.rename :email_confirmed, :confirmed
end
remove_index :users, :name => "index_users_on_email_and_encrypted_password"
add_index :users, [:email, :crypted_password], :name => "index_users_on_email_and_crypted_password"

end
end


Be sure to rename all your login* methods to the new sign* naming convention.

Sunday, January 11, 2009

Variable affectations in ruby

Something I would share today, since I've spend some time yesterday dealing with variables copies and references.

Sounds pretty logical afterward, but it might save you some time :


>> a = {}
=> {}
>> a["my_key"] = "value"
=> "value"
>> a
=> {"my_key"=>"value"}
>> b = a
=> {"my_key"=>"value"}
>> b["my_key"] = "new_value"
=> "new_value"
>> b
=> {"my_key"=>"new_value"}
>> a
=> {"my_key"=>"new_value"}
>>


This simple example demonstrates that affecting an existing Hash to a new variable will send the reference of the Hash, not a copy.
Now, let's try the same with a basic float object :


>> a = 1.0
=> 1.0
>> b = a
=> 1.0
>> b= 2.0
=> 2.0
>> a
=> 1.0


Logical, indeed. But if you're using Hashes and floats in a structure, like in the rails session object, do not forget this example !

Tuesday, January 6, 2009

Clearance : coming from where your were

Clearance will save your current location if you try to access a restricted area, and will forward you to the same place after authentication. Nevertheless, if you put a "Login" link in your pages, your users will be redirected to "/", which is not always what we want. To avoid this, we will use a param to return where we want (this is usually in your app layout) :


link_to t(:login), new_session_url(:return_to => request.request_uri)
Now we have a :return_to param with the correct URI, let's handle this in our session controller :


before_filter :set_return_to, :only => :new

def set_return_to
session[:return_to] = params[:return_to] if params[:return_to]
end


Clearance is using this session var to handle the returning URI.

Now, if you're not on the home page, click on "login", enter your email / password, you will be redirected to the previous page, being identified.

Rails Authentication : Clearance

Some weeks ago, I was looking for a simple authentication system for rails, with minimal generators (so unlike restful-authentication). I was just experiencing some new testing techniques, and was starting using Shoulda and Factory Girl. Thoughtbot did an amazing job there.
Then, I was browsing github for some new auth system, and my eyes were attracted by "shoulda" and "factory girl" in the page. Thoughtbot has released a (very) simple authentication system, with testing based on shoulda and factory girl. After some gem issues, the new 0.3.7 version is ready to be used. With authentication based on stategies (let's say a simple email/password strategy, or facebook connect), this could be the new killer feature for rails.

I'll try to post some usage examples here, to help getting full advantage of the package.

First, let's install the necessary package :


$ sudo gem install thoughtbot-clearance -s http://gems.github.com
Then, you can simply follow instructions here :

http://github.com/thoughtbot/clearance/tree/master