email.rb 702 字节
Newer Older
1 2 3 4
# == Schema Information
#
# Table name: emails
#
D
Dmitriy Zaporozhets 已提交
5 6
#  id         :integer          not null, primary key
#  user_id    :integer          not null
Z
Zeger-Jan van de Weg 已提交
7
#  email      :string           not null
D
Dmitriy Zaporozhets 已提交
8 9 10 11
#  created_at :datetime
#  updated_at :datetime
#

12
class Email < ActiveRecord::Base
13 14
  include Sortable

15
  belongs_to :user
D
Dmitriy Zaporozhets 已提交
16

17
  validates :user_id, presence: true
18
  validates :email, presence: true, uniqueness: true, email: true
19
  validate :unique_email, if: ->(email) { email.email_changed? }
D
Dmitriy Zaporozhets 已提交
20

21 22 23 24 25 26 27 28 29
  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
D
Dmitriy Zaporozhets 已提交
30
end