Created
August 7, 2011 03:26
-
-
Save softwaregravy/1130026 to your computer and use it in GitHub Desktop.
Using a state machine will give you a messy update
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
def update | |
@registration = Registration.find(params[:id]) | |
redirect_to users_path(@registration.user_id) and return if @registration.state == 'complete' | |
if @registration.update_attributes(params[:registration]) | |
if @registration.next! | |
if @registration.state == 'complete' | |
#can only reach this block on first completion -- or next will have failed | |
sign_in(:user, @registration.user) | |
redirect_to user_show_path and return | |
else | |
redirect_to registration_state_path(@registration, @registration.state) and return | |
end | |
else | |
flash[:alert] = @registration.errors | |
render :template => get_template_for_state(@registration, @registration.state) and return | |
end | |
else | |
flash[:alert] = @registration.errors | |
respond_with(@registration, :location => registration_state_path(@registration, @registration.state)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment