Project

General

Profile

Feature #44601 » 0001-Show-the-first-members-of-each-role-with-avatars-and.patch

Marius BĂLTEANU, 2026-10-12 23:31

View differences:

app/assets/stylesheets/application.css
td.center {text-align:center;}
.members.box .avatar {margin-block: 0 2px; margin-inline: 0 4px;}
.members.box .badge {inset-block-end: 0;}
.members.box a.badge-count, .members.box a.badge-count:hover {color: var(--oc-white); text-decoration: none;}
#watchers select {inline-size: 95%; display: block;}
#watchers .avatar {margin-block: 0 2px; margin-inline: 0 4px;}
#watchers svg.icon-svg {margin-block: 0 2px; margin-inline: 0 2px;}
app/controllers/projects_controller.rb
respond_to do |format|
format.html do
@principals_by_role = @project.principals_by_role
@roles_with_principals = @project.roles_with_principals
@subprojects = @project.leaf? ? [] : @project.children.visible.to_a
@news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").to_a
with_subprojects = Setting.display_subprojects_issues?
app/helpers/application_helper.rb
end
css_classes += " #{options[:class]}" if css_classes && options[:class].present?
url ? link_to(principal_icon(principal).to_s + name, url, :class => css_classes) : h(name)
return h(name) unless url
icon =
if options[:avatar]
avatar(principal, :size => (options[:avatar] == true ? 16 : options[:avatar]))
else
principal_icon(principal)
end
link_to(safe_join([icon, name].compact), url, :class => css_classes)
end
def link_to_mention(user, object, options={})
app/helpers/projects_helper.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module ProjectsHelper
# Returns a badge with the number of members of the role that are not listed
# in the project members box (ie. beyond +displayed_count+ out of
# +total_count+), linked to the members settings tab filtered on that role
# for the users allowed to manage the members of the project. Returns nil if
# every member of the role is already displayed.
def members_not_displayed_badge(project, role, total_count, displayed_count)
remaining_count = total_count - displayed_count
return if remaining_count <= 0
options = {:class => 'badge badge-count', :title => l(:label_x_more_members, :count => remaining_count)}
if User.current.allowed_to?(:manage_members, project)
link_to("+#{remaining_count}", settings_project_path(project, :tab => 'members', :member_role_id => role.id), options)
else
content_tag('span', "+#{remaining_count}", options)
end
end
def project_settings_tabs
tabs =
[
app/models/project.rb
end
end
# Returns an array of [Role, principals, count] for the active members of the
# project, ordered by role position. Only the first +limit+ principals are
# returned for each role, count being the total number of members with the role.
def roles_with_principals(limit: 10)
# One row per member and role: enough to count the members of each role and
# to pick the first ones of each, without loading every member of the project
role_and_user_ids =
memberships.active.joins(:member_roles).
reorder(Principal.fields_for_order_statement).
pluck("#{MemberRole.table_name}.role_id", "#{Member.table_name}.user_id").uniq
return [] if role_and_user_ids.empty?
user_ids_by_role_id = role_and_user_ids.group_by(&:first).transform_values {|rows| rows.map(&:last)}
principals =
Principal.where(:id => user_ids_by_role_id.values.flat_map {|ids| ids.first(limit)}).index_by(&:id)
Role.where(:id => user_ids_by_role_id.keys).sorted.map do |role|
user_ids = user_ids_by_role_id[role.id]
[role, user_ids.first(limit).filter_map {|user_id| principals[user_id]}, user_ids.size]
end
end
# Adds user as a project member with the default role
# Used for when a non-admin user creates a project
def add_default_member(user)
app/views/projects/_members_box.html.erb
<% if @principals_by_role.any? %>
<% if @roles_with_principals.any? %>
<div class="members box">
<h3 class="icon icon-group"><%= sprite_icon('group', l(:label_member_plural)) %></h3>
<% @principals_by_role.keys.sort.each do |role| %>
<p><span class="label"><%= role %>:</span> <%= @principals_by_role[role].sort.collect{|p| link_to_principal(p, :class => p.is_a?(Group) ? 'icon icon-group' : nil)}.join(", ").html_safe %></p>
<% @roles_with_principals.each do |role, principals, count| %>
<p>
<span class="label"><%= role %>:</span>
<%= safe_join(principals.collect {|principal| link_to_principal(principal, :avatar => true)}, ', ') %>
<%= members_not_displayed_badge(@project, role, count, principals.size) %>
</p>
<% end %>
<% if User.current.allowed_to?(:view_members, @project) %>
<p><%= link_to l(:label_member_view_all), project_members_path(@project) %></p>
config/locales/en.yml
label_member_new: New member
label_member_plural: Members
label_member_view_all: View all members
label_x_more_members:
one: 1 more member
other: "%{count} more members"
label_tracker: Tracker
label_tracker_plural: Trackers
label_tracker_all: All trackers
test/functional/projects_controller_test.rb
assert_select '#main.nosidebar'
end
def test_show_should_display_members_box_with_avatars
@request.session[:user_id] = 2
get(:show, :params => {:id => 'ecookbook'})
assert_response :success
assert_select 'div.members.box' do
assert_select 'p span.label', :text => 'Manager:'
assert_select 'p a.user[href=?]', '/users/2' do
assert_select '.avatar'
end
end
end
def test_show_should_link_the_number_of_members_not_displayed_of_a_role
# 12 active members with the role, 10 of them displayed
11.times {User.add_to_project(User.generate!, Project.find(1), Role.find(2))}
@request.session[:user_id] = 2
get(:show, :params => {:id => 'ecookbook'})
assert_response :success
assert_select 'div.members.box' do
assert_select 'p a.user', :count => 10 + 1
assert_select(
'p a.badge[href=?][title=?]',
'/projects/ecookbook/settings/members?member_role_id=2', '2 more members',
:text => '+2'
)
end
end
def test_show_should_not_link_the_number_of_members_not_displayed_without_manage_members_permission
11.times {User.add_to_project(User.generate!, Project.find(1), Role.find(2))}
@request.session[:user_id] = 7
get(:show, :params => {:id => 'ecookbook'})
assert_response :success
assert_select 'div.members.box' do
assert_select 'p span.badge[title=?]', '2 more members', :text => '+2'
assert_select 'p a[href*=?]', 'settings/members', :count => 0
end
end
def test_show_should_display_visible_custom_fields
ProjectCustomField.find_by_name('Development status').update_attribute :visible, true
get(:show, :params => {:id => 'ecookbook'})
test/helpers/application_helper_test.rb
assert_equal result, link_to_principal(user)
end
def test_link_to_principal_with_avatar_should_replace_the_icon_with_an_avatar
html = link_to_principal(User.find(2), :avatar => true)
assert_select_in html, 'a.user.active[href=?]', '/users/2' do
assert_select '.avatar'
assert_select 'svg', :count => 0
end
end
def test_link_to_principal_with_avatar_size
html = link_to_principal(Group.find(10), :avatar => 24)
assert_select_in html, 'a.group[href=?]', '/groups/10' do
assert_select 'img.avatar[width=?]', '24'
end
end
def test_link_to_principal_should_link_to_group
group = Group.find(10)
result = %r{<a class="group" href="/groups/10"><svg class="s18 icon-svg" aria-hidden="true"><use href="/assets/icons-\w+.svg#icon--group"></use></svg>A Team</a>}
test/unit/project_test.rb
assert_not principals_by_role.values.flatten.include?(locked_user)
end
def test_roles_with_principals
group = Group.find(10)
Member.create!(:principal => group, :project_id => 1, :role_ids => [1])
roles_with_principals = Project.find(1).roles_with_principals
assert_kind_of Array, roles_with_principals
role, principals, count = roles_with_principals.detect {|r, _, _| r == Role.find(1)}
assert_equal Role.find(1), role
# The group, and the users it gives the role to, are members with the role too
assert_equal 3, count
assert_include User.find(2), principals
assert_include group, principals
# Groups are listed after the users
assert_equal group, principals.last
end
def test_roles_with_principals_should_be_ordered_by_role_position
roles = Project.find(1).roles_with_principals.map(&:first)
assert_equal roles.sort_by(&:position), roles
end
def test_roles_with_principals_should_only_return_active_principals
locked_user = User.find(5)
assert_include locked_user, Project.find(1).memberships.map(&:principal)
roles_with_principals = Project.find(1).roles_with_principals
assert_not_include locked_user, roles_with_principals.flat_map {|_, principals, _| principals}
end
def test_roles_with_principals_should_limit_the_principals_but_not_the_count
project = Project.find(1)
role = Role.find(2)
11.times {User.add_to_project(User.generate!, project, role)}
_, principals, count = project.roles_with_principals.detect {|r, _, _| r == role}
assert_equal 10, principals.size
assert_equal 12, count
end
def test_roles_with_principals_should_list_a_member_under_each_of_its_roles
member = Member.find(1)
member.update!(:role_ids => [1, 2])
roles_with_principals = Project.find(1).roles_with_principals
[1, 2].each do |role_id|
_, principals, = roles_with_principals.detect {|r, _, _| r == Role.find(role_id)}
assert_include member.principal, principals
end
end
def test_roles_with_principals_should_not_include_the_members_of_other_projects
principals = Project.find(1).roles_with_principals.flat_map {|_, p, _| p}
assert_not_include User.find(8), principals
end
def test_roles_with_principals_should_return_an_empty_array_without_members
assert_equal [], Project.generate!.roles_with_principals
end
def test_rolled_up_trackers
parent = Project.find(1)
parent.trackers = Tracker.find([1, 2])
(2-2/2)