diff options
-rw-r--r-- | app/controllers/profiles_controller.rb | 28 | ||||
-rw-r--r-- | app/services/users/update_service.rb | 6 |
2 files changed, 21 insertions, 13 deletions
diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index f98a9e24de1..09161daa7ed 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -12,39 +12,47 @@ class ProfilesController < Profiles::ApplicationController user_params.except!(:email) if @user.external_email? respond_to do |format| - if @user.update_attributes(user_params) + status = Users::UpdateService.new(current_user, @user, user_params).execute + + if status[:success] message = "Profile was successfully updated" + format.html { redirect_back_or_default(default: { action: 'show' }, options: { notice: message }) } format.json { render json: { message: message } } else - message = @user.errors.full_messages.uniq.join('. ') - format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: "Failed to update profile. #{message}" }) } - format.json { render json: { message: message }, status: :unprocessable_entity } + format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: status[:message] }) } + format.json { render json: status } end end end def reset_private_token - if current_user.reset_authentication_token! - flash[:notice] = "Private token was successfully reset" + Users::UpdateService.new(current_user, @user).execute!(skip_authorization: true) do |user| + user.reset_authentication_token! end + flash[:notice] = "Private token was successfully reset" + redirect_to profile_account_path end def reset_incoming_email_token - if current_user.reset_incoming_email_token! - flash[:notice] = "Incoming email token was successfully reset" + Users::UpdateService.new(current_user, @user).execute!(skip_authorization: true) do |user| + user.reset_incoming_email_token! end + flash[:notice] = "Incoming email token was successfully reset" + redirect_to profile_account_path end def reset_rss_token - if current_user.reset_rss_token! - flash[:notice] = "RSS token was successfully reset" + Users::UpdateService.new(current_user, @user).execute!(skip_authorization: true) do |user| + user.reset_rss_token! end + flash[:notice] = "RSS token was successfully reset" + redirect_to profile_account_path end diff --git a/app/services/users/update_service.rb b/app/services/users/update_service.rb index 866eb070913..977124298bf 100644 --- a/app/services/users/update_service.rb +++ b/app/services/users/update_service.rb @@ -10,17 +10,17 @@ module Users def execute(skip_authorization: false, &block) assign_attributes(skip_authorization, &block) - if @user.save + if @user.save || !@user.changed? success else - error('User could not be updated') + error("User could not be updated #{@user.errors.full_messages.uniq.join('. ')}" ) end end def execute!(skip_authorization: false, &block) assign_attributes(skip_authorization, &block) - @user.save! + @user.save! if @user.changed? end private |