aasm/README.md

489 lines
11 KiB
Markdown
Raw Normal View History

2013-08-20 19:47:01 +00:00
# AASM - Ruby state machines
2013-09-05 21:38:52 +00:00
[![Gem Version](https://badge.fury.io/rb/aasm.png)](http://badge.fury.io/rb/aasm) [![Build Status](https://secure.travis-ci.org/aasm/aasm.png?branch=master)](http://travis-ci.org/aasm/aasm) [![Code Climate](https://codeclimate.com/github/aasm/aasm.png)](https://codeclimate.com/github/aasm/aasm) [![Coverage Status](https://coveralls.io/repos/aasm/aasm/badge.png?branch=master)](https://coveralls.io/r/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-10-26 08:32:51 +00:00
AASM started as the *acts_as_state_machine* plugin but has evolved into a more generic library
2012-01-16 16:11:51 +00:00
that no longer targets only ActiveRecord models. It currently provides adapters for
[ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) and
2012-10-25 09:38:05 +00:00
[Mongoid](http://mongoid.org/), but it can be used for any Ruby class, no matter what
parent class it has (if any).
## Usage
2012-10-26 08:32:51 +00:00
Adding a state machine is as simple as including the AASM module and start defining
**states** and **events** together with their **transitions**:
2012-10-25 09:38:05 +00:00
```ruby
class Job
include AASM
aasm do
state :sleeping, :initial => true
state :running
2012-10-26 08:32:51 +00:00
state :cleaning
2012-10-25 09:38:05 +00:00
event :run do
transitions :from => :sleeping, :to => :running
end
2012-10-26 08:32:51 +00:00
event :clean do
transitions :from => :running, :to => :cleaning
end
2012-10-25 09:38:05 +00:00
event :sleep do
2012-10-26 08:32:51 +00:00
transitions :from => [:running, :cleaning], :to => :sleeping
2012-10-25 09:38:05 +00:00
end
end
end
```
2012-10-26 08:32:51 +00:00
This provides you with a couple of public methods for instances of the class `Job`:
2012-10-25 09:38:05 +00:00
```ruby
job = Job.new
job.sleeping? # => true
job.may_run? # => true
job.run
job.running? # => true
job.sleeping? # => false
job.may_run? # => false
job.run # => raises AASM::InvalidTransition
```
2012-10-26 08:32:51 +00:00
If you don't like exceptions and prefer a simple `true` or `false` as response, tell
AASM not to be *whiny*:
2012-10-25 09:38:05 +00:00
```ruby
class Job
...
aasm :whiny_transitions => false do
...
end
end
job.running? # => true
job.may_run? # => false
job.run # => false
```
When firing an event, you can pass a block to the method, it will be called only if
the transition succeeds :
```ruby
job.run do
job.user.notify_job_ran # Will be called if job.may_run? is true
end
```
2012-10-25 09:38:05 +00:00
### Callbacks
2012-10-26 08:32:51 +00:00
You can define a number of callbacks for your transitions. These methods will be
called, when certain criteria are met, like entering a particular state:
2012-10-25 09:38:05 +00:00
```ruby
2012-10-26 08:32:51 +00:00
class Job
2012-10-25 09:38:05 +00:00
include AASM
2012-10-26 08:32:51 +00:00
aasm do
state :sleeping, :initial => true, :before_enter => :do_something
2012-10-25 09:38:05 +00:00
state :running
event :run, :after => Proc.new { |user| notify_somebody(user) } do
2013-02-02 07:10:45 +00:00
transitions :from => :sleeping, :to => :running, :on_transition => Proc.new {|obj, *args| obj.set_process(*args) }
2012-10-25 09:38:05 +00:00
end
event :sleep do
after do
...
end
2013-02-21 08:16:08 +00:00
error do |e|
...
end
2012-10-25 09:38:05 +00:00
transitions :from => :running, :to => :sleeping
end
end
2013-02-02 07:10:45 +00:00
def set_process(name)
...
end
2012-10-26 08:32:51 +00:00
def do_something
...
end
def notify_somebody(user)
2012-10-26 08:32:51 +00:00
...
end
2012-10-25 09:38:05 +00:00
end
```
2012-10-26 08:32:51 +00:00
In this case `do_something` is called before actually entering the state `sleeping`,
while `notify_somebody` is called after the transition `run` (from `sleeping` to `running`)
is finished.
Here you can see a list of all possible callbacks, together with their order of calling:
2012-10-25 09:38:05 +00:00
```ruby
2012-10-26 08:32:51 +00:00
event:before
previous_state:before_exit
new_state:before_enter
...update state...
previous_state:after_exit
new_state:after_enter
event:after
2012-10-25 09:38:05 +00:00
```
2013-02-02 07:10:45 +00:00
Also, you can pass parameters to events:
```ruby
job = Job.new
job.run(:running, :defragmentation)
```
2013-02-02 07:11:31 +00:00
In this case the `set_process` would be called with `:defagmentation` argument.
2013-02-02 07:10:45 +00:00
In case of an error during the event processing the error is rescued and passed to `:error`
callback, which can handle it or re-raise it for further propagation.
2013-02-21 08:16:08 +00:00
2012-10-26 08:32:51 +00:00
### Guards
2012-10-25 09:38:05 +00:00
2012-10-26 08:32:51 +00:00
Let's assume you want to allow particular transitions only if a defined condition is
given. For this you can set up a guard per transition, which will run before actually
running the transition. If the guard returns `false` the transition will be
denied (raising `AASM::InvalidTransition` or returning `false` itself):
2012-10-25 09:38:05 +00:00
2012-10-26 08:32:51 +00:00
```ruby
class Job
include AASM
2012-10-26 08:32:51 +00:00
aasm do
state :sleeping, :initial => true
state :running
state :cleaning
2008-02-22 23:23:19 +00:00
2012-10-26 08:32:51 +00:00
event :run do
transitions :from => :sleeping, :to => :running
end
2008-02-22 23:23:19 +00:00
2012-10-26 08:32:51 +00:00
event :clean do
transitions :from => :running, :to => :cleaning
end
2008-02-22 23:23:19 +00:00
2012-10-26 08:32:51 +00:00
event :sleep do
2012-10-26 11:12:00 +00:00
transitions :from => :running, :to => :sleeping, :guard => :cleaning_needed?
2012-10-26 08:32:51 +00:00
end
end
2008-02-22 23:23:19 +00:00
2012-10-26 11:12:00 +00:00
def cleaning_needed?
2012-10-26 08:32:51 +00:00
false
end
2009-02-26 21:18:22 +00:00
2012-10-26 08:32:51 +00:00
end
2012-10-26 08:32:51 +00:00
job = Job.new
job.run
job.may_sleep? # => false
job.sleep # => raises AASM::InvalidTransition
```
2012-10-26 08:32:51 +00:00
### ActiveRecord
2012-10-26 08:32:51 +00:00
AASM comes with support for ActiveRecord and allows automatical persisting of the object's
state in the database.
2012-10-26 08:32:51 +00:00
```ruby
class Job < ActiveRecord::Base
include AASM
2012-10-26 08:32:51 +00:00
aasm do # default column: aasm_state
state :sleeping, :initial => true
state :running
2011-10-23 19:21:51 +00:00
2012-10-26 08:32:51 +00:00
event :run do
transitions :from => :sleeping, :to => :running
end
2011-10-23 19:21:51 +00:00
2012-10-26 08:32:51 +00:00
event :sleep do
transitions :from => :running, :to => :sleeping
end
end
2012-10-26 08:32:51 +00:00
end
2011-08-31 19:50:59 +00:00
```
2012-10-26 08:32:51 +00:00
You can tell AASM to auto-save the object or leave it unsaved
2012-10-26 08:32:51 +00:00
```ruby
job = Job.new
job.run # not saved
job.run! # saved
```
2012-10-26 08:32:51 +00:00
Saving includes running all validations on the `Job` class. If you want make sure
the state gets saved without running validations (and thereby maybe persisting an
invalid object state), simply tell AASM to skip the validations:
2011-08-31 19:50:59 +00:00
```ruby
2012-10-26 08:32:51 +00:00
class Job < ActiveRecord::Base
2011-11-26 20:11:57 +00:00
include AASM
2012-10-26 08:32:51 +00:00
aasm :skip_validation_on_save => true do
state :sleeping, :initial => true
state :running
2012-10-26 08:32:51 +00:00
event :run do
transitions :from => :sleeping, :to => :running
end
2012-10-26 08:32:51 +00:00
event :sleep do
transitions :from => :running, :to => :sleeping
end
end
2011-11-26 20:11:57 +00:00
end
2011-08-31 19:50:59 +00:00
```
### Mongoid
AASM also supports persistence to Mongodb if you're using Mongoid. Make sure
to include Mongoid::Document before you include AASM.
```ruby
class Job
include Mongoid::Document
include AASM
2013-12-02 11:19:42 +00:00
field :aasm_state
aasm do
...
end
end
```
### Automatic Scopes
AASM will automatically create scope methods for each state in the model.
```ruby
class Job < ActiveRecord::Base
include AASM
aasm do
state :sleeping, :initial => true
state :running
state :cleaning
end
def sleeping
"This method name is in already use"
end
end
```
```ruby
class JobsController < ApplicationController
def index
@running_jobs = jobs.running
@recent_cleaning_jobs = jobs.cleaning.where('created_at >= ?', 3.days.ago)
# @sleeping_jobs = jobs.sleeping #=> "This method name is in already use"
end
end
```
If you don't need scopes (or simply don't want them), disable their creation when
defining the `AASM` states, like this:
```ruby
class Job < ActiveRecord::Base
include AASM
aasm :create_scopes => false do
state :sleeping, :initial => true
state :running
state :cleaning
end
end
```
2012-10-26 08:32:51 +00:00
### Transaction support
Since version *3.0.13* AASM supports ActiveRecord transactions. So whenever a transition
callback or the state update fails, all changes to any database record are rolled back.
Mongodb does not support transactions.
2012-10-26 08:32:51 +00:00
If you want to make sure a depending action happens only after the transaction is committed,
use the `after_commit` callback, like this:
```ruby
class Job < ActiveRecord::Base
include AASM
aasm do
state :sleeping, :initial => true
2013-11-29 14:39:50 +00:00
state :running, :after_commit => :notify_about_running_job
2013-11-29 14:39:50 +00:00
event :run do
transitions :from => :sleeping, :to => :running
end
end
def notify_about_running_job
...
end
end
```
If you want to encapsulate state changes within an own transaction, the behavior
of this nested transaction might be confusing. Take a look at
[ActiveRecord Nested Transactions](http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html)
if you want to know more about this. Nevertheless, AASM by default requires a new transaction
`transaction(:requires_new => true)`. You can override this behavior by changing
the configuration
```ruby
class Job < ActiveRecord::Base
include AASM
aasm :requires_new_transaction => false do
...
end
...
end
```
which then leads to `transaction(:requires_new => false)`, the Rails default.
2012-10-26 08:32:51 +00:00
### Column name & migration
2012-10-26 08:32:51 +00:00
As a default AASM uses the column `aasm_state` to store the states. You can override
this by defining your favorite column name, using `:column` like this:
2011-08-31 19:50:59 +00:00
```ruby
2012-10-26 08:32:51 +00:00
class Job < ActiveRecord::Base
include AASM
aasm :column => 'my_state' do
...
end
2012-10-26 08:32:51 +00:00
end
2011-08-31 19:50:59 +00:00
```
2012-10-26 08:32:51 +00:00
Whatever column name is used, make sure to add a migration to provide this column
(of type `string`):
2011-09-04 15:59:55 +00:00
```ruby
2012-10-26 08:32:51 +00:00
class AddJobState < ActiveRecord::Migration
def self.up
add_column :jobs, :aasm_state, :string
end
def self.down
2013-04-03 15:43:32 +00:00
remove_column :jobs, :aasm_state
2011-09-04 15:59:55 +00:00
end
2012-10-26 08:32:51 +00:00
end
2011-09-04 15:59:55 +00:00
```
### Inspection
AASM supports a couple of methods to find out which states or events are provided or permissible.
Given the `Job` class from above:
```ruby
job = Job.new
job.aasm.states
=> [:sleeping, :running, :cleaning]
job.aasm.states(:permissible => true)
=> [:running]
job.run
job.aasm.states(:permissible => true)
=> [:cleaning, :sleeping]
job.aasm.events
=> [:run, :clean, :sleep]
```
2013-04-28 15:49:59 +00:00
## <a id="installation">Installation ##
2012-10-26 08:32:51 +00:00
### Manually from RubyGems.org ###
```sh
% gem install aasm
```
### Or if you are using Bundler ###
```ruby
2012-10-26 08:32:51 +00:00
# Gemfile
gem 'aasm'
```
2012-10-26 08:32:51 +00:00
### Building your own gems ###
2012-10-26 08:32:51 +00:00
```sh
% rake build
% sudo gem install pkg/aasm-x.y.z.gem
```
2012-10-26 08:32:51 +00:00
## Latest changes ##
2011-09-04 15:59:55 +00:00
Take a look at the [CHANGELOG](https://github.com/aasm/aasm/blob/master/CHANGELOG.md) for details about recent changes to the current version.
## Questions? ##
Feel free to
* [create an issue on GitHub](https://github.com/aasm/aasm/issues)
* [ask a question on StackOverflow](http://stackoverflow.com) (tag with `aasm`)
* send us a tweet [@aasm](http://twitter.com/aasm)
## Maintainers ##
* [Scott Barron](https://github.com/rubyist) (20062009, original author)
* [Travis Tilley](https://github.com/ttilley) (20092011)
* [Thorsten Böttger](http://github.com/alto) (since 2011)
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 ##
Copyright (c) 2006-2014 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.