2014-02-08 22:08:49 -05:00
|
|
|
class Email < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
|
|
|
|
2014-02-08 22:08:49 -05:00
|
|
|
belongs_to :user
|
2014-04-02 14:02:02 -04:00
|
|
|
|
2014-02-08 22:08:49 -05:00
|
|
|
validates :user_id, presence: true
|
2016-02-09 11:59:47 -05:00
|
|
|
validates :email, presence: true, uniqueness: true, email: true
|
2014-02-08 22:08:49 -05:00
|
|
|
validate :unique_email, if: ->(email) { email.email_changed? }
|
2014-04-02 14:02:02 -04:00
|
|
|
|
2016-05-23 05:19:27 -04:00
|
|
|
def email=(value)
|
|
|
|
write_attribute(:email, value.downcase.strip)
|
2014-02-08 22:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unique_email
|
|
|
|
self.errors.add(:email, 'has already been taken') if User.exists?(email: self.email)
|
|
|
|
end
|
2014-04-02 14:02:02 -04:00
|
|
|
end
|