forked from Gitlink/forgeplus
60 lines
1.6 KiB
Ruby
60 lines
1.6 KiB
Ruby
# == Schema Information
|
|
#
|
|
# Table name: project_invite_links
|
|
#
|
|
# id :integer not null, primary key
|
|
# project_id :integer
|
|
# user_id :integer
|
|
# role :integer default("4")
|
|
# is_apply :boolean default("1")
|
|
# sign :string(255)
|
|
# expired_at :datetime
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_project_invite_links_on_project_id (project_id)
|
|
# index_project_invite_links_on_sign (sign)
|
|
# index_project_invite_links_on_user_id (user_id)
|
|
#
|
|
|
|
class ProjectInviteLink < ApplicationRecord
|
|
|
|
default_scope { where("expired_at > ?", Time.now).or(where(expired_at: nil)) }
|
|
|
|
belongs_to :project
|
|
belongs_to :user
|
|
has_many :applied_projects
|
|
|
|
scope :with_project_id, -> (project_id) {where(project_id: project_id)}
|
|
scope :with_user_id, -> (user_id) {where(user_id: user_id)}
|
|
|
|
enum role: {manager: 3, developer: 4, reporter: 5}
|
|
|
|
before_create :set_old_data_expired_at
|
|
|
|
def self.random_hex_sign
|
|
hex = (SecureRandom.hex(32))
|
|
return hex unless ProjectInviteLink.where(sign: hex).exists?
|
|
end
|
|
|
|
def self.build!(project, user, role="developer", is_apply=true)
|
|
self.create!(
|
|
project_id: project&.id,
|
|
user_id: user&.id,
|
|
role: role,
|
|
is_apply: is_apply,
|
|
sign: random_hex_sign,
|
|
expired_at: Time.now + 3.days
|
|
)
|
|
end
|
|
|
|
private
|
|
def set_old_data_expired_at
|
|
ProjectInviteLink.where(user_id: self.user_id, project_id: self.project, role: self.role, is_apply: self.is_apply).update_all(expired_at: Time.now)
|
|
end
|
|
|
|
|
|
end
|