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

3.1 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

class Users::HoverCardComponent < ApplicationComponent
  include OpPrimer::ComponentHelpers

  def initialize(id:)
    super

    @id = id
    @user = User.find_by(id: @id)
  end

  def show_email?
    (@user == User.current) || User.current.allowed_globally?(:view_user_email)
  end

  # Constructs a string in the form of:
  # "Member of group4, group5"
  # or
  # "Member of group1, group2 and 3 more"
  # The latter string is cut off since the complete list of group names would exceed the allowed `max_length`.
  def group_membership_summary(max_length = 40)
    groups = @user.groups.visible.order(:lastname)
    return no_group_text if groups.empty?

    group_links = linked_group_names(groups)

    cutoff_index = calculate_cutoff_index(groups.map(&:name), max_length)
    build_summary(group_links, cutoff_index)
  end

  private

  def linked_group_names(groups)
    groups.map { |group| link_to(h(group.name), show_group_path(group)) }
  end

  def no_group_text
    t("users.groups.no_results_title_text")
  end

  # Calculate the index at which to cut off the group names, based on plain text length
  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(links, cutoff_index)
    summary_links = safe_join(links[0...cutoff_index], ", ")
    remaining_count = links.size - cutoff_index
    remaining_count_link = link_to(t("users.groups.more", count: remaining_count), user_path(@user))

    if remaining_count > 0
      t("users.groups.summary_with_more", names: summary_links, count_link: remaining_count_link).html_safe
    else
      t("users.groups.summary", names: summary_links).html_safe
    end
  end
end