Skip to content

Instantly share code, notes, and snippets.

@tuxayo
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxayo/5e398af926e40cd314b0 to your computer and use it in GitHub Desktop.
Save tuxayo/5e398af926e40cd314b0 to your computer and use it in GitHub Desktop.
Ce que j'ai obtenu après une première étape de refactoring
def change_password
@user = get_and_authorize_user
@user_input = params.require(:user)
if (old_password_match)
if (new_password_confirmation_match)
@user.password = @user_input['password']
save_user_and_display_eventual_error
else
error_message_and_retry 'messages.users.password_dont_match'
end
else
error_message_and_retry 'messages.users.invalid_old_password'
end
end
private
def get_and_authorize_user
user = User.find(params[:id])
authorize! :update, user
return user
end
def error_message_and_retry(error_message)
flash[:error] = t(error_message)
render :password
end
def save_user_and_display_eventual_error
user_saved_successfully = @user.save
if user_saved_successfully
redirect_to root_path, :notice => 'Mot de passe mis à jour'
else
error_message_and_retry 'activerecord.errors.models.user.attributes.password.too_short'
end
end
def old_password_match
@user.valid_password?(@user_input['old_password'])
end
def new_password_confirmation_match
@user_input['password'] == @user_input['password_confirmation']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment