4291e28af7
Allow primary email to be set to an email that you've already added. Fixes gitlab-com/support-forum#106. When the user sets their primary email to an email that they've already added to their account, this patch makes sure that secondary email record is destroyed, and a new email record is created for the old primary email. This is based on the assumption that in this case no email was meant to be deleted, but the user simply wanted to change which of their emails is primary. See merge request !591
36 lines
748 B
Ruby
36 lines
748 B
Ruby
class Profiles::EmailsController < Profiles::ApplicationController
|
|
def index
|
|
@primary = current_user.email
|
|
@emails = current_user.emails
|
|
end
|
|
|
|
def create
|
|
@email = current_user.emails.new(email_params)
|
|
|
|
if @email.save
|
|
NotificationService.new.new_email(@email)
|
|
else
|
|
flash[:alert] = @email.errors.full_messages.first
|
|
end
|
|
|
|
redirect_to profile_emails_url
|
|
end
|
|
|
|
def destroy
|
|
@email = current_user.emails.find(params[:id])
|
|
@email.destroy
|
|
|
|
current_user.update_secondary_emails!
|
|
|
|
respond_to do |format|
|
|
format.html { redirect_to profile_emails_url }
|
|
format.js { render nothing: true }
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def email_params
|
|
params.require(:email).permit(:email)
|
|
end
|
|
end
|