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 ...@@ -12,16 +12,16 @@ class ProfilesController < Profiles::ApplicationController
user_params.except!(:email) if @user.external_email? user_params.except!(:email) if @user.external_email?
respond_to do |format| 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" message = "Profile was successfully updated"
format.html { redirect_back_or_default(default: { action: 'show' }, options: { notice: message }) } format.html { redirect_back_or_default(default: { action: 'show' }, options: { notice: message }) }
format.json { render json: { message: message } } format.json { render json: { message: message } }
else else
format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: status[:message] }) } format.html { redirect_back_or_default(default: { action: 'show' }, options: { alert: result[:message] }) }
format.json { render json: status } format.json { render json: result }
end end
end end
end end
...@@ -63,11 +63,12 @@ class ProfilesController < Profiles::ApplicationController ...@@ -63,11 +63,12 @@ class ProfilesController < Profiles::ApplicationController
end end
def update_username def update_username
if @user.update_attributes(username: user_params[:username]) result = Users::UpdateService.new(current_user, @user, username: user_params[:username]).execute
options = { notice: "Username successfully changed" }
options = if result[:success]
{ notice: "Username successfully changed" }
else else
message = @user.errors.full_messages.uniq.join('. ') { alert: "Username change failed - #{result[:message]}" }
options = { alert: "Username change failed - #{message}" }
end end
redirect_back_or_default(default: { action: 'show' }, options: options) redirect_back_or_default(default: { action: 'show' }, options: options)
......
...@@ -13,7 +13,7 @@ module Users ...@@ -13,7 +13,7 @@ module Users
if @user.save || !@user.changed? if @user.save || !@user.changed?
success success
else else
error("User could not be updated #{@user.errors.full_messages.uniq.join('. ')}" ) error(@user.errors.full_messages.uniq.join('. '))
end end
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