Created
June 7, 2010 19:14
-
-
Save mikew/429048 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
- form_for @user do |f| | |
= f.hidden_field :signup_step, :value => @user.signup_step | |
= render :partial => "step_#{@user.signup_step}", :locals => { :f => f } | |
= f.submit |
This file contains hidden or 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 User < ActiveRecord::Base | |
attr_accessor :signup_step | |
validates_presence_of :first_name, :last_name, :billing_address | |
end |
This file contains hidden or 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 RegistrationsController < ApplicationController | |
STEP_VALIDATIONS = [ | |
%w(first_name last_name), | |
%w(billing_address) | |
] | |
def new | |
@user = User.new | |
end | |
def create | |
@user = user_from_session | |
increment_signup_step @user, params[:reg] | |
respond_to do |format| | |
if @user.signup_step == STEP_VALIDATIONS.length && @user.save | |
format.any { render :final, :status => 202 } | |
else | |
format.html { render :new } | |
format.any(:js, :json) { render :json => @user.errors, :status => @user.errors.empty? ? 202 : 400 } | |
end | |
end | |
end | |
private | |
def increment_signup_step(model, params) | |
model.attributes = params | |
model.signup_step += 1 if valid_for_attributes? model, *STEP_VALIDATIONS[model.signup_step] | |
end | |
def valid_for_attributes?(model, *attrs) | |
return true if model.valid? | |
errors = model.errors.dup | |
model.errors.clear | |
errors.each do |attr, message| | |
model.errors.add(attr, message) if attrs.include? attr | |
end | |
return model.errors.empty? | |
end | |
def user_from_session | |
session[:temp_user] ||= User.new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment