2013-01-19 13:52:55 -05:00
|
|
|
class ProjectTeam
|
2013-01-03 14:09:18 -05:00
|
|
|
attr_accessor :project
|
|
|
|
|
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2013-01-04 01:43:25 -05: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 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
2013-06-26 08:57:02 -04:00
|
|
|
def find(user_id)
|
2013-06-17 07:55:41 -04:00
|
|
|
user = project.users.find_by_id(user_id)
|
2013-06-17 08:38:19 -04:00
|
|
|
|
|
|
|
if group
|
|
|
|
user ||= group.users.find_by_id(user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
user
|
2013-06-17 07:55:41 -04:00
|
|
|
end
|
|
|
|
|
2013-06-26 08:57:02 -04:00
|
|
|
def find_tm(user_id)
|
2013-08-27 14:35:41 -04: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 12:45:13 -05:00
|
|
|
end
|
|
|
|
|
2013-01-03 14:09:18 -05: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 01:43:25 -05:00
|
|
|
def add_users_ids(user_ids, access)
|
2013-01-03 14:09:18 -05: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 05:57:05 -04:00
|
|
|
@members ||= fetch_members
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def guests
|
2013-06-17 07:17:32 -04:00
|
|
|
@guests ||= fetch_members(:guests)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reporters
|
2013-06-17 07:17:32 -04:00
|
|
|
@reporters ||= fetch_members(:reporters)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def developers
|
2013-06-17 07:17:32 -04:00
|
|
|
@developers ||= fetch_members(:developers)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def masters
|
2013-06-17 07:17:32 -04:00
|
|
|
@masters ||= fetch_members(:masters)
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|
2013-01-04 01:43:25 -05:00
|
|
|
|
|
|
|
def import(source_project)
|
|
|
|
target_project = project
|
|
|
|
|
2013-12-14 08:43:48 -05:00
|
|
|
source_team = source_project.users_projects.to_a
|
|
|
|
target_team = target_project.users_projects.to_a
|
2013-01-04 01:43:25 -05:00
|
|
|
target_user_ids = target_team.map(&:user_id)
|
|
|
|
|
|
|
|
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 07:17:32 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_members(level = nil)
|
|
|
|
project_members = project.users_projects
|
2013-06-17 08:38:19 -04:00
|
|
|
group_members = group ? group.users_groups : []
|
2013-06-17 07:17:32 -04:00
|
|
|
|
|
|
|
if level
|
|
|
|
project_members = project_members.send(level)
|
2013-06-17 08:38:19 -04:00
|
|
|
group_members = group_members.send(level) if group
|
2013-06-17 07:17:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
(project_members + group_members).map(&:user).uniq
|
|
|
|
end
|
2013-06-17 08:38:19 -04:00
|
|
|
|
|
|
|
def group
|
|
|
|
project.group
|
|
|
|
end
|
2013-01-03 14:09:18 -05:00
|
|
|
end
|