Last active
December 8, 2016 20:24
-
-
Save nicolasblanco/a9a1b9c75b57756631793703bc66eda5 to your computer and use it in GitHub Desktop.
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
class WizardsController < ApplicationController | |
before_action :load_user_wizard, except: %i(validate_step) | |
def validate_step | |
current_step = params[:current_step] | |
@user_wizard = wizard_user_for_step(current_step) | |
@user_wizard.user.attributes = user_wizard_params | |
session[:user_attributes] = @user_wizard.user.attributes | |
if @user_wizard.valid? | |
next_step = wizard_user_next_step(current_step) | |
create and return unless next_step | |
redirect_to action: next_step | |
else | |
render current_step | |
end | |
end | |
def create | |
if @user_wizard.user.save | |
session[:user_attributes] = nil | |
redirect_to root_path, notice: 'User succesfully created!' | |
else | |
redirect_to({ action: Wizard::User::STEPS.first }, alert: 'There were a problem when creating the user.') | |
end | |
end | |
private | |
def load_user_wizard | |
@user_wizard = wizard_user_for_step(action_name) | |
end | |
def wizard_user_next_step(step) | |
Wizard::User::STEPS[Wizard::User::STEPS.index(step) + 1] | |
end | |
def wizard_user_for_step(step) | |
raise InvalidStep unless step.in?(Wizard::User::STEPS) | |
"Wizard::User::#{step.camelize}".constantize.new(session[:user_attributes]) | |
end | |
def user_wizard_params | |
params.require(:user_wizard).permit(:email, :first_name, :last_name, :address_1, :address_2, :zip_code, :city, :country, :phone_number) | |
end | |
class InvalidStep < StandardError; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment