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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Extract group functionality into index endpoint
Oliver Günther4e2dc171db37
diff --git a/app/models/queries/notifications/orders/reason_order.rb b/app/models/queries/notifications/orders/reason_order.rbindex 68afe3cc49c..90f508539ca 100644--- a/app/models/queries/notifications/orders/reason_order.rb+++ b/app/models/queries/notifications/orders/reason_order.rb@@ -34,4 +34,8 @@ class Queries::Notifications::Orders::ReasonOrder < Queries::Orders::Basedef self.key:reasonend++ def name+ :reason_ian+ endenddiff --git a/app/services/api/v3/work_package_collection_from_query_service.rb b/app/services/api/v3/work_package_collection_from_query_service.rbindex f88d9d7b8eb..4a09c772541 100644--- a/app/services/api/v3/work_package_collection_from_query_service.rb+++ b/app/services/api/v3/work_package_collection_from_query_service.rb@@ -104,7 +104,7 @@ module APIsums = generate_group_sumsresults.work_package_count_by_group.map do |group, count|- ::API::Decorators::AggregationGroup.new(group, count, query: query, sums: sums[group], current_user: current_user)+ ::API::V3::WorkPackages::WorkPackageAggregationGroup.new(group, count, query: query, sums: sums[group], current_user: current_user)endenddiff --git a/app/services/params_to_query_service.rb b/app/services/params_to_query_service.rbindex 8caf5ad8ff2..b8e479a4ace 100644--- a/app/services/params_to_query_service.rb+++ b/app/services/params_to_query_service.rb@@ -40,6 +40,7 @@ class ParamsToQueryServicequery = apply_filters(query, params)apply_order(query, params)+ apply_group_by(query, params)endprivate@@ -74,6 +75,14 @@ class ParamsToQueryServicequery.order(hash_sort)end+ def apply_group_by(query, params)+ return query unless params[:groupBy]++ group_by = convert_attribute(params[:groupBy])++ query.group(group_by)+ end+# Expected format looks like:# [# {diff --git a/lib/api/decorators/aggregation_group.rb b/lib/api/decorators/aggregation_group.rbindex d5f6c881b02..d4314870639 100644--- a/lib/api/decorators/aggregation_group.rb+++ b/lib/api/decorators/aggregation_group.rb@@ -31,9 +31,8 @@module APImodule Decoratorsclass AggregationGroup < Single- def initialize(group_key, count, query:, current_user:, sums: nil)+ def initialize(group_key, count, query:, current_user:)@count = count- @sums = sums@query = queryif group_key.is_a?(Array)@@ -55,15 +54,6 @@ module APIendend- link :groupBy do- converted_name = convert_attribute(query.group_by_column.name)-- {- href: api_v3_paths.query_group_by(converted_name),- title: query.group_by_column.caption- }- end-property :value,exec_context: :decorator,render_nil: true@@ -73,23 +63,11 @@ module APIgetter: ->(*) { count },render_nil: true- property :sums,- exec_context: :decorator,- getter: ->(*) {- ::API::V3::WorkPackages::WorkPackageSumsRepresenter.create(sums, current_user) if sums- },- render_nil: false-- def has_sums?- sums.present?- end-def model_required?falseend- attr_reader :sums,- :count,+ attr_reader :count,:query##@@ -104,17 +82,13 @@ module API}end- if group_key.empty?- nil- else+ if group_key.present?group_key.map(&:name).sort.join(", ")endenddef value- if query.group_by_column.name == :done_ratio- "#{represented}%"- elsif represented == true || represented == false+ if represented == true || represented == falserepresentedelserepresented ? represented.to_s : nildiff --git a/lib/api/decorators/collection.rb b/lib/api/decorators/collection.rbindex f62d50d76ab..1ca2c12c8e5 100644--- a/lib/api/decorators/collection.rb+++ b/lib/api/decorators/collection.rb@@ -33,8 +33,9 @@ module APIclass Collection < ::API::Decorators::Singleinclude API::Utilities::UrlHelper- def initialize(models, total, self_link:, current_user:)+ def initialize(models, total, self_link:, current_user:, groups: nil)@total = total+ @groups = groups@self_link = self_linksuper(models, current_user: current_user)@@ -69,6 +70,10 @@ module APIproperty :total, getter: ->(*) { @total }, exec_context: :decoratorproperty :count, getter: ->(*) { count }+ property :groups,+ exec_context: :decorator,+ render_nil: false+collection :elements,getter: ->(*) {represented.map do |model|@@ -81,6 +86,8 @@ module APIdef _type'Collection'end++ attr_reader :groupsendendenddiff --git a/lib/api/decorators/offset_paginated_collection.rb b/lib/api/decorators/offset_paginated_collection.rbindex c4969184637..d4e50692244 100644--- a/lib/api/decorators/offset_paginated_collection.rb+++ b/lib/api/decorators/offset_paginated_collection.rb@@ -37,7 +37,7 @@ module APIrelation.base_class.per_pageend- def initialize(models, self_link:, current_user:, query: {}, page: nil, per_page: nil)+ def initialize(models, self_link:, current_user:, query: {}, page: nil, per_page: nil, groups: nil)@self_link_base = self_link@query = query@page = page.to_i > 0 ? page.to_i : 1@@ -46,7 +46,7 @@ module APIfull_self_link = make_page_link(page: @page, page_size: @per_page)paged = paged_models(models)- super(paged, models.count, self_link: full_self_link, current_user: current_user)+ super(paged, models.count, self_link: full_self_link, current_user: current_user, groups: groups)endlink :jumpTo dodiff --git a/lib/api/v3/utilities/endpoints/index.rb b/lib/api/v3/utilities/endpoints/index.rbindex 7e1c816d32a..b3b3ef1b37f 100644--- a/lib/api/v3/utilities/endpoints/index.rb+++ b/lib/api/v3/utilities/endpoints/index.rb@@ -88,6 +88,7 @@ module APIquery: resulting_params,page: resulting_params[:offset],per_page: resulting_params[:pageSize],+ groups: calculate_groups(query),current_user: User.current)end@@ -105,6 +106,14 @@ module APIendend+ def calculate_groups(query)+ return unless query.group_by++ query.groups.map do |group, count|+ ::API::Decorators::AggregationGroup.new(group, count, query: query, current_user: User.current)+ end+ end+def calculate_default_params(query)::API::Decorators::QueryParamsRepresenter.new(query)diff --git a/lib/api/v3/work_packages/work_package_aggregation_group.rb b/lib/api/v3/work_packages/work_package_aggregation_group.rbnew file mode 100644index 00000000000..0da70bea56e--- /dev/null+++ b/lib/api/v3/work_packages/work_package_aggregation_group.rb@@ -0,0 +1,73 @@+#-- encoding: UTF-8++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2021 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 API+ module V3+ module WorkPackages+ class WorkPackageAggregationGroup < ::API::Decorators::AggregationGroup+ def initialize(group_key, count, query:, current_user:, sums: nil)+ @sums = sums++ super(group_key, count, query: query, current_user: current_user)+ end++ property :sums,+ exec_context: :decorator,+ getter: ->(*) {+ ::API::V3::WorkPackages::WorkPackageSumsRepresenter.create(sums, current_user) if sums+ },+ render_nil: false++ link :groupBy do+ converted_name = convert_attribute(query.group_by_column.name)++ {+ href: api_v3_paths.query_group_by(converted_name),+ title: query.group_by_column.caption+ }+ end++ def has_sums?+ sums.present?+ end++ attr_reader :sums++ def value+ if query.group_by_column.name == :done_ratio+ "#{represented}%"+ else+ super+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/work_packages/work_package_collection_representer.rb b/lib/api/v3/work_packages/work_package_collection_representer.rbindex 05c5ef489b0..01c1e353ca9 100644--- a/lib/api/v3/work_packages/work_package_collection_representer.rb+++ b/lib/api/v3/work_packages/work_package_collection_representer.rb@@ -40,7 +40,6 @@ module APIper_page: nil,embed_schemas: false)@project = project- @groups = groups@total_sums = total_sums@embed_schemas = embed_schemas@@ -49,6 +48,7 @@ module APIquery: query,page: page,per_page: per_page,+ groups: groups,current_user: current_user)# In order to optimize performance we@@ -145,10 +145,6 @@ module APIembedded: true,render_nil: false- property :groups,- exec_context: :decorator,- render_nil: false-property :total_sums,exec_context: :decorator,getter: ->(*) {@@ -281,7 +277,6 @@ module APIendattr_reader :project,- :groups,:total_sums,:embed_schemasend