aasm/README.md

209 lines
5.5 KiB
Markdown
Raw Normal View History

2012-10-19 06:23:23 +00:00
# AASM - Ruby state machines [![Build Status](https://secure.travis-ci.org/aasm/aasm.png)](http://travis-ci.org/aasm/aasm) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/aasm/aasm)
2008-02-22 23:23:19 +00:00
This package contains AASM, a library for adding finite state machines to Ruby classes.
2008-02-22 23:23:19 +00:00
2012-01-16 16:11:51 +00:00
AASM started as the acts_as_state_machine plugin but has evolved into a more generic library
that no longer targets only ActiveRecord models. It currently provides adapters for
[ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) and
[Mongoid](http://mongoid.org/), but it can be used for any Ruby class, no matter its
parent class.
### Transaction support
Since version 3.0.13 AASM supports ActiveRecord transactions. So whenever a transition
callback fails, all changes to any database record are rolled back.
2008-02-22 23:23:19 +00:00
2011-08-31 19:50:59 +00:00
## Features ##
2008-02-22 23:23:19 +00:00
2008-05-29 14:24:01 +00:00
* States
* Machines
* Events
* Transitions
2008-02-22 23:23:19 +00:00
2011-08-31 19:50:59 +00:00
## New Callbacks ##
2008-02-22 23:23:19 +00:00
2009-02-26 21:18:22 +00:00
The callback chain & order on a successful event looks like:
oldstate:exit*
event:before
__find transition, if possible__
transition:on_transition*
oldstate:before_exit
newstate:before_enter
newstate:enter*
__update state__
event:success*
oldstate:after_exit
newstate:after_enter
event:after
obj:aasm_event_fired*
(*) marks old callbacks
2011-08-31 19:50:59 +00:00
## Installation ##
2011-10-23 19:21:51 +00:00
### Manually from RubyGems.org ###
2011-08-31 19:50:59 +00:00
```sh
% gem install aasm
```
2011-10-23 19:21:51 +00:00
### Or if you are using Bundler ###
```ruby
# Gemfile
gem 'aasm'
```
2011-08-31 19:50:59 +00:00
### Building your own gems ###
2011-08-31 19:50:59 +00:00
```sh
% rake build
2011-08-31 21:56:57 +00:00
% sudo gem install pkg/aasm-x.y.z.gem
2011-08-31 19:50:59 +00:00
```
## Examples ##
### Simple Example ###
Here's a quick example highlighting some of the features.
2011-08-31 19:50:59 +00:00
```ruby
2011-11-26 20:11:57 +00:00
class Conversation
include AASM
2011-11-26 20:11:57 +00:00
aasm :column => :current_state do # defaults to aasm_state
state :unread, :initial => true
state :read
state :closed
2011-11-26 20:11:57 +00:00
event :view do
transitions :to => :read, :from => [:unread]
end
2011-11-26 20:11:57 +00:00
event :close do
transitions :to => :closed, :from => [:read, :unread]
end
end
2011-11-26 20:11:57 +00:00
end
2011-08-31 19:50:59 +00:00
```
### A Slightly More Complex Example ###
This example uses a few of the more complex features available.
2011-08-31 19:50:59 +00:00
```ruby
class Relationship
include AASM
2010-01-17 06:12:18 +00:00
2012-02-05 12:32:32 +00:00
aasm :column => :status do
2011-11-26 20:11:57 +00:00
state :dating, :enter => :make_happy, :exit => :make_depressed
state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover
event :get_intimate do
transitions :to => :intimate, :from => [:dating], :guard => :drunk?
end
# Will allow transitioning from any state if guard allows it
2011-11-26 20:11:57 +00:00
event :get_married do
transitions :to => :married, :guard => :willing_to_give_up_manhood?
2011-11-26 20:11:57 +00:00
end
end
2011-11-26 20:11:57 +00:00
aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }
def strictly_for_fun?; end
def drunk?; end
def willing_to_give_up_manhood?; end
def make_happy; end
def make_depressed; end
def make_very_happy; end
def never_speak_again; end
def give_up_intimacy; end
def buy_exotic_car_and_wear_a_combover; end
end
2011-08-31 19:50:59 +00:00
```
### Callbacks around events ###
2011-09-04 15:59:55 +00:00
```ruby
class Relationship
include AASM
2011-11-26 20:11:57 +00:00
aasm do
state :dating
state :married
2011-09-04 15:59:55 +00:00
2011-11-26 20:11:57 +00:00
event :get_married,
:before => :make_vows,
:after => :eat_wedding_cake do
transitions :to => :married, :from => [:dating]
end
2011-11-26 20:23:36 +00:00
end
2011-09-04 15:59:55 +00:00
end
```
### Persistence example ###
```ruby
class InvalidPersistor < ActiveRecord::Base
include AASM
aasm :column => :status, :skip_validation_on_save => true do
state :sleeping, :initial => true
state :running
event :run do
transitions :to => :running, :from => :sleeping
end
event :sleep do
transitions :to => :sleeping, :from => :running
end
end
validates_presence_of :name
end
```
This model can change AASM states which are stored into the database, even if the model itself is invalid!
## Changelog ##
2011-09-04 15:59:55 +00:00
Look at the [CHANGELOG](https://github.com/aasm/aasm/blob/master/CHANGELOG.md) for details.
## Authors ##
* [Scott Barron](https://github.com/rubyist)
* [Travis Tilley](https://github.com/ttilley)
2011-11-25 23:13:19 +00:00
* [Thorsten Böttger](http://github.com/alto)
2011-08-31 19:50:59 +00:00
## Warranty ##
This software is provided "as is" and without any express or
implied warranties, including, without limitation, the implied
warranties of merchantibility and fitness for a particular
purpose.
## License ##
2012-01-16 16:24:58 +00:00
Copyright (c) 2006-2012 Scott Barron
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.