* Move emails from users table to email_addresses table. * Validate that addresses are formatted correctly and are unique across users. Existing invalid emails are grandfathered in. * Add is_verified flag (the address has been confirmed by the user). * Add is_deliverable flag (an undeliverable address is an address that bounces). * Normalize addresses to prevent registering multiple accounts with the same email address (using tricks like Gmail's plus addressing).
16 lines
475 B
Ruby
16 lines
475 B
Ruby
class EmailAddress < ApplicationRecord
|
|
# https://www.regular-expressions.info/email.html
|
|
EMAIL_REGEX = /\A[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\z/
|
|
|
|
belongs_to :user
|
|
|
|
validates :address, presence: true, confirmation: true, format: { with: EMAIL_REGEX }
|
|
validates :normalized_address, uniqueness: true
|
|
validates :user_id, uniqueness: true
|
|
|
|
def address=(value)
|
|
self.normalized_address = EmailNormalizer.normalize(value) || address
|
|
super
|
|
end
|
|
end
|