state_machine_history
state_machine_history is an extension of the state_machine gem, that logs transitions between states and allows to check if a state was visited earlier.
Install
gem install state_machine_history
Ruby On Rails 2.x
Add the gem dependency to environment.rb:
...
config.gem 'state_machine_history'
...
Generate the migration:
script/generate state_machine_history_2 create_machine_history
Run the migration
rake db:migrate
Ruby On Rails 3
In Gemfile:
gem 'state_machine_history'
Generate the migration:
rails generate state_machine_history create_machine_history
Run the migration:
rake db:migrate
Usage
This gem adds logging function to the state_machine gem. Sample:
Class definition
class Order
state_machine :initial => :not_selected do
# Switch on state machine logging
track_history
event :choose do
transition :not_selected => :selected
end
event :add_to_basket do
transition :selected => :in_basket
end
event :pay do
transition :in_basket => :paid
end
event :to_send do
transition :paid => :sent
end
end
end
Using extensions
order = Order.new
# Perform transition (state change is logged)
order.choose
# Find whether order has visited not_selected state before selected one
order.was_there?(:not_selected, :selected) #=> true
# Find whether order has visited not_selected state before the current one
order.was_there?(:not_selected) #=> true
Credits
Project Team
-
Mykhaylo Sorochan - Project Manager
-
Dmitriy Landberg - Software Developer
-
Nataliya Shatokhina - Tester
-
Sergey Mostovoy - Extension idea
Copyright © 2010 Sphere Consulting Inc., released under the MIT license (see LICENSE).