2020-03-25 17:45:53 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# ProjectUser is the relation table between a Project and an User.
|
|
|
|
# Users are collaborators to a Project, with write access if they have confirmed their participation.
|
2020-03-25 10:16:47 +01:00
|
|
|
class ProjectUser < ApplicationRecord
|
2023-01-27 13:53:23 +01:00
|
|
|
include NotificationAttachedObject
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
before_create :generate_valid_token
|
2020-03-24 16:45:27 +01:00
|
|
|
after_update :notify_project_author_when_collaborator_valid, if: :saved_change_to_is_valid?
|
2023-01-27 13:53:23 +01:00
|
|
|
after_commit :notify_project_collaborator_to_valid, on: :create
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
private
|
2020-03-25 17:45:53 +01:00
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
def generate_valid_token
|
2020-03-25 17:45:53 +01:00
|
|
|
loop do
|
2015-05-05 03:10:25 +02:00
|
|
|
self.valid_token = SecureRandom.hex
|
2020-03-25 17:45:53 +01:00
|
|
|
break unless self.class.exists?(valid_token: valid_token)
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_project_collaborator_to_valid
|
|
|
|
NotificationCenter.call type: 'notify_project_collaborator_to_valid',
|
|
|
|
receiver: user,
|
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_project_author_when_collaborator_valid
|
|
|
|
NotificationCenter.call type: 'notify_project_author_when_collaborator_valid',
|
2021-04-06 14:16:03 +02:00
|
|
|
receiver: project.author.user,
|
2015-05-05 03:10:25 +02:00
|
|
|
attached_object: self
|
|
|
|
end
|
|
|
|
end
|