2014-09-02 18:07:02 +05:30
|
|
|
class Email < ActiveRecord::Base
|
2015-04-26 12:48:37 +05:30
|
|
|
include Sortable
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :user_id, presence: true
|
2016-04-02 18:10:28 +05:30
|
|
|
validates :email, presence: true, uniqueness: true, email: true
|
2014-09-02 18:07:02 +05:30
|
|
|
validate :unique_email, if: ->(email) { email.email_changed? }
|
|
|
|
|
|
|
|
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
|
|
|
|
end
|