Skip to content

Instantly share code, notes, and snippets.

@joemsak
Created August 17, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemsak/871cd16d04200ab6c16cfa95a52954e0 to your computer and use it in GitHub Desktop.
Save joemsak/871cd16d04200ab6c16cfa95a52954e0 to your computer and use it in GitHub Desktop.
app/controllers/concerns/profile_controller.rb
def update
#
# TODO: explore why location details errors appear order-dependent
# and unable to run in the error path
#
if setting_location_without_location_details?
handle_blank_location_error
elsif ProfileUpdating.execute(profile, permitted_params)
handle_successful_profile_update
else
handle_profile_errors
end
end
private
def setting_location_without_location_details?
!!params[:setting_location] &&
permitted_params[:account_attributes][:country].blank? &&
permitted_params[:account_attributes][:latitude].blank?
end
def handle_blank_location_error
profile.account.errors.add(:country, :blank)
render "location_details/show"
end
def handle_successful_profile_update
respond_to do |format|
format.json {
render json: {
flash: {
success: t("controllers.accounts.update.success"),
},
},
status: 200
}
format.html {
redirect_to after_update_path,
success: t('controllers.accounts.update.success')
}
end
end
def handle_profile_errors
#
# TODO: at next related change request,
# consider the Open-Closed Principle
# for factory-created objects that can
# handle_profile_errors
#
if parent_guardian_errors?
render "student/parental_consent_notices/new"
elsif password_errors?
handle_password_errors
elsif email_errors?
render 'email_addresses/edit'
elsif address_errors?
render "location_details/show"
else
render :edit
end
end
def parent_guardian_errors?
profile.errors["parent_guardian_email"].any?
end
def password_errors?
profile.errors["account.password"].any? ||
profile.errors["account.existing_password"].any?
end
def email_errors?
profile.errors["account.email"].any?
end
def address_errors?
profile.errors["account.city"].any? ||
profile.errors["account.state_province"].any? ||
profile.errors["account.country"].any?
end
def handle_password_errors
if profile.account.email_changed?
render 'email_addresses/edit'
else
render "passwords/edit"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment