2013-08-20 15:47:01 -04:00
# AASM - Ruby state machines
2014-12-01 14:47:27 -05:00
[![Gem Version ](https://badge.fury.io/rb/aasm.svg )](http://badge.fury.io/rb/aasm)
2014-03-21 05:15:13 -04:00
[![Build Status ](https://travis-ci.org/aasm/aasm.svg?branch=master )](https://travis-ci.org/aasm/aasm)
2014-12-01 14:47:27 -05:00
[![Code Climate ](https://codeclimate.com/github/aasm/aasm/badges/gpa.svg )](https://codeclimate.com/github/aasm/aasm)
2019-09-02 01:20:48 -04:00
[![codecov ](https://codecov.io/gh/aasm/aasm/branch/master/graph/badge.svg )](https://codecov.io/gh/aasm/aasm)
2008-02-22 18:23:19 -05:00
2017-08-08 04:13:27 -04:00
## Index
- [Upgrade from version 3 to 4 ](#upgrade-from-version-3-to-4 )
- [Usage ](#usage )
- [Callbacks ](#callbacks )
- [Lifecycle ](#lifecycle )
- [The current event triggered ](#the-current-event-triggered )
- [Guards ](#guards )
- [Transitions ](#transitions )
- [Multiple state machines per class ](#multiple-state-machines-per-class )
- [Handling naming conflicts between multiple state machines ](#handling-naming-conflicts-between-multiple-state-machines )
- [Binding event ](#binding-event )
- [Auto-generated Status Constants ](#auto-generated-status-constants )
- [Extending AASM ](#extending-aasm )
- [ActiveRecord ](#activerecord )
- [Bang events ](#bang-events )
- [ActiveRecord enums ](#activerecord-enums )
- [Sequel ](#sequel )
- [Dynamoid ](#dynamoid )
- [Mongoid ](#mongoid )
2018-01-12 05:41:12 -05:00
- [Nobrainer ](#nobrainer )
2017-08-08 04:13:27 -04:00
- [Redis ](#redis )
- [Automatic Scopes ](#automatic-scopes )
- [Transaction support ](#transaction-support )
- [Pessimistic Locking ](#pessimistic-locking )
- [Column name & migration ](#column-name--migration )
2019-09-03 12:55:06 -04:00
- [Log State Changes ](#log-state-changes )
2017-08-08 04:13:27 -04:00
- [Inspection ](#inspection )
- [Warning output ](#warning-output )
- [RubyMotion support ](#rubymotion-support )
- [Testing ](#testing )
- [RSpec ](#rspec )
- [Minitest ](#minitest )
- [Assertions ](#assertions )
- [Expectations ](#expectations )
- [Installation ](#installation )
- [Manually from RubyGems.org ](#manually-from-rubygemsorg )
- [Bundler ](#or-if-you-are-using-bundler )
- [Building your own gems ](#building-your-own-gems )
- [Generators ](#generators )
2018-02-13 03:08:28 -05:00
- [Test suite with Docker ](#docker )
2017-08-08 04:13:27 -04:00
- [Latest changes ](#latest-changes )
- [Questions? ](#questions )
- [Maintainers ](#maintainers )
- [Contributing ](CONTRIBUTING.md )
- [Warranty ](#warranty )
- [License ](#license )
2008-03-04 18:59:58 -05:00
This package contains AASM, a library for adding finite state machines to Ruby classes.
2008-02-22 18:23:19 -05:00
2012-10-26 04:32:51 -04:00
AASM started as the *acts_as_state_machine* plugin but has evolved into a more generic library
2018-01-12 05:41:12 -05:00
that no longer targets only ActiveRecord models. It currently provides adapters for many
ORMs but it can be used for any Ruby class, no matter what parent class it has (if any).
2012-10-25 05:38:05 -04:00
2014-11-15 11:52:03 -05:00
## Upgrade from version 3 to 4
Take a look at the [README_FROM_VERSION_3_TO_4 ](https://github.com/aasm/aasm/blob/master/README_FROM_VERSION_3_TO_4.md ) for details how to switch from version 3.x to 4.0 of _AASM_ .
2012-10-25 05:38:05 -04:00
## Usage
2012-10-26 04:32:51 -04: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 05:38:05 -04:00
```ruby
class Job
include AASM
aasm do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2015-12-02 04:38:56 -05:00
state :running, :cleaning
2012-10-25 05:38:05 -04:00
event :run do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :running
2012-10-25 05:38:05 -04:00
end
2012-10-26 04:32:51 -04:00
event :clean do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :cleaning
2012-10-26 04:32:51 -04:00
end
2012-10-25 05:38:05 -04:00
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: [:running, :cleaning], to: :sleeping
2012-10-25 05:38:05 -04:00
end
end
end
```
2012-10-26 04:32:51 -04:00
This provides you with a couple of public methods for instances of the class `Job` :
2012-10-25 05:38:05 -04: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 04:32:51 -04: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 05:38:05 -04:00
```ruby
class Job
...
2019-03-07 16:56:19 -05:00
aasm whiny_transitions: false do
2012-10-25 05:38:05 -04:00
...
end
end
job.running? # => true
job.may_run? # => false
job.run # => false
```
2013-11-19 13:58:10 -05:00
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 05:38:05 -04:00
### Callbacks
2018-06-19 14:11:56 -04:00
You can define a number of callbacks for your events, transitions and states. These methods, Procs or classes will be
2018-05-25 07:59:00 -04:00
called when certain criteria are met, like entering a particular state:
2012-10-25 05:38:05 -04:00
```ruby
2012-10-26 04:32:51 -04:00
class Job
2012-10-25 05:38:05 -04:00
include AASM
2012-10-26 04:32:51 -04:00
aasm do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true, before_enter: :do_something
2018-06-19 14:11:56 -04:00
state :running, before_enter: Proc.new { do_something & & notify_somebody }
2016-03-08 04:59:42 -05:00
state :finished
2012-10-25 05:38:05 -04:00
2015-10-16 19:17:57 -04:00
after_all_transitions :log_status_change
2019-03-07 16:56:19 -05:00
event :run, after: :notify_somebody do
2015-08-24 20:06:27 -04:00
before do
log('Preparing to run')
2014-09-12 08:30:36 -04:00
end
2015-08-24 20:06:27 -04:00
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :running, after: Proc.new {|*args| set_process(*args) }
transitions from: :running, to: :finished, after: LogRunTime
2012-10-25 05:38:05 -04:00
end
event :sleep do
2013-02-02 03:22:52 -05:00
after do
...
end
2013-02-21 03:16:08 -05:00
error do |e|
...
end
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :sleeping
2012-10-25 05:38:05 -04:00
end
end
2015-10-16 19:17:57 -04:00
def log_status_change
puts "changing from #{aasm.from_state} to #{aasm.to_state} (event: #{aasm.current_event})"
end
2013-02-02 02:10:45 -05:00
def set_process(name)
...
end
2012-10-26 04:32:51 -04:00
def do_something
...
end
2016-07-05 03:42:15 -04:00
def notify_somebody
2012-10-26 04:32:51 -04:00
...
end
2012-10-25 05:38:05 -04:00
end
2016-02-07 11:51:52 -05:00
class LogRunTime
def call
log "Job was running for X seconds"
end
end
2012-10-25 05:38:05 -04:00
```
2012-10-26 04:32:51 -04:00
In this case `do_something` is called before actually entering the state `sleeping` ,
2014-01-29 15:43:04 -05:00
while `notify_somebody` is called after the transition `run` (from `sleeping` to `running` )
2012-10-26 04:32:51 -04:00
is finished.
2016-02-18 20:53:58 -05:00
AASM will also initialize `LogRunTime` and run the `call` method for you after the transition from `running` to `finished` in the example above. You can pass arguments to the class by defining an initialize method on it, like this:
2016-02-07 11:51:52 -05:00
2018-06-19 14:11:56 -04:00
Note that Procs are executed in the context of a record, it means that you don't need to expect the record as an argument, just call the methods you need.
2016-04-28 10:49:13 -04:00
```ruby
2016-02-07 11:51:52 -05:00
class LogRunTime
# optional args parameter can be omitted, but if you define initialize
# you must accept the model instance as the first parameter to it.
def initialize(job, args = {})
@job = job
end
2016-03-18 10:43:40 -04:00
2016-02-07 11:51:52 -05:00
def call
log "Job was running for #{@job.run_time} seconds"
end
end
```
2013-02-02 02:10:45 -05:00
Also, you can pass parameters to events:
```ruby
job = Job.new
2019-05-20 07:18:31 -04:00
job.run(:defragmentation)
2013-02-02 02:10:45 -05:00
```
2014-07-21 19:49:46 -04:00
In this case the `set_process` would be called with `:defragmentation` argument.
2013-02-02 02:10:45 -05:00
2013-02-21 15:35:06 -05: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 03:16:08 -05:00
2017-04-05 20:58:00 -04:00
Also, you can define a method that will be called if any event fails:
2018-05-24 12:28:57 -04:00
```ruby
2017-04-12 22:29:10 -04:00
def aasm_event_failed(event_name, old_state_name)
2017-04-05 20:58:00 -04:00
# use custom exception/messages, report metrics, etc
end
```
2015-10-16 19:17:57 -04:00
During the transition's `:after` callback (and reliably only then, or in the global
`after_all_transitions` callback) you can access the originating state (the from-state)
and the target state (the to state), like this:
2014-01-27 15:51:38 -05:00
```ruby
def set_process(name)
logger.info "from #{aasm.from_state} to #{aasm.to_state}"
end
```
2017-05-19 09:45:25 -04:00
#### Lifecycle
Here you can see a list of all possible callbacks, together with their order of calling:
```ruby
begin
event before_all_events
event before
event guards
transition guards
old_state before_exit
old_state exit
after_all_transitions
transition after
new_state before_enter
new_state enter
...update state...
event before_success # if persist successful
2020-06-19 06:49:55 -04:00
transition success # if persist successful, database update not guaranteed
event success # if persist successful, database update not guaranteed
2017-05-19 09:45:25 -04:00
old_state after_exit
new_state after_enter
event after
event after_all_events
rescue
event error
event error_on_all_events
ensure
event ensure
event ensure_on_all_events
end
```
2020-06-19 06:49:55 -04:00
Use event's `after_commit` callback if it should be fired after database update.
2014-09-12 05:42:01 -04:00
#### The current event triggered
While running the callbacks you can easily retrieve the name of the event triggered
by using `aasm.current_event` :
```ruby
# taken the example callback from above
def do_something
puts "triggered #{aasm.current_event}"
end
```
and then
```ruby
job = Job.new
# without bang
job.sleep # => triggered :sleep
# with bang
job.sleep! # => triggered :sleep!
```
2012-10-26 04:32:51 -04:00
### Guards
2012-10-25 05:38:05 -04:00
2012-10-26 04:32:51 -04: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 05:38:05 -04:00
2012-10-26 04:32:51 -04:00
```ruby
2014-09-12 08:30:36 -04:00
class Cleaner
2012-10-26 04:32:51 -04:00
include AASM
2012-10-24 04:03:09 -04:00
2012-10-26 04:32:51 -04:00
aasm do
2019-03-07 16:56:19 -05:00
state :idle, initial: true
2012-10-26 04:32:51 -04:00
state :cleaning
2008-02-22 18:23:19 -05:00
2012-10-26 04:32:51 -04:00
event :clean do
2019-03-07 16:56:19 -05:00
transitions from: :idle, to: :cleaning, guard: :cleaning_needed?
2012-10-26 04:32:51 -04:00
end
2008-02-22 18:23:19 -05:00
2014-09-12 08:30:36 -04:00
event :clean_if_needed do
2019-03-07 16:56:19 -05:00
transitions from: :idle, to: :cleaning do
2014-09-12 08:30:36 -04:00
guard do
cleaning_needed?
end
end
2019-03-07 16:56:19 -05:00
transitions from: :idle, to: :idle
2012-10-26 04:32:51 -04:00
end
2017-08-08 13:20:58 -04:00
2017-07-27 04:19:20 -04:00
event :clean_if_dirty do
2019-03-07 16:56:19 -05:00
transitions from: :idle, to: :cleaning, guard: :if_dirty?
2017-07-27 04:19:20 -04:00
end
2012-10-26 04:32:51 -04:00
end
2008-02-22 18:23:19 -05:00
2012-10-26 07:12:00 -04:00
def cleaning_needed?
2012-10-26 04:32:51 -04:00
false
end
2017-08-08 13:20:58 -04:00
2017-07-27 04:19:20 -04:00
def if_dirty?(status)
status == :dirty
end
2012-10-26 04:32:51 -04:00
end
2011-08-31 15:56:09 -04:00
2014-09-12 08:30:36 -04:00
job = Cleaner.new
job.may_clean? # => false
job.clean # => raises AASM::InvalidTransition
job.may_clean_if_needed? # => true
job.clean_if_needed! # idle
2017-07-27 04:19:20 -04:00
job.clean_if_dirty(:clean) # => false
job.clean_if_dirty(:dirty) # => true
2012-10-26 04:32:51 -04:00
```
2008-03-04 18:59:58 -05:00
2014-01-24 05:12:43 -05:00
You can even provide a number of guards, which all have to succeed to proceed
```ruby
2014-01-24 06:02:22 -05:00
def walked_the_dog?; ...; end
2014-01-24 05:12:43 -05:00
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :sleeping, guards: [:cleaning_needed?, :walked_the_dog?]
2014-01-24 05:12:43 -05:00
end
```
2008-03-04 18:59:58 -05:00
2014-07-17 09:23:54 -04:00
If you want to provide guards for all transitions within an event, you can use event guards
2014-01-24 06:02:22 -05:00
```ruby
2019-03-07 16:56:19 -05:00
event :sleep, guards: [:walked_the_dog?] do
transitions from: :running, to: :sleeping, guards: [:cleaning_needed?]
transitions from: :cleaning, to: :sleeping
2014-01-24 06:02:22 -05:00
end
```
2014-11-14 12:08:34 -05:00
If you prefer a more Ruby-like guard syntax, you can use `if` and `unless` as well:
```ruby
event :clean do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :cleaning, if: :cleaning_needed?
2014-11-14 12:08:34 -05:00
end
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :sleeping, unless: :cleaning_needed?
2014-11-14 12:08:34 -05:00
end
end
```
2019-03-07 16:56:19 -05:00
You can invoke a Class instead a method since this Class responds to `call`
2018-11-29 08:02:10 -05:00
```ruby
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :sleeping, guards: Dog
2018-11-29 08:02:10 -05:00
end
```
```ruby
class Dog
def call
cleaning_needed? & & walked?
end
...
end
```
2014-11-14 12:08:34 -05:00
2014-10-07 15:50:20 -04:00
### Transitions
In the event of having multiple transitions for an event, the first transition that successfully completes will stop other transitions in the same event from being processed.
```ruby
require 'aasm'
class Job
include AASM
aasm do
2019-03-07 16:56:19 -05:00
state :stage1, initial: true
2014-10-07 15:50:20 -04:00
state :stage2
state :stage3
state :completed
event :stage1_completed do
transitions from: :stage1, to: :stage3, guard: :stage2_completed?
transitions from: :stage1, to: :stage2
end
end
def stage2_completed?
true
end
end
job = Job.new
job.stage1_completed
job.aasm.current_state # stage3
```
2020-08-12 10:49:59 -04:00
You can define transition from any defined state by omitting `from` :
```ruby
event :abort do
transitions to: :aborted
end
```
2020-08-11 08:10:16 -04:00
### Display name for state
You can define display name for state using :display option
```ruby
class Job
include AASM
aasm do
state :stage1, initial: true, display: 'First Stage'
state :stage2
state :stage3
end
end
job = Job.new
job.aasm.human_state
```
2015-09-08 06:40:18 -04:00
2015-09-10 06:06:41 -04:00
### Multiple state machines per class
2015-09-08 06:40:18 -04:00
2015-09-10 06:07:52 -04:00
Multiple state machines per class are supported. Be aware though that _AASM_ has been
2020-03-22 12:29:22 -04:00
built with one state machine per class in mind. Nonetheless, here's how to do it (see below). Please note that you will need to specify database columns for where your pertinent states will be stored - we have specified two columns `move_state` and `work_state` in the example below. See the [Column name & migration ](https://github.com/aasm/aasm#column-name--migration ) section for further info.
2015-09-08 06:40:18 -04:00
```ruby
class SimpleMultipleExample
include AASM
2019-09-24 10:12:46 -04:00
aasm(:move, column: 'move_state') do
2019-03-07 16:56:19 -05:00
state :standing, initial: true
2015-09-08 06:40:18 -04:00
state :walking
state :running
event :walk do
2019-03-07 16:56:19 -05:00
transitions from: :standing, to: :walking
2015-09-08 06:40:18 -04:00
end
event :run do
2019-03-07 16:56:19 -05:00
transitions from: [:standing, :walking], to: :running
2015-09-08 06:40:18 -04:00
end
event :hold do
2019-03-07 16:56:19 -05:00
transitions from: [:walking, :running], to: :standing
2015-09-08 06:40:18 -04:00
end
end
2019-09-24 10:12:46 -04:00
aasm(:work, column: 'work_state') do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2015-09-08 06:40:18 -04:00
state :processing
event :start do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :processing
2015-09-08 06:40:18 -04:00
end
event :stop do
2019-03-07 16:56:19 -05:00
transitions from: :processing, to: :sleeping
2015-09-08 06:40:18 -04:00
end
end
end
simple = SimpleMultipleExample.new
simple.aasm(:move).current_state
# => :standing
simple.aasm(:work).current
# => :sleeping
simple.start
simple.aasm(:move).current_state
# => :standing
simple.aasm(:work).current
# => :processing
```
2017-06-06 16:53:23 -04:00
#### Handling naming conflicts between multiple state machines
_AASM_ doesn't prohibit to define the same event in more than one state
machine. If no namespace is provided, the latest definition "wins" and
overrides previous definitions. Nonetheless, a warning is issued:
2016-03-15 13:40:13 -04:00
`SimpleMultipleExample: overriding method 'run'!` .
2015-09-08 06:40:18 -04:00
2017-06-06 16:53:23 -04:00
Alternatively, you can provide a namespace for each state machine:
```ruby
class NamespacedMultipleExample
include AASM
aasm(:status) do
2019-03-07 16:56:19 -05:00
state :unapproved, initial: true
2017-06-06 16:53:23 -04:00
state :approved
event :approve do
2019-03-07 16:56:19 -05:00
transitions from: :unapproved, to: :approved
2017-06-06 16:53:23 -04:00
end
event :unapprove do
2019-03-07 16:56:19 -05:00
transitions from: :approved, to: :unapproved
2017-06-06 16:53:23 -04:00
end
end
aasm(:review_status, namespace: :review) do
2019-03-07 16:56:19 -05:00
state :unapproved, initial: true
2017-06-06 16:53:23 -04:00
state :approved
event :approve do
2019-03-07 16:56:19 -05:00
transitions from: :unapproved, to: :approved
2017-06-06 16:53:23 -04:00
end
event :unapprove do
2019-03-07 16:56:19 -05:00
transitions from: :approved, to: :unapproved
2017-06-06 16:53:23 -04:00
end
end
end
namespaced = NamespacedMultipleExample.new
namespaced.aasm(:status).current_state
# => :unapproved
namespaced.aasm(:review_status).current_state
# => :unapproved
namespaced.approve_review
namespaced.aasm(:review_status).current_state
# => :approved
```
2015-09-08 06:40:18 -04:00
All _AASM_ class- and instance-level `aasm` methods accept a state machine selector.
So, for example, to use inspection on a class level, you have to use
```ruby
2018-05-18 14:11:32 -04:00
SimpleMultipleExample.aasm(:move).states.map(& :name)
2015-09-08 06:40:18 -04:00
# => [:standing, :walking, :running]
```
2017-02-28 05:06:59 -05:00
### Binding event
Allow an event to be bound to another
```ruby
class Example
include AASM
aasm(:work) do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2017-02-28 05:06:59 -05:00
state :processing
event :start do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :processing
2017-02-28 05:06:59 -05:00
end
event :stop do
2019-03-07 16:56:19 -05:00
transitions from: :processing, to: :sleeping
2017-02-28 05:06:59 -05:00
end
end
aasm(:question) do
2019-03-07 16:56:19 -05:00
state :answered, initial: true
2017-02-28 05:06:59 -05:00
state :asked
2019-03-07 16:56:19 -05:00
event :ask, binding_event: :start do
transitions from: :answered, to: :asked
2017-02-28 05:06:59 -05:00
end
2019-03-07 16:56:19 -05:00
event :answer, binding_event: :stop do
transitions from: :asked, to: :answered
2017-02-28 05:06:59 -05:00
end
end
end
example = Example.new
example.aasm(:work).current_state #=> :sleeping
example.aasm(:question).current_state #=> :answered
example.ask
example.aasm(:work).current_state #=> :processing
example.aasm(:question).current_state #=> :asked
```
2016-07-19 19:32:34 -04:00
### Auto-generated Status Constants
AASM automatically [generates constants ](https://github.com/aasm/aasm/pull/60 )
for each status so you don't have to explicitly define them.
```ruby
class Foo
include AASM
aasm do
state :initialized
state :calculated
state :finalized
end
end
> Foo::STATE_INITIALIZED
#=> :initialized
> Foo::STATE_CALCULATED
#=> :calculated
```
2015-12-18 13:27:42 -05:00
### Extending AASM
AASM allows you to easily extend `AASM::Base` for your own application purposes.
Let's suppose we have common logic across many AASM models. We can embody this logic in a sub-class of `AASM::Base` .
2016-02-11 01:59:51 -05:00
```ruby
2015-12-18 13:27:42 -05:00
class CustomAASMBase < AASM::Base
# A custom transiton that we want available across many AASM models.
def count_transitions!
klass.class_eval do
2019-03-07 16:56:19 -05:00
aasm with_klass: CustomAASMBase do
2015-12-18 13:27:42 -05:00
after_all_transitions :increment_transition_count
end
end
end
# A custom annotation that we want available across many AASM models.
def requires_guards!
klass.class_eval do
attr_reader :authorizable_called,
:transition_count,
:fillable_called
def authorizable?
@authorizable_called = true
end
def fillable?
@fillable_called = true
end
def increment_transition_count
@transition_count ||= 0
@transition_count += 1
end
end
end
end
```
2017-12-19 16:44:30 -05:00
When we declare our model that has an AASM state machine, we simply declare the AASM block with a `:with_klass` key to our own class.
2015-12-18 13:27:42 -05:00
2016-02-11 01:59:51 -05:00
```ruby
2015-12-18 13:27:42 -05:00
class SimpleCustomExample
include AASM
# Let's build an AASM state machine with our custom class.
2019-03-07 16:56:19 -05:00
aasm with_klass: CustomAASMBase do
2015-12-18 13:27:42 -05:00
requires_guards!
count_transitions!
2019-03-07 16:56:19 -05:00
state :initialised, initial: true
2015-12-18 13:27:42 -05:00
state :filled_out
state :authorised
event :fill_out do
2019-03-07 16:56:19 -05:00
transitions from: :initialised, to: :filled_out, guard: :fillable?
2015-12-18 13:27:42 -05:00
end
event :authorise do
2019-03-07 16:56:19 -05:00
transitions from: :filled_out, to: :authorised, guard: :authorizable?
2015-12-18 13:27:42 -05:00
end
end
end
```
2015-09-08 06:40:18 -04:00
2012-10-26 04:32:51 -04:00
### ActiveRecord
2008-03-04 18:59:58 -05:00
2015-08-24 20:29:05 -04:00
AASM comes with support for ActiveRecord and allows automatic persisting of the object's
2012-10-26 04:32:51 -04:00
state in the database.
2009-10-23 16:33:01 -04:00
2020-05-12 14:08:09 -04:00
Add `gem 'after_commit_everywhere', '~> 0.1', '>= 0.1.5'` to your Gemfile
2012-10-26 04:32:51 -04:00
```ruby
class Job < ActiveRecord::Base
include AASM
2008-04-29 20:51:12 -04:00
2012-10-26 04:32:51 -04:00
aasm do # default column: aasm_state
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2012-10-26 04:32:51 -04:00
state :running
2011-10-23 15:21:51 -04:00
2012-10-26 04:32:51 -04:00
event :run do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :running
2012-10-26 04:32:51 -04:00
end
2011-10-23 15:21:51 -04:00
2012-10-26 04:32:51 -04:00
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :sleeping
2012-10-26 04:32:51 -04:00
end
end
2008-03-04 18:59:58 -05:00
2012-10-26 04:32:51 -04:00
end
2011-08-31 15:50:59 -04:00
```
2008-03-04 18:59:58 -05:00
2017-02-28 05:16:55 -05:00
### Bang events
2012-10-26 04:32:51 -04:00
You can tell AASM to auto-save the object or leave it unsaved
2012-01-16 11:27:15 -05:00
2012-10-26 04:32:51 -04:00
```ruby
job = Job.new
job.run # not saved
job.run! # saved
2017-08-08 13:20:58 -04:00
# or
2017-09-07 07:43:01 -04:00
job.aasm.fire(:run) # not saved
job.aasm.fire!(:run) # saved
2012-10-26 04:32:51 -04:00
```
2008-03-04 18:59:58 -05:00
2016-10-19 22:54:13 -04:00
Saving includes running all validations on the `Job` class. If
`whiny_persistence` flag is set to `true` , exception is raised in case of
failure. If `whiny_persistence` flag is set to false, methods with a bang return
`true` if the state transition is successful or `false` if an error occurs.
2016-01-07 15:44:10 -05:00
If you want make sure the state gets saved without running validations (and
2016-06-01 12:56:59 -04:00
thereby maybe persisting an invalid object state), simply tell AASM to skip the
2016-01-07 15:44:10 -05:00
validations. Be aware that when skipping validations, only the state column will
2016-04-26 21:50:27 -04:00
be updated in the database (just like ActiveRecord `update_column` is working).
2008-03-04 18:59:58 -05:00
2011-08-31 15:50:59 -04:00
```ruby
2012-10-26 04:32:51 -04:00
class Job < ActiveRecord::Base
2011-11-26 15:11:57 -05:00
include AASM
2008-03-04 18:59:58 -05:00
2019-03-07 16:56:19 -05:00
aasm skip_validation_on_save: true do
state :sleeping, initial: true
2012-10-26 04:32:51 -04:00
state :running
2008-03-04 18:59:58 -05:00
2012-10-26 04:32:51 -04:00
event :run do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :running
2008-03-04 18:59:58 -05:00
end
2012-10-26 04:32:51 -04:00
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :sleeping
2008-03-04 18:59:58 -05:00
end
end
2011-11-26 15:11:57 -05:00
end
2011-08-31 15:50:59 -04:00
```
2008-03-04 18:59:58 -05:00
2019-07-30 08:31:01 -04:00
Also You can skip the validation at instance level with `some_event_name_without_validation!` method.
With this you have the flexibility of having validation for all your transitions by default and then skip it wherever required.
Please note that only state column will be updated as mentioned in the above example.
```ruby
job.run_without_validation!
```
2014-05-18 08:21:35 -04:00
If you want to make sure that the _AASM_ column for storing the state is not directly assigned,
configure _AASM_ to not allow direct assignment, like this:
```ruby
class Job < ActiveRecord::Base
include AASM
2019-03-07 16:56:19 -05:00
aasm no_direct_assignment: true do
state :sleeping, initial: true
2014-05-18 08:21:35 -04:00
state :running
event :run do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :running
2014-05-18 08:21:35 -04:00
end
end
end
```
resulting in this:
```ruby
job = Job.create
job.aasm_state # => 'sleeping'
job.aasm_state = :running # => raises AASM::NoDirectAssignmentError
job.aasm_state # => 'sleeping'
```
2014-05-10 05:02:11 -04:00
#### ActiveRecord enums
You can use
[enumerations ](http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html )
in Rails 4.1+ for your state column:
```ruby
class Job < ActiveRecord::Base
include AASM
2014-07-08 06:46:42 -04:00
enum state: {
2014-05-10 05:02:11 -04:00
sleeping: 5,
running: 99
}
2019-03-07 16:56:19 -05:00
aasm column: :state, enum: true do
state :sleeping, initial: true
2014-05-10 05:02:11 -04:00
state :running
end
end
```
2014-05-15 04:04:57 -04:00
You can explicitly pass the name of the method which provides access
to the enumeration mapping as a value of ```enum```, or you can simply
2014-05-15 04:09:22 -04:00
set it to ```true```. In the latter case AASM will try to use
pluralized column name to access possible enum states.
2014-05-15 04:04:57 -04:00
Furthermore, if your column has integer type (which is normally the
2014-05-15 04:09:22 -04:00
case when you're working with Rails enums), you can omit ```:enum```
setting --- AASM auto-detects this situation and enabled enum
support. If anything goes wrong, you can disable enum functionality
2014-05-15 04:10:22 -04:00
and fall back to the default behavior by setting ```:enum```
to ```false```.
2014-05-10 05:02:11 -04:00
2014-05-04 06:12:04 -04:00
### Sequel
2017-03-08 12:59:42 -05:00
AASM also supports [Sequel ](http://sequel.jeremyevans.net/ ) besides _ActiveRecord_ , and _Mongoid_ .
2014-05-04 06:12:04 -04:00
```ruby
class Job < Sequel::Model
include AASM
aasm do # default column: aasm_state
...
end
end
```
2014-05-05 15:54:33 -04:00
However it's not yet as feature complete as _ActiveRecord_ . For example, there are
scopes defined yet. See [Automatic Scopes ](#automatic-scopes ).
2014-05-04 06:12:04 -04:00
2016-02-04 21:31:49 -05:00
### Dynamoid
Since version `4.8.0` _AASM_ also supports [Dynamoid ](http://joshsymonds.com/Dynamoid/ ) as
persistence ORM.
2013-08-29 11:05:43 -04: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 06:19:42 -05:00
field :aasm_state
2013-08-29 11:05:43 -04:00
aasm do
...
end
end
```
2018-01-12 05:41:12 -05:00
### NoBrainer
AASM also supports persistence to [RethinkDB ](https://www.rethinkdb.com/ )
if you're using [Nobrainer ](http://nobrainer.io/ ).
Make sure to include NoBrainer::Document before you include AASM.
```ruby
class Job
include NoBrainer::Document
include AASM
field :aasm_state
aasm do
...
end
end
```
2014-12-02 20:46:11 -05:00
### Redis
2016-03-18 23:53:23 -04:00
2017-07-11 06:42:49 -04:00
AASM also supports persistence in Redis via
[Redis::Objects ](https://github.com/nateware/redis-objects ).
Make sure to include Redis::Objects before you include AASM. Note that non-bang
events will work as bang events, persisting the changes on every call.
2014-12-02 20:46:11 -05:00
```ruby
class User
include Redis::Objects
2014-12-09 13:16:12 -05:00
include AASM
2014-12-02 20:46:11 -05:00
aasm do
end
end
```
2013-01-06 17:42:05 -05:00
### Automatic Scopes
AASM will automatically create scope methods for each state in the model.
```ruby
class Job < ActiveRecord::Base
include AASM
aasm do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2013-01-06 17:42:05 -05:00
state :running
state :cleaning
end
2014-04-30 23:17:15 -04:00
def self.sleeping
2014-09-16 09:28:45 -04:00
"This method name is already in use"
2013-01-06 17:42:05 -05:00
end
end
```
```ruby
class JobsController < ApplicationController
def index
2014-05-03 06:24:30 -04:00
@running_jobs = Job.running
@recent_cleaning_jobs = Job.cleaning.where('created_at >= ?', 3.days.ago)
2013-01-06 17:42:05 -05:00
2014-09-16 09:28:45 -04:00
# @sleeping_jobs = Job.sleeping #=> "This method name is already in use"
2013-01-06 17:42:05 -05:00
end
end
```
2013-08-06 10:55:32 -04:00
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
2019-03-07 16:56:19 -05:00
aasm create_scopes: false do
state :sleeping, initial: true
2013-08-06 10:55:32 -04:00
state :running
state :cleaning
end
end
```
2012-10-26 04:32:51 -04: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.
2013-08-29 11:05:43 -04:00
Mongodb does not support transactions.
2012-10-26 04:32:51 -04:00
2015-11-05 13:37:51 -05:00
There are currently 3 transactional callbacks that can be handled on the event, and 2 transactional callbacks for all events.
```ruby
event before_all_transactions
event before_transaction
event aasm_fire_event (within transaction)
event after_commit (if event successful)
event after_transaction
event after_all_transactions
```
2013-10-24 09:29:18 -04:00
If you want to make sure a depending action happens only after the transaction is committed,
2015-11-18 14:39:53 -05:00
use the `after_commit` callback along with the auto-save (bang) methods, like this:
2013-10-24 09:29:18 -04:00
```ruby
class Job < ActiveRecord::Base
include AASM
aasm do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2014-03-17 16:58:44 -04:00
state :running
2013-10-24 09:29:18 -04:00
2019-03-07 16:56:19 -05:00
event :run, after_commit: :notify_about_running_job do
transitions from: :sleeping, to: :running
2013-10-24 09:29:18 -04:00
end
end
def notify_about_running_job
...
end
end
2015-11-18 14:39:53 -05:00
job = Job.where(state: 'sleeping').first!
job.run! # Saves the model and triggers the after_commit callback
```
Note that the following will not run the `after_commit` callbacks because
the auto-save method is not used:
```ruby
job = Job.where(state: 'sleeping').first!
job.run
job.save! #notify_about_running_job is not run
2013-10-24 09:29:18 -04:00
```
2020-03-05 00:07:29 -05:00
Please note that `:after_commit` AASM callbacks behaves around custom implementation
of transaction pattern rather than a real-life DB transaction. This fact still causes
the race conditions and redundant callback calls within nested transaction. In order
2020-03-22 12:29:22 -04:00
to fix that it's highly recommended to add `gem 'after_commit_everywhere', '~> 0.1', '>= 0.1.5'`
to your `Gemfile` .
2020-03-05 00:07:29 -05:00
2014-01-22 15:28:40 -05:00
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
2019-03-07 16:56:19 -05:00
`transaction(requires_new: true)` . You can override this behavior by changing
2014-01-22 15:28:40 -05:00
the configuration
```ruby
class Job < ActiveRecord::Base
include AASM
2019-03-07 16:56:19 -05:00
aasm requires_new_transaction: false do
2014-01-22 15:28:40 -05:00
...
end
...
end
```
2019-03-07 16:56:19 -05:00
which then leads to `transaction(requires_new: false)` , the Rails default.
2014-01-22 15:28:40 -05:00
2017-05-30 17:22:38 -04:00
Additionally, if you do not want any of your active record actions to be
wrapped in a transaction, you can specify the `use_transactions` flag. This can
be useful if you want want to persist things to the database that happen as a
result of a transaction or callback, even when some error occurs. The
`use_transactions` flag is true by default.
```ruby
class Job < ActiveRecord::Base
include AASM
2019-03-07 16:56:19 -05:00
aasm use_transactions: false do
2017-05-30 17:22:38 -04:00
...
end
...
end
```
2015-11-06 12:28:12 -05:00
### Pessimistic Locking
AASM supports [Active Record pessimistic locking via `with_lock` ](http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html#method-i-with_lock ) for database persistence layers.
| Option | Purpose |
| ------ | ------- |
| `false` (default) | No lock is obtained | |
| `true` | Obtain a blocking pessimistic lock e.g. `FOR UPDATE` |
| String | Obtain a lock based on the SQL string e.g. `FOR UPDATE NOWAIT` |
```ruby
class Job < ActiveRecord::Base
include AASM
2019-03-07 16:56:19 -05:00
aasm requires_lock: true do
2015-11-06 12:28:12 -05:00
...
end
...
end
```
```ruby
class Job < ActiveRecord::Base
include AASM
2019-03-07 16:56:19 -05:00
aasm requires_lock: 'FOR UPDATE NOWAIT' do
2015-11-06 12:28:12 -05:00
...
end
...
end
```
2014-01-22 15:28:40 -05:00
2012-10-26 04:32:51 -04:00
### Column name & migration
2009-02-26 12:06:43 -05:00
2012-10-26 04:32:51 -04: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:
2009-02-26 12:06:43 -05:00
2011-08-31 15:50:59 -04:00
```ruby
2012-10-26 04:32:51 -04:00
class Job < ActiveRecord::Base
include AASM
2020-01-19 13:15:15 -05:00
aasm column: :my_state do
2012-10-26 04:32:51 -04:00
...
2009-02-26 12:06:43 -05:00
end
2012-10-26 04:32:51 -04:00
2020-01-19 13:15:15 -05:00
aasm :another_state_machine, column: :second_state do
2017-04-13 05:49:45 -04:00
...
end
2012-10-26 04:32:51 -04:00
end
2011-08-31 15:50:59 -04:00
```
2009-02-26 12:06:43 -05:00
2012-10-26 04:32:51 -04:00
Whatever column name is used, make sure to add a migration to provide this column
2020-06-16 06:45:44 -04:00
(of type `string` ).
Do not add default value for column at the database level. If you add default
value in database then AASM callbacks on the initial state will not be fired upon
instantiation of the model.
2012-10-26 04:32:51 -04:00
2011-09-04 11:59:55 -04:00
```ruby
2012-10-26 04:32:51 -04:00
class AddJobState < ActiveRecord::Migration
def self.up
add_column :jobs, :aasm_state, :string
end
def self.down
2013-04-03 11:43:32 -04:00
remove_column :jobs, :aasm_state
2011-09-04 11:59:55 -04:00
end
2012-10-26 04:32:51 -04:00
end
2011-09-04 11:59:55 -04:00
```
2019-09-03 12:55:06 -04:00
### Log State Changes
2020-03-22 12:29:22 -04:00
Logging state change can be done using [paper_trail ](https://github.com/paper-trail-gem/paper_trail ) gem
2019-09-03 12:55:06 -04:00
Example of implementation can be found here [https://github.com/nitsujri/aasm-papertrail-example ](https://github.com/nitsujri/aasm-papertrail-example )
2013-10-24 09:29:18 -04:00
### Inspection
2013-04-28 10:42:48 -04:00
2016-05-14 18:14:10 -04:00
AASM supports query methods for states and events
2013-04-28 10:42:48 -04:00
2016-05-14 18:14:10 -04:00
Given the following `Job` class:
2016-06-19 01:37:11 -04:00
2016-05-14 18:14:10 -04:00
```ruby
class Job
include AASM
aasm do
2019-03-07 16:56:19 -05:00
state :sleeping, initial: true
2016-05-14 18:14:10 -04:00
state :running, :cleaning
event :run do
2019-03-07 16:56:19 -05:00
transitions from: :sleeping, to: :running
2016-05-14 18:14:10 -04:00
end
event :clean do
2019-03-07 16:56:19 -05:00
transitions from: :running, to: :cleaning, guard: :cleaning_needed?
2016-05-14 18:14:10 -04:00
end
event :sleep do
2019-03-07 16:56:19 -05:00
transitions from: [:running, :cleaning], to: :sleeping
2016-05-14 18:14:10 -04:00
end
end
2016-06-19 01:37:11 -04:00
2016-05-14 18:14:10 -04:00
def cleaning_needed?
false
end
end
```
2013-04-28 10:42:48 -04:00
```ruby
2014-10-10 17:41:59 -04:00
# show all states
2016-11-18 04:21:42 -05:00
Job.aasm.states.map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:sleeping, :running, :cleaning]
2013-04-28 10:42:48 -04:00
2014-10-10 17:41:59 -04:00
job = Job.new
2016-05-14 18:14:10 -04:00
# show all permitted states (from initial state)
2019-03-07 16:56:19 -05:00
job.aasm.states(permitted: true).map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:running]
2020-02-08 07:49:30 -05:00
# List all the permitted transitions(event and state pairs) from initial state
job.aasm.permitted_transitions
#=> [{ :event => :run, :state => :running }]
2013-04-28 11:46:16 -04:00
job.run
2019-03-07 16:56:19 -05:00
job.aasm.states(permitted: true).map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:sleeping]
# show all non permitted states
2019-03-07 16:56:19 -05:00
job.aasm.states(permitted: false).map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:cleaning]
2013-04-28 11:46:16 -04:00
2016-05-14 18:14:10 -04:00
# show all possible (triggerable) events from the current state
2014-10-12 09:46:11 -04:00
job.aasm.events.map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:clean, :sleep]
# show all permitted events
2019-03-07 16:56:19 -05:00
job.aasm.events(permitted: true).map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:sleep]
# show all non permitted events
2019-03-07 16:56:19 -05:00
job.aasm.events(permitted: false).map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:clean]
# show all possible events except a specific one
2019-03-07 16:56:19 -05:00
job.aasm.events(reject: :sleep).map(& :name)
2016-05-14 18:14:10 -04:00
#=> [:clean]
2015-10-01 10:50:53 -04:00
# list states for select
Job.aasm.states_for_select
2019-03-07 16:56:19 -05:00
#=> [["Sleeping", "sleeping"], ["Running", "running"], ["Cleaning", "cleaning"]]
2016-08-01 10:45:39 -04:00
# show permitted states with guard parameter
2019-03-07 16:56:19 -05:00
job.aasm.states({permitted: true}, guard_parameter).map(& :name)
2013-04-28 10:42:48 -04:00
```
2016-06-19 01:37:11 -04:00
### Warning output
Warnings are by default printed to `STDERR` . If you want to log those warnings to another output,
use
```ruby
class Job
include AASM
2019-03-07 16:56:19 -05:00
aasm logger: Rails.logger do
2016-06-19 01:37:11 -04:00
...
end
end
```
2017-04-10 14:32:15 -04:00
You can hide warnings by setting `AASM::Configuration.hide_warnings = true`
2016-01-30 19:00:58 -05:00
### RubyMotion support
2016-03-18 10:43:40 -04:00
Now supports [CodeDataQuery ](https://github.com/infinitered/cdq.git ) !
However I'm still in the process of submitting my compatibility updates to their repository.
In the meantime you can use [my fork ](https://github.com/Infotaku/cdq.git ), there may still be some minor issues but I intend to extensively use it myself, so fixes should come fast.
2016-02-05 17:00:46 -05:00
Warnings:
- Due to RubyMotion Proc's lack of 'source_location' method, it may be harder
to find out the origin of a "cannot transition from" error. I would recommend using
the 'instance method symbol / string' way whenever possible when defining guardians and callbacks.
2016-02-02 21:22:28 -05:00
2016-01-30 19:00:58 -05:00
2015-10-27 04:39:52 -04:00
### Testing
2017-04-03 02:55:59 -04:00
#### RSpec
2019-03-07 16:56:19 -05:00
AASM provides some matchers for [RSpec ](http://rspec.info ):
2019-07-19 17:23:08 -04:00
* `transition_from` ,
2019-01-16 01:43:50 -05:00
* `have_state` , `allow_event`
2019-03-07 16:56:19 -05:00
* and `allow_transition_to` .
2019-01-16 01:43:50 -05:00
2019-03-07 16:56:19 -05:00
##### Installation Instructions:
2019-01-16 01:43:50 -05:00
* Add `require 'aasm/rspec'` to your `spec_helper.rb` file.
##### Examples Of Usage in Rspec:
2015-10-27 04:39:52 -04:00
2015-10-30 06:47:13 -04:00
```ruby
# classes with only the default state machine
job = Job.new
expect(job).to transition_from(:sleeping).to(:running).on_event(:run)
expect(job).not_to transition_from(:sleeping).to(:cleaning).on_event(:run)
2015-10-30 07:07:20 -04:00
expect(job).to have_state(:sleeping)
expect(job).not_to have_state(:running)
2015-10-30 07:19:09 -04:00
expect(job).to allow_event :run
expect(job).to_not allow_event :clean
2015-10-30 07:30:19 -04:00
expect(job).to allow_transition_to(:running)
expect(job).to_not allow_transition_to(:cleaning)
2019-01-25 06:04:17 -05:00
# on_event also accept multiple arguments
2016-01-31 11:01:47 -05:00
expect(job).to transition_from(:sleeping).to(:running).on_event(:run, :defragmentation)
2015-10-27 04:39:52 -04:00
2015-10-30 06:47:13 -04:00
# classes with multiple state machine
multiple = SimpleMultipleExample.new
expect(multiple).to transition_from(:standing).to(:walking).on_event(:walk).on(:move)
expect(multiple).to_not transition_from(:standing).to(:running).on_event(:walk).on(:move)
2015-10-30 07:07:20 -04:00
expect(multiple).to have_state(:standing).on(:move)
expect(multiple).not_to have_state(:walking).on(:move)
2015-10-30 07:19:09 -04:00
expect(multiple).to allow_event(:walk).on(:move)
expect(multiple).to_not allow_event(:hold).on(:move)
2015-10-30 07:30:19 -04:00
expect(multiple).to allow_transition_to(:walking).on(:move)
expect(multiple).to_not allow_transition_to(:running).on(:move)
2015-10-30 06:47:13 -04:00
expect(multiple).to transition_from(:sleeping).to(:processing).on_event(:start).on(:work)
expect(multiple).to_not transition_from(:sleeping).to(:sleeping).on_event(:start).on(:work)
2015-10-30 07:07:20 -04:00
expect(multiple).to have_state(:sleeping).on(:work)
expect(multiple).not_to have_state(:processing).on(:work)
2015-10-30 07:19:09 -04:00
expect(multiple).to allow_event(:start).on(:move)
expect(multiple).to_not allow_event(:stop).on(:move)
2015-10-30 07:30:19 -04:00
expect(multiple).to allow_transition_to(:processing).on(:move)
expect(multiple).to_not allow_transition_to(:sleeping).on(:move)
2016-11-27 09:13:49 -05:00
# allow_event also accepts arguments
expect(job).to allow_event(:run).with(:defragmentation)
2015-10-30 06:47:13 -04:00
```
2013-04-28 10:42:48 -04:00
2017-04-03 02:55:59 -04:00
#### Minitest
AASM provides assertions and rspec-like expectations for [Minitest ](https://github.com/seattlerb/minitest ).
##### Assertions
List of supported assertions: `assert_have_state` , `refute_have_state` , `assert_transitions_from` , `refute_transitions_from` , `assert_event_allowed` , `refute_event_allowed` , `assert_transition_to_allowed` , `refute_transition_to_allowed` .
2019-01-16 01:43:50 -05:00
##### Examples Of Usage (Minitest):
2018-04-18 11:10:08 -04:00
Add `require 'aasm/minitest'` to your `test_helper.rb` file and use them like this:
2017-04-03 02:55:59 -04:00
```ruby
# classes with only the default state machine
job = Job.new
assert_transitions_from job, :sleeping, to: :running, on_event: :run
refute_transitions_from job, :sleeping, to: :cleaning, on_event: :run
assert_have_state job, :sleeping
refute_have_state job, :running
assert_event_allowed job, :run
refute_event_allowed job, :clean
assert_transition_to_allowed job, :running
refute_transition_to_allowed job, :cleaning
# on_event also accept arguments
assert_transitions_from job, :sleeping, :defragmentation, to: :running, on_event: :run
# classes with multiple state machine
multiple = SimpleMultipleExample.new
assert_transitions_from multiple, :standing, to: :walking, on_event: :walk, on: :move
refute_transitions_from multiple, :standing, to: :running, on_event: :walk, on: :move
assert_have_state multiple, :standing, on: :move
refute_have_state multiple, :walking, on: :move
assert_event_allowed multiple, :walk, on: :move
refute_event_allowed multiple, :hold, on: :move
assert_transition_to_allowed multiple, :walking, on: :move
refute_transition_to_allowed multiple, :running, on: :move
assert_transitions_from multiple, :sleeping, to: :processing, on_event: :start, on: :work
refute_transitions_from multiple, :sleeping, to: :sleeping, on_event: :start, on: :work
assert_have_state multiple, :sleeping, on: :work
refute_have_state multiple, :processing, on: :work
assert_event_allowed multiple, :start, on: :move
refute_event_allowed multiple, :stop, on: :move
assert_transition_to_allowed multiple, :processing, on: :move
refute_transition_to_allowed multiple, :sleeping, on: :move
```
##### Expectations
List of supported expectations: `must_transition_from` , `wont_transition_from` , `must_have_state` , `wont_have_state` , `must_allow_event` , `wont_allow_event` , `must_allow_transition_to` , `wont_allow_transition_to` .
Add `require 'aasm/minitest_spec'` to your `test_helper.rb` file and use them like this:
```ruby
# classes with only the default state machine
job = Job.new
job.must_transition_from :sleeping, to: :running, on_event: :run
job.wont_transition_from :sleeping, to: :cleaning, on_event: :run
job.must_have_state :sleeping
job.wont_have_state :running
job.must_allow_event :run
job.wont_allow_event :clean
job.must_allow_transition_to :running
job.wont_allow_transition_to :cleaning
# on_event also accept arguments
job.must_transition_from :sleeping, :defragmentation, to: :running, on_event: :run
# classes with multiple state machine
multiple = SimpleMultipleExample.new
multiple.must_transition_from :standing, to: :walking, on_event: :walk, on: :move
multiple.wont_transition_from :standing, to: :running, on_event: :walk, on: :move
multiple.must_have_state :standing, on: :move
multiple.wont_have_state :walking, on: :move
multiple.must_allow_event :walk, on: :move
multiple.wont_allow_event :hold, on: :move
multiple.must_allow_transition_to :walking, on: :move
multiple.wont_allow_transition_to :running, on: :move
multiple.must_transition_from :sleeping, to: :processing, on_event: :start, on: :work
multiple.wont_transition_from :sleeping, to: :sleeping, on_event: :start, on: :work
multiple.must_have_state :sleeping, on: :work
multiple.wont_have_state :processing, on: :work
multiple.must_allow_event :start, on: :move
multiple.wont_allow_event :stop, on: :move
multiple.must_allow_transition_to :processing, on: :move
multiple.wont_allow_transition_to :sleeping, on: :move
```
2013-04-28 11:49:59 -04:00
## <a id="installation">Installation ##
2012-10-26 04:32:51 -04:00
### Manually from RubyGems.org ###
```sh
% gem install aasm
```
### Or if you are using Bundler ###
2012-01-16 11:27:15 -05:00
```ruby
2012-10-26 04:32:51 -04:00
# Gemfile
gem 'aasm'
2012-01-16 11:27:15 -05:00
```
2012-10-26 04:32:51 -04:00
### Building your own gems ###
2012-01-16 11:27:15 -05:00
2012-10-26 04:32:51 -04:00
```sh
% rake build
% sudo gem install pkg/aasm-x.y.z.gem
```
2012-01-16 11:27:15 -05:00
2015-09-28 04:11:33 -04:00
### Generators
2016-09-01 20:52:39 -04:00
After installing AASM you can run generator:
2015-09-28 04:11:33 -04:00
```sh
% rails generate aasm NAME [COLUMN_NAME]
```
2016-01-30 19:00:58 -05:00
Replace NAME with the Model name, COLUMN_NAME is optional(default is 'aasm_state').
2015-09-28 04:11:33 -04:00
This will create a model (if one does not exist) and configure it with aasm block.
For Active record orm a migration file is added to add aasm state column to table.
2018-02-13 03:08:28 -05:00
### Docker
Run test suite easily on docker
```
1. docker-compose build aasm
2. docker-compose run --rm aasm
```
2012-10-26 04:32:51 -04:00
## Latest changes ##
2011-09-04 11:59:55 -04:00
2013-11-30 18:35:45 -05: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.
2011-11-25 18:11:30 -05:00
2013-02-25 22:26:45 -05:00
## 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 )
2011-11-25 18:11:30 -05:00
2013-11-30 18:35:45 -05:00
## Maintainers ##
2011-11-25 18:11:30 -05:00
2013-11-30 18:35:45 -05:00
* [Scott Barron ](https://github.com/rubyist ) (2006– 2009, original author)
* [Travis Tilley ](https://github.com/ttilley ) (2009– 2011)
* [Thorsten Böttger ](http://github.com/alto ) (since 2011)
2016-08-22 05:06:05 -04:00
* [Anil Maurya ](http://github.com/anilmaurya ) (since 2016)
2008-03-04 18:59:58 -05:00
2017-03-08 13:11:56 -05:00
## [Contributing](CONTRIBUTING.md)
2015-06-23 05:08:50 -04:00
2011-08-31 15:50:59 -04:00
## Warranty ##
2008-03-04 18:59:58 -05:00
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.
2011-11-25 18:11:30 -05:00
## License ##
2017-08-15 06:46:46 -04:00
Copyright (c) 2006-2017 Scott Barron
2011-11-25 18:11:30 -05:00
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.