-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
48 lines (27 loc) · 1.63 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
has_state
================
An informal library implementing various kinds of state machines: finite, event-driven, deterministic, nondeterministic.
States are kept in a states table and stateful models are associated to the State model polymorphically. The states table includes columns for the state of the associated model (state) and the datetime when the state occurred (recorded_at).
Example
=======
Get the plugin. Downloads to vendor/plugins/has_state
project dir> ./script/plugin install git://github.com/jolohaga/has_state.git
Run the generator. Produces app/models/state.rb and db/migrate/<serialnumber>_create_state.rb
project dir> ./scripts/generator has_state
Run the migration. Creates state table.
project dir> rake db:migrate
In the model intended to be stateful add the various commands. Example:
class Title < ActiveRecord::Base
# Add the polymorphic association to the State model.
has_state
# Define the initial state. This is the state entered when a new Title is saved.
state :submitted, :initial => true
# Define the method Title#submit! which will record the submitted state in the State table.
# Also defined is Title.submitted which finds all Titles in submitted state.
event :submit!, :to => :submitted, :from => []
# Define the method Title#adopt! which will record the adopted state in the State table.
event :adopt!, :to => :adopted, :from => [:submitted]
# Define the method Title#accept! which will record the accepted state in the State table.
event :accept!, :to => :accepted, :from => [:adopted]
end
Copyright (c) 2009 Jose Hales-Garcia, released under the MIT license