Skip to content

Instantly share code, notes, and snippets.

View tuxayo's full-sized avatar
🕊️
GiveUpGithub(.org)

tuxayo tuxayo

🕊️
GiveUpGithub(.org)
View GitHub Profile
@tuxayo
tuxayo / refactor_start.rb
Last active August 29, 2015 14:03
Voilà à peu près mon point de départ
def change_password
@user = User.find(params[:id])
authorize! :update, @user
@user_input = params.require(:user)
if (@user.valid_password?(@user_input['old_password']))
if (@user_input['password'] == @user_input['password_confirmation'])
@user.password = @user_input['password']
save_successful = @user.save
@tuxayo
tuxayo / refactor_without_exceptions.rb
Last active August 29, 2015 14:03
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'
@tuxayo
tuxayo / refactor_exceptions.rb
Last active August 29, 2015 14:03
Ce que j'ai obtenu après avoir refactoré autant que je pouvais à l'aide d'exceptions pour supprimer les if imbriqués
def change_password
@user = get_and_authorize_user
@user_input = params.require(:user)
begin
check_old_password_match
check_new_password_confirmation_match
@user.password = @user_input['password']
save_user
redirect_to root_path, :notice => 'Mot de passe mis à jour'