forgeplus/app/models/team.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

2021-01-11 17:44:11 +08:00
# == Schema Information
#
# Table name: teams
#
# id :integer not null, primary key
# organization_id :integer
# name :string(255)
# description :string(255)
# authorize :integer default("0")
# num_projects :integer default("0")
# num_users :integer default("0")
# includes_all_project :boolean default("0")
# can_create_org_project :boolean default("0")
# gtid :integer
# created_at :datetime not null
# updated_at :datetime not null
2021-04-19 10:54:36 +08:00
# nickname :string(255)
2021-01-11 17:44:11 +08:00
#
# Indexes
#
# index_teams_on_organization_id (organization_id)
#
class Team < ApplicationRecord
belongs_to :organization
2021-02-02 14:21:02 +08:00
belongs_to :organization_extension, foreign_key: :organization_id, primary_key: :organization_id, counter_cache: :num_teams
2021-01-11 17:44:11 +08:00
has_many :team_projects, dependent: :destroy
has_many :team_units, dependent: :destroy
has_many :team_users, dependent: :destroy
2021-01-15 14:55:52 +08:00
validates :name, uniqueness: {scope: :organization_id}
2021-08-24 18:30:58 +08:00
enum authorize: {read: 1, write: 2, admin: 3, owner: 4}
2021-01-11 17:44:11 +08:00
2021-05-06 18:22:30 +08:00
def self.build(organization_id, name, nickname, description, authorize, includes_all_project, can_create_org_project)
2021-01-11 17:44:11 +08:00
self.create!(organization_id: organization_id,
name: name,
2021-05-06 18:22:30 +08:00
nickname: nickname,
2021-01-11 17:44:11 +08:00
description: description,
authorize: authorize,
includes_all_project: includes_all_project,
can_create_org_project: can_create_org_project)
end
2021-01-19 19:02:51 +08:00
def setup_team_project!
return unless includes_all_project
organization.projects.each do |project|
TeamProject.build(organization.id, id, project.id)
end
end
2021-02-01 14:50:56 +08:00
def is_member?(user_id)
team_users.where(user_id: user_id).present?
end
2021-09-15 18:55:58 +08:00
def authorize_name
case self.authorize
when 'read' then '报告者'
when 'write' then '开发者'
when 'admin' then '管理员'
2021-09-17 10:37:49 +08:00
when 'owner' then '拥有者'
2021-09-15 18:55:58 +08:00
else
''
end
end
2021-01-11 17:44:11 +08:00
end