Read about our upcoming Code of Conduct on this issue

email.rb 1010 Bytes
Newer Older
1
2
# frozen_string_literal: true

3
class Email < ApplicationRecord
4
  include Sortable
5
  include Gitlab::SQL::Pattern
6

Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
7
  belongs_to :user, optional: false
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
8

9
  validates :email, presence: true, uniqueness: true, devise_email: true
10
  validate :unique_email, if: ->(email) { email.email_changed? }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11

12
13
  scope :confirmed, -> { where.not(confirmed_at: nil) }

14
15
  after_commit :update_invalid_gpg_signatures, if: -> { previous_changes.key?('confirmed_at') }

16
  devise :confirmable
17
  self.reconfirmable = false # currently email can't be changed, no need to reconfirm
18

19
20
  delegate :username, to: :user

21
22
  def email=(value)
    write_attribute(:email, value.downcase.strip)
23
24
25
26
27
  end

  def unique_email
    self.errors.add(:email, 'has already been taken') if User.exists?(email: self.email)
  end
28

29
30
31
32
  def accept_pending_invitations!
    user.accept_pending_invitations!
  end

33
34
35
36
  # once email is confirmed, update the gpg signatures
  def update_invalid_gpg_signatures
    user.update_invalid_gpg_signatures if confirmed?
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37
end