-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathemails_controller.rb
60 lines (50 loc) · 1.35 KB
/
emails_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true
class User::EmailsController < ApplicationController
load_and_authorize_resource :email, through: :current_user, class: 'User::Email'
def index
end
def create
if @email.save
render_emails
else
render json: { errors: @email.errors }, status: :bad_request
end
end
def destroy
if @email.primary?
@email.errors.add(:base, I18n.t('user.emails.index.cannot_delete_primary'))
render json: { errors: @email.errors.full_messages.to_sentence }, status: :bad_request
return
end
if @email.destroy
render_emails
else
render json: { errors: @email.errors.full_messages.to_sentence }, status: :bad_request
end
end
# Set an email as the primary email
def set_primary
current_user.email = @email.email
if current_user.save
render_emails
else
render json: { errors: @email.errors.full_messages.to_sentence }, status: :bad_request
end
end
def send_confirmation
if @email.confirmed?
render json: { errors: t('.already_confirmed', email: @email.email) }, status: :bad_request
else
@email.send_confirmation_instructions
head :ok
end
end
private
def render_emails
@emails = current_user.reload.emails
render 'index'
end
def email_params
params.require(:user_email).permit(:email)
end
end