18.12.2012 Views

Rails Magazine - Issue 3

Rails Magazine - Issue 3

Rails Magazine - Issue 3

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

22<br />

22<br />

Workflow Definition<br />

A workflow is an abstract representation of real work<br />

undertaken by a single or complex group of individuals<br />

or mechanisms. It describes a system of activities enabled<br />

through a collection of resources and information flows.<br />

Workflows are primarily used to achieve some form of processing<br />

intentions, such as data processing.<br />

There are two main forms of workflows: sequential and<br />

state-machine. Sequential workflows are predictable. They<br />

utilize the rules and conditions we provide at the beginning<br />

to progress a process forward. The workflow is in control of<br />

the process.<br />

A state-machine workflow is the reverse. It is driven by<br />

external events to its completion. We define the states and<br />

required transitions between those states. The workflow sits<br />

and waits for an external event to occur before transitioning<br />

to one of the defined states. The decision making process happens<br />

externally outside of the workflow – there is a structure<br />

to be followed still like a sequential workflow but control is<br />

passed to its external environment.<br />

What is a state machine?<br />

The diagram below is a simple example of a state machine.<br />

It has three main properties: states, events and transitions.<br />

Event<br />

Workflow solutions using AASM by Chee Yeo<br />

Workflow solutions using AASM<br />

by Chee Yeo<br />

A state represents a particular situation. The state machine<br />

above has two main states – ‘Power On’ and ‘Power Off ’. The<br />

machine will be in one of these two states at any point in<br />

time.<br />

There is only one main event in the state machine above<br />

– a button click. An event describes an external stimulus or<br />

input into the system. In this instance, a button click will<br />

either ‘power on’ the state machine or power it off and both<br />

states shown above respond to the same event.<br />

A transition moves the state machine from one state to<br />

the next. In the diagram above, following a button click, if<br />

the machine is in a ‘power on’ state it will transition to the<br />

‘power off ’ state and vice versa. Not all transitions move the<br />

state machine to a new state – it is possible to loop back to the<br />

same state.<br />

A state machine does not just store states and events<br />

alone. It can also execute code when an event arrives. In the<br />

example above, the state machine can be designed to control<br />

electricity flow when it transitions to a new state following a<br />

button click event.<br />

State Machine in <strong>Rails</strong><br />

Sometimes you need to track the state of a model in an<br />

application and the transitions between a set of pre-defined<br />

states which are triggered based on certain events. Or you<br />

might need to express the business logic of your application<br />

using a workflow to illustrate the flow of state changes based<br />

on its events.<br />

For example, you might have an application<br />

that allows the user to upload files<br />

for processing and you need to provide<br />

some form of mechanism to track the file<br />

while it is added to a queue for processing<br />

and to provide some form of feedback to<br />

inform the user of the status of the outcome<br />

(success or an error) and the relevant<br />

action to take based on the outcome (if an<br />

error occurred, inform the user).<br />

This can be modelled by adding a ‘state’<br />

or ‘status’ column in your model table and<br />

updating it as you go along. However, it<br />

becomes tedious and error prone when<br />

multiple states are involved.<br />

The acts_as_state_machine (AASM) plugin converts any<br />

model into a Finite State Machine capable of remembering<br />

which state it is in at any point in time and the transitions it<br />

is allowed to go through between those states and the actions<br />

that are to be triggered when a specific state is reached.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!