2019-07-11 21:40:20 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class TeamRepository < ApplicationRecord
|
2020-07-13 18:56:26 +08:00
|
|
|
enum permission_level: Extends::SHARED_INVENTORIES_PERMISSION_LEVELS.except(:not_shared)
|
2019-07-11 21:40:20 +08:00
|
|
|
|
|
|
|
belongs_to :team
|
|
|
|
belongs_to :repository
|
|
|
|
|
2020-06-01 23:50:31 +08:00
|
|
|
before_destroy :unassign_unshared_items
|
|
|
|
|
2019-07-11 21:40:20 +08:00
|
|
|
validates :permission_level, presence: true
|
2019-07-15 23:17:26 +08:00
|
|
|
validates :repository, uniqueness: { scope: :team_id }
|
2019-08-29 23:17:38 +08:00
|
|
|
validate :team_cannot_be_the_same
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def team_cannot_be_the_same
|
|
|
|
errors.add(:team_id, :same_team) if repository&.team_id == team_id
|
|
|
|
end
|
2020-06-01 23:50:31 +08:00
|
|
|
|
|
|
|
def unassign_unshared_items
|
|
|
|
return if repository.shared_read? || repository.shared_write?
|
|
|
|
|
|
|
|
MyModuleRepositoryRow.joins(my_module: { experiment: { project: :team } })
|
|
|
|
.joins(repository_row: :repository)
|
|
|
|
.where(my_module: { experiment: { projects: { team: team } } })
|
|
|
|
.where(repository_rows: { repository: repository })
|
|
|
|
.destroy_all
|
|
|
|
end
|
2019-07-11 21:40:20 +08:00
|
|
|
end
|