Last active
August 29, 2015 14:03
Revisions
-
tuxayo revised this gist
Jul 9, 2014 . No changes.There are no files selected for viewing
-
tuxayo revised this gist
Jul 9, 2014 . No changes.There are no files selected for viewing
-
tuxayo revised this gist
Jul 9, 2014 . No changes.There are no files selected for viewing
-
tuxayo created this gist
Jul 3, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ 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' rescue PasswordExeception => exeception error_message_and_retry exeception.message end end private def get_and_authorize_user user = User.find(params[:id]) authorize! :update, user return user end def check_old_password_match if not @user.valid_password?(@user_input['old_password']) raise PasswordExeception, 'messages.users.invalid_old_password' end end def check_new_password_confirmation_match if not @user_input['password'] == @user_input['password_confirmation'] raise PasswordExeception, 'messages.users.password_dont_match' end end def save_user user_saved_successfully = @user.save if not user_saved_successfully raise PasswordExeception, 'activerecord.errors.models.user.attributes.password.too_short' end end def error_message_and_retry(error_message) flash[:error] = t(error_message) render :password end class PasswordExeception < Exception end