AASM installation (using AASM gem in your application)
AASM is a/the state machine plugin, which you can now use as a gem. To do this you need to do 2 things
- Install the GEM
- Tell your app about AASM
Install
sudo gem sources -a http://gems.github.com
sudo gem install rubyist-aasm
note first line only required once per machine for any github gems
Configure Application
This is relatively new in rails. Idea is that you map a gem to a particular library in environment.rb. This is needed to deal with the fact that I might have several different aasm gems installed. Add following inside Rails::Initializer.run do |config| block
config.gem 'rubyist-aasm', :version => '~> 2.0.2', :lib => 'aasm', :source => "http://gems.github.com"
n.b. by configuring the application in this way we can use the rake tasks for gems (rake -T gems) i.e we should be able to install the gem using rake gems:install
AASM gotchas
This one really got me!!
Whats wrong with the following
class Order < ActiveRecord::Base
include AASM
aasm_initial_state :customer
aasm_column :state
aasm_state :address
aasm_event :set_user do
transitions :from => :customer, :to => :address
end
end
The problem is that you don't have a :customer state defined. So horrible things happen when you try and transfer using set_user
so we should have
class Order < ActiveRecord::Base
include AASM
aasm_column :state
aasm_state :customer
aasm_state :address
aasm_initial_state :customer
aasm_event :set_user do
transitions :from => :customer, :to => :address
end
end