Commit 1c14805f authored by James Lopez's avatar James Lopez

refactor profiles controller and update service

parent 01378ab4
......@@ -12,16 +12,16 @@ class ProfilesController < Profiles::ApplicationController
user_params.except!(:email) if @user.external_email?
respond_to do |format|
status = Users::UpdateService.new(current_user, @user, user_params).execute
result = Users::UpdateService.new(current_user, @user, user_params).execute
if status[:success]
if result[: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
format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: status[:message] }) }
format.json { render json: status }
format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: result[:message] }) }
format.json { render json: result }
end
end
end
......@@ -63,12 +63,13 @@ class ProfilesController < Profiles::ApplicationController
end
def update_username
if @user.update_attributes(username: user_params[:username])
options = { notice: "Username successfully changed" }
else
message = @user.errors.full_messages.uniq.join('. ')
options = { alert: "Username change failed - #{message}" }
end
result = Users::UpdateService.new(current_user, @user, username: user_params[:username]).execute
options = if result[:success]
{ notice: "Username successfully changed" }
else
{ alert: "Username change failed - #{result[:message]}" }
end
redirect_back_or_default(default: { action: 'show' }, options: options)
end
......
......@@ -13,7 +13,7 @@ module Users
if @user.save || !@user.changed?
success
else
error("User could not be updated #{@user.errors.full_messages.uniq.join('. ')}" )
error(@user.errors.full_messages.uniq.join('. '))
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment