BigW Consortium Gitlab

email.rb 466 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 12 13 14 15 16 17 18
  before_validation :cleanup_email

  def cleanup_email
    self.email = self.email.downcase.strip
  end

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