demarches-normaliennes/app/models/invite.rb
2023-08-02 11:43:23 +02:00

28 lines
823 B
Ruby

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) }
after_create_commit :send_notification
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 }
def send_notification
if self.user.present?
InviteMailer.invite_user(self).deliver_later
else
InviteMailer.invite_guest(self).deliver_later
end
end
end