Permalink
Browse files

Updated documentation

  • Loading branch information...
p8 committed Sep 14, 2010
1 parent 1c880d9 commit 9e44deaa7c9324ab5c5f300e6b4a2228f98fd308
Showing with 18 additions and 6 deletions.
  1. +7 −3 README.rdoc
  2. +2 −3 examples/conversation.rb
  3. +9 −0 spec/simple_state_machine_spec.rb
View
@@ -83,12 +83,14 @@ To add a state machine with ActiveRecord persistence:
class User < ActiveRecord::Base
extend SimpleStateMachine::ActiveRecord
-
+ # define a custum state_method (state is default)
+ state_machine_definition.state_method = :ssm_state
+
def after_initialize
- self.state ||= 'new'
+ self.ssm_state ||= 'new'
# if you get an ActiveRecord::MissingAttributeError
# you'll probably need to do (http://bit.ly/35q23b):
- # write_attribute(:state, "new") unless read_attribute(:state)
+ # write_attribute(:ssm_state, "new") unless read_attribute(:ssm_state)
end
def invite
@@ -104,6 +106,8 @@ To add a state machine with ActiveRecord persistence:
end
event :confirm_invitation, :invited => :active
+ # :all can be used to catch all from states
+ event :suspend, :all => :suspended
end
This generates the following methods
View
@@ -28,7 +28,6 @@ def initialize
end
event :view, :unread => :read
- event :close, :unread => :closed,
- :read => :closed
+ event :close, :all => :closed
-end
+end
@@ -111,6 +111,15 @@ def initialize(state = 'state1')
describe 'custom state method' do
+ it "changes state when calling events" do
+ example = SimpleExampleWithCustomStateMethod.new
+ example.should be_state1
+ example.event1
+ example.should be_state2
+ example.event2
+ example.should be_state3
+ end
+
it "raise an error if an invalid state_transition is called" do
example = SimpleExampleWithCustomStateMethod.new
lambda { example.event2 }.should raise_error(SimpleStateMachine::Error, "You cannot 'event2' when state is 'state1'")

0 comments on commit 9e44dea

Please sign in to comment.