Acts as state machine (aasm)
A finite-state machine (FSM) is based on the concept of an object being in a particular state and then transitioning to other states based on certain events. Let's take the example of reserving a book. The states that the reservation can be in are available
, booked
, and late
. For this example, the reservation can transition to these states in any of the following ways:
All books have a single reservation each
The default status of a reservation is
available
A customer can reserve a book if it's
available
A customer can return a book if it's
booked
or it'slate
A book is delayed if it's reserved for more than the default seven days
To use the state machine, we need to add the aasm
gem into Gemfile
as follows:
class Reservation include Mongoid::Document include AASM field :aasm_state, type: String field :booked_on, type: Date field booked_by, type: String field :returned_on, type: Date field :days_reserved, type: Integer, default: 7 belongs_to :book...