Skip to content

Commit

Permalink
Extracting method to better define method
Browse files Browse the repository at this point in the history
This also involved removing a method that was dependent on a guard
condition from another method (`params[:user][:update_directory]`) was
built on the assumption that `params[:user]` existed (which was guarded
in the parent method).
  • Loading branch information
jeremyf committed Mar 30, 2017
1 parent 976f6b4 commit 805ee38
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions app/controllers/hyrax/dashboard/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,36 @@ def edit

# Process changes from profile form
def update
if params[:user]
@user.attributes = user_params
@user.populate_attributes if update_directory?
end
conditionally_set_user_attributes

unless @user.save
if @user.save
handle_successful_update
redirect_to hyrax.dashboard_profile_path(@user.to_param), notice: "Your profile has been updated"
else
redirect_to hyrax.edit_dashboard_profile_path(@user.to_param), alert: @user.errors.full_messages
return
end
# TODO: this should be moved to TrophiesController
params.keys.select { |k, _v| k.starts_with? 'remove_trophy_' }.each do |smash_trophy|
smash_trophy = smash_trophy.sub(/^remove_trophy_/, '')
current_user.trophies.where(work_id: smash_trophy).destroy_all
end
UserEditProfileEventJob.perform_later(@user)
redirect_to hyrax.dashboard_profile_path(@user.to_param), notice: "Your profile has been updated"
end

def update_directory?
['1', 'true'].include? params[:user][:update_directory]
end

private

def conditionally_set_user_attributes
return true unless params[:user]
@user.attributes = user_params
case params[:user][:update_directory]
when '1', 'true', true
@user.populate_attributes
end
end

def handle_successful_update
# TODO: this should be moved to TrophiesController
params.keys.select { |k, _v| k.starts_with? 'remove_trophy_' }.each do |smash_trophy|
smash_trophy = smash_trophy.sub(/^remove_trophy_/, '')
current_user.trophies.where(work_id: smash_trophy).destroy_all
end
UserEditProfileEventJob.perform_later(@user)
end

def user_params
params.require(:user).permit(:avatar, :facebook_handle, :twitter_handle,
:googleplus_handle, :linkedin_handle, :remove_avatar, :orcid)
Expand Down

0 comments on commit 805ee38

Please sign in to comment.