summaryrefslogtreecommitdiff
path: root/app/models/email.rb
blob: 826d4f16edb4a5d12b4fb198179196cde40697db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Email < ActiveRecord::Base
  include Sortable

  belongs_to :user

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

  def email=(value)
    write_attribute(:email, value.downcase.strip)
  end

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