2013-01-19 18:52:55 +00:00
|
|
|
class ProjectTeam
|
2013-01-03 19:09:18 +00:00
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2013-01-04 06:43:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut to add users
|
|
|
|
#
|
|
|
|
# Use:
|
|
|
|
# @team << [@user, :master]
|
|
|
|
# @team << [@users, :master]
|
|
|
|
#
|
|
|
|
def << args
|
|
|
|
users = args.first
|
|
|
|
|
|
|
|
if users.respond_to?(:each)
|
|
|
|
add_users(users, args.second)
|
|
|
|
else
|
|
|
|
add_user(users, args.second)
|
|
|
|
end
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
2013-06-26 12:57:02 +00:00
|
|
|
def find(user_id)
|
2013-06-17 11:55:41 +00:00
|
|
|
user = project.users.find_by_id(user_id)
|
2013-06-17 12:38:19 +00:00
|
|
|
|
|
|
|
if group
|
|
|
|
user ||= group.users.find_by_id(user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
2013-06-17 11:55:41 +00:00
|
|
|
end
|
|
|
|
|
2013-06-26 12:57:02 +00:00
|
|
|
def find_tm(user_id)
|
2013-08-27 18:35:41 +00:00
|
|
|
tm = project.users_projects.find_by_user_id(user_id)
|
|
|
|
|
|
|
|
# If user is not in project members
|
|
|
|
# we should check for group membership
|
|
|
|
if group && !tm
|
|
|
|
tm = group.users_groups.find_by_user_id(user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
tm
|
2013-01-22 17:45:13 +00:00
|
|
|
end
|
|
|
|
|
2013-01-03 19:09:18 +00:00
|
|
|
def add_user(user, access)
|
|
|
|
add_users_ids([user.id], access)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_users(users, access)
|
|
|
|
add_users_ids(users.map(&:id), access)
|
|
|
|
end
|
|
|
|
|
2013-01-04 06:43:25 +00:00
|
|
|
def add_users_ids(user_ids, access)
|
2013-01-03 19:09:18 +00:00
|
|
|
UsersProject.add_users_into_projects(
|
|
|
|
[project.id],
|
|
|
|
user_ids,
|
|
|
|
access
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove all users from project team
|
|
|
|
def truncate
|
|
|
|
UsersProject.truncate_team(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def members
|
2013-06-22 09:57:05 +00:00
|
|
|
@members ||= fetch_members
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def guests
|
2013-06-17 11:17:32 +00:00
|
|
|
@guests ||= fetch_members(:guests)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporters
|
2013-06-17 11:17:32 +00:00
|
|
|
@reporters ||= fetch_members(:reporters)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def developers
|
2013-06-17 11:17:32 +00:00
|
|
|
@developers ||= fetch_members(:developers)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def masters
|
2013-06-17 11:17:32 +00:00
|
|
|
@masters ||= fetch_members(:masters)
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|
2013-01-04 06:43:25 +00:00
|
|
|
|
|
|
|
def import(source_project)
|
|
|
|
target_project = project
|
|
|
|
|
2013-12-14 13:43:48 +00:00
|
|
|
source_team = source_project.users_projects.to_a
|
2013-12-17 18:37:56 +00:00
|
|
|
target_user_ids = target_project.users_projects.pluck(:user_id)
|
2013-01-04 06:43:25 +00:00
|
|
|
|
|
|
|
source_team.reject! do |tm|
|
|
|
|
# Skip if user already present in team
|
|
|
|
target_user_ids.include?(tm.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
source_team.map! do |tm|
|
|
|
|
new_tm = tm.dup
|
|
|
|
new_tm.id = nil
|
|
|
|
new_tm.project_id = target_project.id
|
|
|
|
new_tm
|
|
|
|
end
|
|
|
|
|
|
|
|
UsersProject.transaction do
|
|
|
|
source_team.each do |tm|
|
|
|
|
tm.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
2013-06-17 11:17:32 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_members(level = nil)
|
|
|
|
project_members = project.users_projects
|
2013-06-17 12:38:19 +00:00
|
|
|
group_members = group ? group.users_groups : []
|
2013-06-17 11:17:32 +00:00
|
|
|
|
|
|
|
if level
|
|
|
|
project_members = project_members.send(level)
|
2013-06-17 12:38:19 +00:00
|
|
|
group_members = group_members.send(level) if group
|
2013-06-17 11:17:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
(project_members + group_members).map(&:user).uniq
|
|
|
|
end
|
2013-06-17 12:38:19 +00:00
|
|
|
|
|
|
|
def group
|
|
|
|
project.group
|
|
|
|
end
|
2013-01-03 19:09:18 +00:00
|
|
|
end
|