Skip to content

Instantly share code, notes, and snippets.

@dougfarre
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save dougfarre/690965f8b6bb12424a8a to your computer and use it in GitHub Desktop.

Select an option

Save dougfarre/690965f8b6bb12424a8a to your computer and use it in GitHub Desktop.
Business/Model Logic Inside State Machine?
class OrderStateMachine
include Statesman::Machine
# Possible states
state :entered, initial: true
state :validating
state :validated
state :invalidated
# Allowed transitions
transition from: :entered, to: [:validating]
transition from: :validating, to: [:validated, :invalidated]
# Events
event :validate do |model|
transition from: :entered, to: :validating
if model.valid?
transition from: :validating, to: :validated
else
transition from: :validation, to: :invalidated
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment