Skip to content

Instantly share code, notes, and snippets.

Avatar
😔
Wishing GitHub wasn't the center of the libre/open source world

tuxayo tuxayo

😔
Wishing GitHub wasn't the center of the libre/open source world
View GitHub Profile
@tuxayo
tuxayo / refactor_start.rb
Last active Aug 29, 2015
Voilà à peu près mon point de départ
View refactor_start.rb
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 Aug 29, 2015
Ce que j'ai obtenu après une première étape de refactoring
View refactor_without_exceptions.rb
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 Aug 29, 2015
Ce que j'ai obtenu après avoir refactoré autant que je pouvais à l'aide d'exceptions pour supprimer les if imbriqués
View refactor_exceptions.rb
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'