BigW Consortium Gitlab

email.rb 852 Bytes
Newer Older
1
class Email < ActiveRecord::Base
2 3
  include Sortable

4
  belongs_to :user
5

6
  validates :user_id, presence: true
7
  validates :email, presence: true, uniqueness: true, email: true
8
  validate :unique_email, if: ->(email) { email.email_changed? }
9

10 11
  scope :confirmed, -> { where.not(confirmed_at: nil) }

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

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

17 18
  def email=(value)
    write_attribute(:email, value.downcase.strip)
19 20 21 22 23
  end

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

25 26 27 28
  # once email is confirmed, update the gpg signatures
  def update_invalid_gpg_signatures
    user.update_invalid_gpg_signatures if confirmed?
  end
29
end