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

Add shared column for work packages without filter capability

Oliver Günther6cc61a51c73f
diff --git a/app/components/members/row_component.rb b/app/components/members/row_component.rb
index c0d9325c68b..4d748ec8405 100644
--- a/app/components/members/row_component.rb
+++ b/app/components/members/row_component.rb
@@ -73,6 +73,15 @@ module Members
end
end
+ def shared
+ count = member.shared_work_packages_count
+ if count > 0
+ link_to I18n.t(:'label_x_work_packages', count:),
+ helpers.project_work_packages_shared_with_path(principal, member.project),
+ target: "_blank"
+ end
+ end
+
def roles_label
label = h member.roles.uniq.sort.collect(&:name).join(', ')
diff --git a/app/components/members/table_component.rb b/app/components/members/table_component.rb
index 511fb3037dc..f45c3360550 100644
--- a/app/components/members/table_component.rb
+++ b/app/components/members/table_component.rb
@@ -31,9 +31,17 @@
module Members
class TableComponent < ::TableComponent
options :authorize_update, :available_roles, :is_filtered
- columns :name, :mail, :roles, :groups, :status
+ columns :name, :mail, :roles, :groups, :shared, :status
sortable_columns :name, :mail, :status
+ def initialize_sorted_model
+ super
+
+ # There's no way to provide an additional scope to the query
+ # without accessing the results. So we have to do it here.
+ @model = @model.with_shared_work_packages_count
+ end
+
def initial_sort
%i[name asc]
end
@@ -45,7 +53,15 @@ module Members
end
def header_options(name)
- { caption: User.human_attribute_name(name) }
+ caption =
+ case name
+ when :shared
+ I18n.t('members.columns.shared')
+ else
+ User.human_attribute_name(name)
+ end
+
+ { caption: }
end
##
diff --git a/app/helpers/work_packages_filter_helper.rb b/app/helpers/work_packages_filter_helper.rb
index 3137dc78160..94fb7f80027 100644
--- a/app/helpers/work_packages_filter_helper.rb
+++ b/app/helpers/work_packages_filter_helper.rb
@@ -48,6 +48,16 @@ module WorkPackagesFilterHelper
project_work_packages_with_query_path(version.project, query, options)
end
+ def project_work_packages_shared_with_path(principal, project, options = {})
+ query = {
+ f: [
+ filter_object('status_id', '*'),
+ filter_object('shared_with_user', '=', principal.id)
+ ]
+ }
+ project_work_packages_with_query_path(project, query, options)
+ end
+
# Links for reports
def project_report_property_path(project, property_name, property_id, options = {})
diff --git a/app/models/member.rb b/app/models/member.rb
index 349996b23b6..f88c982a86c 100644
--- a/app/models/member.rb
+++ b/app/models/member.rb
@@ -57,7 +57,8 @@ class Member < ApplicationRecord
:of_any_work_package,
:of_any_entity,
:of_anything_in_project,
- :visible
+ :visible,
+ :with_shared_work_packages_count
delegate :name, to: :principal
diff --git a/app/models/members/scopes/with_shared_work_packages_count.rb b/app/models/members/scopes/with_shared_work_packages_count.rb
new file mode 100644
index 00000000000..b61691e480c
--- /dev/null
+++ b/app/models/members/scopes/with_shared_work_packages_count.rb
@@ -0,0 +1,66 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+module Members::Scopes
+ module WithSharedWorkPackagesCount
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def with_shared_work_packages_count
+ Member
+ .from("#{Member.table_name} members")
+ .joins(shared_work_packages_sql)
+ .select('members.*')
+ .select('members_sums.shared_work_packages_count AS shared_work_packages_count')
+ end
+
+ private
+
+ def shared_work_packages_sql
+ <<~SQL.squish
+ LEFT JOIN (
+ SELECT members_sums.user_id, members_sums.project_id, COUNT(*) AS shared_work_packages_count
+ FROM #{Member.table_name} members_sums
+ WHERE members_sums.entity_type = 'WorkPackage'
+ GROUP BY members_sums.user_id, members_sums.project_id
+ ) members_sums
+ ON members.user_id = members_sums.user_id AND members.project_id = members_sums.project_id
+ SQL
+ end
+ end
+
+ def shared_work_packages_count
+ @shared_work_packages_count ||= begin
+ value = read_attribute(:shared_work_packages_count) ||
+ self.class.with_shared_work_packages_count.where(id:).pick('members_sums.shared_work_packages_count')
+
+ value.to_i
+ end
+ end
+ end
+end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 91aff80b279..ab58d06ccd1 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -313,6 +313,8 @@ en:
no_results_content_text: Add a member to the project
invite_by_mail: "Send invite to %{mail}"
send_invite_to: "Send invite to"
+ columns:
+ shared: "Shared"
filters:
all_shares: 'All shares'
menu:
@@ -2288,6 +2290,10 @@ en:
one: "1 open"
other: "%{count} open"
zero: "0 open"
+ label_x_work_packages:
+ one: "1 work package"
+ other: "%{count} work packages"
+ zero: "No work packages"
label_x_projects:
one: "1 project"
other: "%{count} projects"