2014-02-08 22:08:49 -05:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: emails
|
|
|
|
#
|
2014-04-09 08:05:03 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# email :string(255) not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
|
2014-02-08 22:08:49 -05:00
|
|
|
class Email < ActiveRecord::Base
|
|
|
|
attr_accessible :email, :user_id
|
|
|
|
|
|
|
|
#
|
|
|
|
# Relations
|
|
|
|
#
|
|
|
|
belongs_to :user
|
2014-04-02 14:02:02 -04:00
|
|
|
|
2014-02-08 22:08:49 -05:00
|
|
|
#
|
|
|
|
# Validations
|
|
|
|
#
|
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :email, presence: true, email: { strict_mode: true }, uniqueness: true
|
|
|
|
validate :unique_email, if: ->(email) { email.email_changed? }
|
2014-04-02 14:02:02 -04:00
|
|
|
|
|
|
|
after_create :notify
|
2014-02-08 22:08:49 -05:00
|
|
|
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
|
2014-04-02 14:02:02 -04:00
|
|
|
|
|
|
|
def notify
|
|
|
|
NotificationService.new.new_email(self)
|
|
|
|
end
|
|
|
|
end
|