Ruby on Rails Finite State Machine Plugin: acts_as_state_machine

Note: This is a summary of this article - the page is unaccessible at the time of writing

Installing Finite State Machine

./script/plugin install \
http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk/

Using Finite State Machine

class Person < ActiveRecord::Base
  acts_as_state_machine :initial => :sleeping
  state :sleeping
  state :showering
  state :working
  state :dating

  event :shower do
    transitions :from => :sleeping, :to => :showering
    transitions :from => :working, :to => :showering
    transitions :from => :dating, :to => :showering
  end

  event :work do
    transitions :from => :showering, :to => :working
    # Going to work before showering?  Stinky.
    transitions :from => :sleeping, :to => :working
  end

  event :date do
    transitions :from => :showering, :to => :dating
  end

  event :sleep do
    transitions :from => :showering, :to => :sleeping
    transitions :from => :working, :to => :sleeping
    transitions :from => :dating, :to => :sleeping
  end
end

Note: The plugin makes the assumption that the state of your model is saved in a field called state. This can be replaced by adding the additional option :column => ‘field’. Beware of address fields that have a state field!

We can now see what state an object has by calling state? : Person.find(:first).sleeping?

The specified events also create usefull instance methods to transition from one state to another : Person.find(:first).work!

Disable Leopard Spotlight from the menu bar

If you want to get rid of Leopard’s Spotlight from the menu bar, simply open terminal and run:

sudo chmod 0 /System/Library/CoreServices/Spotlight.app

To re-enable it you just have to run:

sudo chmod 755 /System/Library/CoreServices/Spotlight.app

Terminal in Leopard

I’m loving the new terminal in Leopard. We finally have tabs! Time to say goodbye to iTerm …

Fix backspace in remote shells in iTerm

The backspace key seems bust by default when in an ssh session. To fix, run this command in iTerm :
defaults write com.apple.Terminal TermCapString xterm

Thanks atomized

Update : a simpler ways is to add this line to your .profile

export TERM=xterm

Turning on vi-style editing in script/console

You can turn on vi-style by entering the following line in your ~/.inputrc file :

set editing-mode vi

Thanks slash7!

Using double quotes for HTML attributes in HAML

For some obscure reason, HAML uses single quotes to surround HTML attributes by default. Of course, you probably want double-quotes in your HTML files ;-) Just paste this line in config/environment.rb

Haml::Template.options[:attr_wrapper] = '"'

Convert a table's character set and collation to UTF8 in MySQL

Character encoding is probably my singlest biggest gripe with MySQL. To force a table’s character set and collation to UTF-8, use this command :

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;