Jessica Fae (jessica_fae) wrote,
Jessica Fae
jessica_fae

Code Refactoring.

 First it was this:

def update
unless request.request_method == :post
redirect_to "/myaccount"
else
update_errors = false
pending_changes = false
@update_message = ""
if @client.first_name != params[:client][:first_name]
@client.first_name = params[:client][:first_name]
pending_changes = true
end
if @client.last_name != params[:client][:last_name]
@client.last_name = params[:client][:last_name]
pending_changes = true
end
if @client.phone != params[:client][:phone]
@client.phone = params[:client][:phone]
pending_changes = true
end
if @client.email != params[:client][:email]
if params[:client][:email] == params[:client][:email_confirmation]
@client.email = params[:client][:email]
pending_changes = true
else
update_errors = true
@update_message += "Email Address Does Not Match Confirmation.<br />"
end
end
if params[:client][:password] != ""
if params[:client][:password] == params[:client][:password_confirmation]
@client.set_password_to params[:client][:password]
unless @update_message == "Email Address Does Not Match Confirmation.<br />"
pending_changes = true
end
else
update_errors = true
@update_message += "Password Does Not Match Confirmation.<br />"
end
end
if pending_changes and !update_errors
@client.save
@update_message = "Updates Saved Successfully."
end
params[:client][:password] = nil
@client.password = nil
params[:client][:password_confirmation] = nil
@client.password_confirmation = nil
if !update_errors
render :action => "index"
else
render :action => "personal_info"
end
end
end


Now it is this:

def update
updating_user = User.find params[:id]
if @user.id == updating_user.id or @user.is_administrator?
updating_user.first_name = params[:user][:first_name]
updating_user.last_name = params[:user][:last_name]
updating_user.phone = params[:user][:phone]
updating_user.save!
if updating_user.valid?
redirect_to user_path(updating_user)
else
errmsg = ""
updating_user.errors.each do |key, msg|
errmsg += "#{msg}<br />"
end
@user_error_messages = errmsg
@editing_user = updating_user
render "edit"
end
else
redirect_to user_path(updating_user)
end
end

Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments