emails_controller.rb 1.25 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Profiles::EmailsController < Profiles::ApplicationController
4 5
  before_action :find_email, only: [:destroy, :resend_confirmation_instructions]

6
  def index
7
    @primary_email = current_user.email
8
    @emails = current_user.emails.order_id_desc
9 10 11
  end

  def create
James Lopez's avatar
James Lopez committed
12
    @email = Emails::CreateService.new(current_user, email_params.merge(user: current_user)).execute
13
    unless @email.errors.blank?
14 15
      flash[:alert] = @email.errors.full_messages.first
    end
16 17 18 19 20

    redirect_to profile_emails_url
  end

  def destroy
21
    Emails::DestroyService.new(current_user, user: current_user).execute(@email)
22

23
    respond_to do |format|
Lin Jen-Shin's avatar
Lin Jen-Shin committed
24
      format.html { redirect_to profile_emails_url, status: :found }
25
      format.js { head :ok }
26 27
    end
  end
28

29
  def resend_confirmation_instructions
30
    if Emails::ConfirmService.new(current_user, user: current_user).execute(@email)
31
      flash[:notice] = _("Confirmation email sent to %{email}") % { email: @email.email }
32
    else
33
      flash[:alert] = _("There was a problem sending the confirmation email")
34
    end
35

36 37
    redirect_to profile_emails_url
  end
Brett Walker's avatar
Brett Walker committed
38

39 40 41 42 43
  private

  def email_params
    params.require(:email).permit(:email)
  end
Brett Walker's avatar
Brett Walker committed
44

45 46 47
  def find_email
    @email = current_user.emails.find(params[:id])
  end
48
end