1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/models/project_user.rb

36 lines
1.1 KiB
Ruby
Raw Normal View History

# 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
2015-05-05 03:10:25 +02:00
include NotifyWith::NotificationAttachedObject
belongs_to :project
belongs_to :user
before_create :generate_valid_token
2016-03-23 18:39:41 +01:00
after_commit :notify_project_collaborator_to_valid, on: :create
after_update :notify_project_author_when_collaborator_valid, if: :saved_change_to_is_valid?
2015-05-05 03:10:25 +02:00
private
2015-05-05 03:10:25 +02:00
def generate_valid_token
loop do
2015-05-05 03:10:25 +02:00
self.valid_token = SecureRandom.hex
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',
receiver: project.author,
attached_object: self
end
end