Skip to content

Instantly share code, notes, and snippets.

@eliza-abraham
Last active August 29, 2015 13:58
Show Gist options
  • Save eliza-abraham/10118153 to your computer and use it in GitHub Desktop.
Save eliza-abraham/10118153 to your computer and use it in GitHub Desktop.
State Machines
# To get parameters from your controller inside your transition callback
# transition : cannot be changed to a different variable name. It should always be transition
# my_model : will always have the current object of the model class in which your state machine is defined
# can add any variable name
# params : Actual parameters are stored in transition.args which is an array of parameters passed
class MyModel
def update_my_model
@object.select(params) # calls the select event from state machine
end
state_machine :initial => :started do
before_transition to: :selected do |my_model,transition|
params = transition.args.first
# do something with params
# do something with my_model object
my_model.column = params[:column]
# return false if you do not want the transition to complete based on condition
return false unless my_model.save
end
event :select do
transition :started => :selected
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment