2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# email :string
|
|
|
|
# email_sender :string
|
|
|
|
# message :text
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# dossier_id :integer
|
|
|
|
# user_id :integer
|
|
|
|
#
|
2018-03-06 13:44:29 +01:00
|
|
|
class Invite < ApplicationRecord
|
2018-02-26 14:51:25 +01:00
|
|
|
include EmailSanitizableConcern
|
|
|
|
|
2020-07-20 16:56:23 +02:00
|
|
|
belongs_to :dossier, optional: false
|
2020-07-20 16:06:03 +02:00
|
|
|
belongs_to :user, optional: true
|
2022-06-13 16:00:41 +02:00
|
|
|
has_one :targeted_user_link, dependent: :destroy, inverse_of: :target_model, foreign_key: 'target_model_id'
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2018-02-26 14:51:25 +01:00
|
|
|
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 }
|
2016-02-08 18:16:18 +01:00
|
|
|
|
2018-02-26 14:51:25 +01:00
|
|
|
validates :email, format: { with: Devise.email_regexp, message: "n'est pas valide" }, allow_nil: true
|
2018-03-14 18:57:56 +01:00
|
|
|
|
2022-03-09 10:27:43 +01:00
|
|
|
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
|
2016-02-08 18:16:18 +01:00
|
|
|
end
|