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

avoid sorting and order on counting

ulferts15b81403c7f0
diff --git a/app/models/queries/projects/orders/name_order.rb b/app/models/queries/projects/orders/name_order.rb
index 0a5a2932753..cf2e148712d 100644
--- a/app/models/queries/projects/orders/name_order.rb
+++ b/app/models/queries/projects/orders/name_order.rb
@@ -35,6 +35,10 @@ class Queries::Projects::Orders::NameOrder < Queries::BaseOrder
:name
end
+ def scope
+ super.select('projects.*', 'lower(projects.name)')
+ end
+
def order
with_raise_on_invalid do
model.order(Arel.sql("lower(projects.name)").send(direction))
diff --git a/lib/api/decorators/unpaginated_collection.rb b/lib/api/decorators/unpaginated_collection.rb
index efaafade73e..0456487f5a4 100644
--- a/lib/api/decorators/unpaginated_collection.rb
+++ b/lib/api/decorators/unpaginated_collection.rb
@@ -31,7 +31,18 @@ module API
module Decorators
class UnpaginatedCollection < ::API::Decorators::Collection
def initialize(models, self_link, current_user:)
- super(models, models.count, self_link, current_user: current_user)
+ super(models, model_count(models), self_link, current_user: current_user)
+ end
+
+ def model_count(models)
+ if models.respond_to?(:except)
+ # We do not want any order/selecting with counting
+ # when it would result in an invalid SELECT COUNT(DISTINCT *, ).
+ # As both, order and select should have no impact on the count result, we remove them.
+ models.except(:select, :order)
+ else
+ models
+ end.count
end
end
end