2017-04-27 02:47:36 +08:00
|
|
|
require_dependency 'email_validator'
|
|
|
|
|
|
|
|
class UserEmail < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
|
2017-08-09 10:56:08 +08:00
|
|
|
attr_accessor :should_validate_email
|
|
|
|
|
2017-04-27 02:47:36 +08:00
|
|
|
before_validation :strip_downcase_email
|
|
|
|
|
|
|
|
validates :email, presence: true, uniqueness: true
|
|
|
|
|
|
|
|
validates :email, email: true, format: { with: EmailValidator.email_regex },
|
2017-08-09 10:56:08 +08:00
|
|
|
if: :validate_email?
|
2017-04-27 02:47:36 +08:00
|
|
|
|
|
|
|
validates :primary, uniqueness: { scope: [:user_id] }
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def strip_downcase_email
|
|
|
|
if self.email
|
|
|
|
self.email = self.email.strip
|
|
|
|
self.email = self.email.downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-09 10:56:08 +08:00
|
|
|
def validate_email?
|
|
|
|
return false unless self.should_validate_email
|
2017-04-27 02:47:36 +08:00
|
|
|
email_changed?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_emails
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# email :string(513) not null
|
|
|
|
# primary :boolean default(FALSE), not null
|
2017-08-16 22:38:11 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-04-27 02:47:36 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2017-10-06 11:13:01 +08:00
|
|
|
# index_user_emails_on_email (lower((email)::text)) UNIQUE
|
2017-04-27 02:47:36 +08:00
|
|
|
# index_user_emails_on_user_id (user_id)
|
|
|
|
# index_user_emails_on_user_id_and_primary (user_id,primary) UNIQUE
|
|
|
|
#
|