Last active
August 29, 2015 13:58
-
-
Save eliza-abraham/10118153 to your computer and use it in GitHub Desktop.
State Machines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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