demarches-normaliennes/app/models/invite.rb

29 lines
823 B
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class Invite < ApplicationRecord
include EmailSanitizableConcern
belongs_to :dossier, optional: false
belongs_to :user, optional: true
has_one :targeted_user_link, as: :target_model, dependent: :destroy, inverse_of: :target_model
before_validation -> { sanitize_email(:email) }
2020-09-28 17:38:49 +02:00
after_create_commit :send_notification
2020-09-25 11:10:12 +02:00
2018-03-06 13:44:29 +01:00
validates :email, presence: true
validates :email, uniqueness: { scope: :dossier_id }
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
scope :with_dossiers, -> { joins(:dossier).merge(Dossier.visible_by_user) }
default_scope { with_dossiers }
2020-09-25 11:10:12 +02:00
def send_notification
if self.user.present?
InviteMailer.invite_user(self).deliver_later
else
InviteMailer.invite_guest(self).deliver_later
end
end
end