Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#55581] construct project membership string, link to profile

Tobias Dillmann3f2ab1dad56d
diff --git a/app/components/users/hover_card_component.html.erb b/app/components/users/hover_card_component.html.erb
index 8a71b8fa434..98d68e7a8c4 100644
--- a/app/components/users/hover_card_component.html.erb
+++ b/app/components/users/hover_card_component.html.erb
@@ -11,8 +11,10 @@
render(Primer::Beta::Text.new(font_weight: :semibold)) { @user.name }
end
- f.with_column do
- render(Primer::Beta::Text.new(color: :muted)) { @user.mail } # TODO: only with permission
+ if show_email?
+ f.with_column do
+ render(Primer::Beta::Text.new(color: :muted)) { @user.mail } # TODO: only with permission
+ end
end
end
end
@@ -24,13 +26,16 @@
end
f.with_column do
- render(Primer::Beta::Text.new(color: :muted)) { "Member of ..., ... and 4 more" }
+ render(Primer::Beta::Text.new(color: :muted)) { project_membership_summary }
end
end
end
flex.with_row do
- render(Primer::Beta::Button.new) { "Open profile" }
+ # TODO: could also use helpers.allowed_management_user_profile_path here...
+ render(Primer::Beta::Button.new(tag: :a, href: user_path(@user))) do
+ "Open profile"
+ end
end
end
else
diff --git a/app/components/users/hover_card_component.rb b/app/components/users/hover_card_component.rb
index 6a7fd3fa024..639886e97cb 100644
--- a/app/components/users/hover_card_component.rb
+++ b/app/components/users/hover_card_component.rb
@@ -11,6 +11,50 @@ class Users::HoverCardComponent < ApplicationComponent
end
def show_email?
- false
+ # TODO: permission
+ true
+ end
+
+ # Constructs a string in the form of:
+ # "Member of project4, project5"
+ # or
+ # "Member of project1, project2 and 3 more"
+ # The latter string is cut off since the complete list of project names would exceed the allowed `max_length`.
+ def project_membership_summary(max_length = 50)
+ project_names = @user.projects.pluck(:name)
+ return no_project_text if project_names.empty?
+
+ cutoff_index = calculate_cutoff_index(project_names, max_length)
+ build_summary(project_names, cutoff_index)
+ end
+
+ private
+
+ def no_project_text
+ t("users.memberships.no_results_title_text")
+ end
+
+ def calculate_cutoff_index(names, max_length)
+ current_length = 0
+
+ names.each_with_index do |name, index|
+ new_length = current_length + name.length + (index > 0 ? 2 : 0) # 2 for ", " separator
+ return index if new_length > max_length
+
+ current_length = new_length
+ end
+
+ names.size # No cutoff needed -> return the total size
+ end
+
+ def build_summary(names, cutoff_index)
+ summary_names = names[0...cutoff_index].join(", ")
+ remaining_count = names.size - cutoff_index
+
+ if remaining_count > 0
+ t("users.memberships.summary_with_more", names: summary_names, count: remaining_count)
+ else
+ t("users.memberships.summary", names: summary_names)
+ end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 9657cfe546b..598d2335780 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -541,6 +541,8 @@ en:
no_results_title_text: This user is currently not a member in any group.
memberships:
no_results_title_text: This user is currently not a member of a project.
+ summary_with_more: Member of %{names} and %{count} more
+ summary: Member of %{names}
page:
text: "Text"
placeholder_users: