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
Add group_by capabilities to the base query
Oliver Güntherfc687df65dff
diff --git a/app/models/queries/base_query.rb b/app/models/queries/base_query.rbindex 0650c03f8eb..b2707892c15 100644--- a/app/models/queries/base_query.rb+++ b/app/models/queries/base_query.rb@@ -38,17 +38,21 @@ class Queries::BaseQueryendattr_accessor :filters, :orders+ attr_reader :group_byinclude Queries::AvailableFiltersinclude Queries::AvailableOrders+ include Queries::GroupBys::AvailableGroupBysinclude ActiveModel::Validationsvalidate :filters_valid,- :sortation_valid+ :sortation_valid,+ :group_by_validdef initialize(user: nil)@filters = []@orders = []+ @group_by = nil@user = userend@@ -60,6 +64,15 @@ class Queries::BaseQueryendend+ def groups+ return nil if group_by.nil?+ return empty_scope unless valid?++ apply_group_by(apply_filters(default_scope))+ .pluck(group_by.name, Arel.sql('COUNT(*)'))+ .to_h+ end+def where(attribute, operator, values)filter = filter_for(attribute)filter.operator = operator@@ -81,6 +94,12 @@ class Queries::BaseQueryselfend+ def group(attribute)+ self.group_by = group_by_for(attribute)++ self+ end+def default_scopeself.class.model.allend@@ -100,6 +119,7 @@ class Queries::BaseQueryprotectedattr_accessor :user+ attr_writer :group_bydef filters_validfilters.each do |filter|@@ -117,6 +137,12 @@ class Queries::BaseQueryendend+ def group_by_valid+ return if group_by.nil? || group_by.valid?++ add_error(:group_by, group_by.name, group_by)+ end+def add_error(local_attribute, attribute_name, object)messages = object.errors@@ -145,7 +171,7 @@ class Queries::BaseQueryenddef apply_orders(scope)- orders.each do |order|+ build_orders.each do |order|scope = scope.merge(order.scope)end@@ -156,6 +182,30 @@ class Queries::BaseQueryalready_ordered_by_id?(scope) ? scope : scope.order(id: :desc)end+ def apply_group_by(scope)+ return scope if group_by.nil?++ scope+ .merge(group_by.scope)+ .order(group_by.name)+ end++ def build_orders+ return orders if group_by.nil? || has_group_by_order?++ [group_by_order] + orders+ end++ def has_group_by_order?+ !!group_by && orders.detect { |order| order.class.key == group_by.order_key }+ end++ def group_by_order+ order_for(group_by.order_key).tap do |order|+ order.direction = :asc+ end+ end+def already_ordered_by_id?(scope)scope.order_values.any? do |order|order.respond_to?(:value) && order.value.respond_to?(:relation) &&diff --git a/app/models/queries/not_existing_filter.rb b/app/models/queries/filters/not_existing_filter.rbsimilarity index 100%rename from app/models/queries/not_existing_filter.rbrename to app/models/queries/filters/not_existing_filter.rbdiff --git a/app/models/queries/group_bys/available_group_bys.rb b/app/models/queries/group_bys/available_group_bys.rbnew file mode 100644index 00000000000..b1a0184f0fa--- /dev/null+++ b/app/models/queries/group_bys/available_group_bys.rb@@ -0,0 +1,51 @@+#-- 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 Queries+ module GroupBys+ module AvailableGroupBys+ def group_by_for(key)+ (find_registered_group_by(key) || ::Queries::GroupBys::NotExistingGroupBy).new(key)+ end++ private++ def find_registered_group_by(key)+ group_by_register.detect do |s|+ s.key === key.to_sym+ end+ end++ def group_by_register+ ::Queries::Register.group_bys[self.class]+ end+ end+ end+enddiff --git a/app/models/queries/group_bys/base.rb b/app/models/queries/group_bys/base.rbnew file mode 100644index 00000000000..6022150f9db--- /dev/null+++ b/app/models/queries/group_bys/base.rb@@ -0,0 +1,72 @@+#-- 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 Queries+ module GroupBys+ class Base+ include ActiveModel::Validations++ def self.i18n_scope+ :activerecord+ end++ class_attribute :model+ attr_accessor :attribute++ def initialize(attribute)+ self.attribute = attribute+ end++ def self.key+ raise NotImplementedError+ end++ def scope+ group_by+ end++ def name+ attribute+ end++ # Default to the same key for order+ # as the one for group+ def order_key+ self.class.key+ end++ protected++ def group_by+ model.group(name)+ end+ end+ end+end\ No newline at end of filediff --git a/app/models/queries/group_bys/not_existing_group_by.rb b/app/models/queries/group_bys/not_existing_group_by.rbnew file mode 100644index 00000000000..b1b5ca9ff2a--- /dev/null+++ b/app/models/queries/group_bys/not_existing_group_by.rb@@ -0,0 +1,47 @@+#-- 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 Queries+ module GroupBys+ class NotExistingGroupBy < Base+ validate :always_false++ def self.key+ :inexistent+ end++ private++ def always_false+ errors.add :base, I18n.t(:'activerecord.errors.messages.does_not_exist')+ end+ end+ end+end\ No newline at end of filediff --git a/app/models/queries/notifications.rb b/app/models/queries/notifications.rbindex 5517c55f0fa..39bbd27525f 100644--- a/app/models/queries/notifications.rb+++ b/app/models/queries/notifications.rb@@ -43,4 +43,9 @@ module Queries::NotificationsQueries::Register.order Queries::Notifications::NotificationQuery,orderend++ [Queries::Notifications::GroupBys::GroupByReason].each do |group|+ Queries::Register.group_by Queries::Notifications::NotificationQuery,+ group+ endenddiff --git a/app/models/queries/base_order.rb b/app/models/queries/notifications/group_bys/group_by_reason.rbsimilarity index 58%rename from app/models/queries/base_order.rbrename to app/models/queries/notifications/group_bys/group_by_reason.rbindex f28cae7677a..343038db3a9 100644--- a/app/models/queries/base_order.rb+++ b/app/models/queries/notifications/group_bys/group_by_reason.rb@@ -28,59 +28,14 @@# See COPYRIGHT and LICENSE files for more details.#++-class Queries::BaseOrder- include ActiveModel::Validations-- VALID_DIRECTIONS = %i(asc desc).freeze-- def self.i18n_scope- :activerecord- end-- validates :direction, inclusion: { in: VALID_DIRECTIONS }-- class_attribute :model- attr_accessor :direction,- :attribute-- def initialize(attribute)- self.attribute = attribute- end+class Queries::Notifications::GroupBys::GroupByReason < Queries::GroupBys::Base+ self.model = Notificationdef self.key- raise NotImplementedError- end-- def scope- scope = order- scope = scope.joins(joins) if joins- scope = scope.left_outer_joins(left_outer_joins) if left_outer_joins- scope+ :reasonenddef name- attribute- end-- private-- def order- model.order(name => direction)- end-- def joins- nil- end-- def left_outer_joins- nil- end-- def with_raise_on_invalid- if VALID_DIRECTIONS.include?(direction)- yield- else- raise ArgumentError, "Only one of #{VALID_DIRECTIONS} allowed. #{direction} is provided."- end+ :reason_ianendenddiff --git a/app/models/queries/orders/base.rb b/app/models/queries/orders/base.rbnew file mode 100644index 00000000000..20c7d078106--- /dev/null+++ b/app/models/queries/orders/base.rb@@ -0,0 +1,90 @@+#-- 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 Queries+ module Orders+ class Base+ include ActiveModel::Validations++ VALID_DIRECTIONS = %i(asc desc).freeze++ def self.i18n_scope+ :activerecord+ end++ validates :direction, inclusion: { in: VALID_DIRECTIONS }++ class_attribute :model+ attr_accessor :direction,+ :attribute++ def initialize(attribute)+ self.attribute = attribute+ end++ def self.key+ raise NotImplementedError+ end++ def scope+ scope = order+ scope = scope.joins(joins) if joins+ scope = scope.left_outer_joins(left_outer_joins) if left_outer_joins+ scope+ end++ def name+ attribute+ end++ private++ def order+ model.order(name => direction)+ end++ def joins+ nil+ end++ def left_outer_joins+ nil+ end++ def with_raise_on_invalid+ if VALID_DIRECTIONS.include?(direction)+ yield+ else+ raise ArgumentError, "Only one of #{VALID_DIRECTIONS} allowed. #{direction} is provided."+ end+ end+ end+ end+enddiff --git a/app/models/queries/register.rb b/app/models/queries/register.rbindex 9178c2f4d53..6ee9a6a2d22 100644--- a/app/models/queries/register.rb+++ b/app/models/queries/register.rb@@ -46,6 +46,14 @@ module Queries::Register@orders[query] << orderend+ def group_by(query, group_by)+ @group_bys ||= Hash.new do |hash, group_key|+ hash[group_key] = []+ end++ @group_bys[query] << group_by+ end+def column(query, column)@columns ||= Hash.new do |hash, column_key|hash[column_key] = []@@ -60,6 +68,8 @@ module Queries::Registerattr_accessor :filters,:orders,- :columns+ :columns,+ :group_bys+endend