Category Archives: Rails

Authlogic – Rails Authentication Done Right

Sorry to borrow the title line directly from Ben’s site, but Authlogic is the authentication system I have been looking for. Bye, bye restful authentication. Hello easy, simple, get out of my way, easily upgradeable, smartly written Authlogic.

I shouldn’t disrespect restful authentication much as she has been with me for over a year now. But every time I had to install, setup, and get the basics working in RA I couldn’t help gnashing my teeth. I jumped on the RA bandwagon like so many other Rails developers looking for an authentication system that just and move on to more important things.

Enter Authlogic by Ben Johnson of BinaryLogic. By luck, I was starting a new rails app this week, so I decided to take Authlogic out for a test drive. Following Ben’s Tutorial: Authlogic Basic Setup I had a basic login/logout/session management system up and running in a ridiculously little amount of code. The best part of Authlogic is that it truly get’s out of my way and provides me with what I need… a robust and secure methodology of authentication and session management.

The benefits of Authlogic are:

  • It’s a plugin and a gem. When Ben pushes an update, getting the latest release is super simple.
  • It’s a plugin and a gem. This keeps the authentication code separate from your codes, the way it should be.
  • Session are treated like ActiveRecord objects. This is just as cools as it sounds and is very Rails-like.
  • Better security. Authlogic uses a non-reversible token that is changed on every session initiation and password change, thus virtually eliminating session persistence and brute force session attacks.
  • Ben Johnson. Ben knows what he is doing and has been quickly releasing updates.
  • Ben Johnson. Sorry for the repetition, but Ben also has a nice series of tutorials with supporting project code you can download.
Read more on on BinaryLogic or get it at Github.
Posted in Rails. Tagged with , .

Butt Biter: Rails ‘truncate’ Method Broken in Ruby 1.8.7

This one threw me for far too many minutes. In ActionView::Helpers::TextHelper the truncate method is broken for Ruby 1.8.7. I’m not exactly sure the version of ruby that it stopped working for, but I was previously on 1.8.6, and I don’t recall it not working. But after I upgraded to the latest patchlevel of 1.8.7, truncate stopped working.

Update 12/2/08: Fixed in Rails 2.2! (read more)

The error that I received was thus:

ActionView::TemplateError (undefined method `length' for #) on line #83 of dashboard/index.html.erb:

Fortunately, someone has submitted a ticket to Rails core. I hope it’s applied in the Rails 2.2.0 release.

Continue reading

Posted in Rails. Tagged with , .

Ruby on Rails Ubuntu Server VM with VMPlayer

I had to setup a Ubuntu (8.04) Server virtual machine and get Rails up-to-speed on it this morning, so I thought I would document my steps for those who find the need to repeat. Here is what I needed:

What you need to start:

Continue reading

Posted in Rails, Ruby. Tagged with , , .

[UPDATE] Capistrano Deploy for Shared Hosting with GIT Repository

Like I said in my original post Capistrano Deploy for Shared Hosting with GIT Repository, I would add the ability to rotate log files and backup your database. Also added a couple of niceties while I was at it.

  • Updated for Capistrano 2.3
  • Added backup of mysql database to local machine
  • Added log rotation and backup to local machine
  • DRYed things up just a touch (delete_files, delete_directories)

Just run a ‘cap -T’ to see all the new functions.
[sourcecode language=”ruby”]
cap -T
[/sourcecode]

CAP db:backup

‘cap db:backup’ will execute a mysqldump, so obviously, this only works on mysql databases. But you could adapt that to any db fairly easily. It dumps the file and bzips it and then downloads it into a ‘/backups’ directory in your project.

Continue reading

Posted in Rails. Tagged with .

Butt Biter: Fix State Abbreviations && Large Data Set Updates

Ran into a little butt biter today… I imported a few thousand addresses that had the state in long form, like ‘Arizona’. But I commonly used the us_states plugin for popup selection of states. Well, us_states will be looking for state abbreviations, like ‘AZ’ when it selects it’s selected value. So what’s a girl (or dude) to do? You gotta convert them states!

But there are a few concerns:

  • you need to iterate over a large set of records, maybe an entire table
  • there may be memory issues with loading such a large set

The following rake task demonstrates a technique that is more memory efficient than loading the entire model/attributes (table/fields) into memory. Only load the ‘id’ and ‘state’ attributes when you need to find your initial record set. The ‘id’ will be used to find the record for updating, and the state is need to find the new abbreviation.

Continue reading

Posted in Rails. Tagged with , , .

Capistrano Deploy for Shared Hosting with GIT Repository

Would you like to deploy your Rails apps on a shared hosting account via Capistrano? I keep a couple of small projects on A Small Orange and since I update them so infrequently, I can never remember all the steps. Duh, that’s what Capistrano was made for. So I took a night and wrote the following deployment script. There are a couple of requirements:

  • Your project is in a GIT repository, local or remote.
  • The GIT repository only needs to be accessible from your local machine (ASO servers do not support GIT).
  • WARNING: running any part of this script will completely delete your /public_html directory!
  • You only need modify the first FOUR ‘set’ lines.

A shared/system_stopped directory will be created on the server, any files in here will be served when you do a ‘cap deploy:web:disable’. You can add a ‘system_stopped’ directory to your project and those files will automatically be copied into the shared directory.

This script works best if you start from the beginning as this is a new deployment as a couple of directories need to be created:

There are a few commented out lines that you may wish to uncomment. I tried to explain their use, so you may wish to read over each function.

Lest I repeat myself… WARNING: this will completely delete your /public_html directory! Make sure you have a backup before you run any function in this script.

I plan on adding a full db backup option (run before migrations, or at will) and a production.log rotation.

Posted in Rails. Tagged with , , .