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

handle ordering of multi value version custom fields

Ivan Kuchin6199be82e317
diff --git a/app/models/custom_field/order_statements.rb b/app/models/custom_field/order_statements.rb
index 939ad6f57ac..5181381b6b0 100644
--- a/app/models/custom_field/order_statements.rb
+++ b/app/models/custom_field/order_statements.rb
@@ -141,10 +141,18 @@ module CustomField::OrderStatements
end
def order_by_version_sql
- <<-SQL
- (SELECT cv_version.name FROM #{Version.quoted_table_name} cv_version
- WHERE cv_version.id = #{select_custom_value_as_decimal}
- LIMIT 1)
+ columns = multi_value? ? "array_agg(cv_version.name ORDER BY cv_version.name)" : "cv_version.name"
+ limit = multi_value? ? "" : "LIMIT 1"
+
+ <<-SQL.squish
+ (
+ SELECT #{columns}
+ FROM #{Version.quoted_table_name} cv_version
+ INNER JOIN #{CustomValue.quoted_table_name} cv_sort
+ ON cv_sort.value IS NOT NULL AND cv_sort.value != '' AND cv_version.id = cv_sort.value::bigint
+ WHERE #{cv_sort_only_custom_field_condition_sql}
+ #{limit}
+ )
SQL
end
diff --git a/spec/models/queries/projects/project_query_custom_field_order_spec.rb b/spec/models/queries/projects/project_query_custom_field_order_spec.rb
index 5311586c176..fd13984ed99 100644
--- a/spec/models/queries/projects/project_query_custom_field_order_spec.rb
+++ b/spec/models/queries/projects/project_query_custom_field_order_spec.rb
@@ -332,17 +332,22 @@ RSpec.describe ProjectQuery, "order using CustomFieldOrder" do
let(:projects) do
[
- project_with_cf_value(*id_by_name.fetch_values("10.10.10")),
- project_with_cf_value(*id_by_name.fetch_values("10.10.2")),
- # TODO: second version is ignored
- project_with_cf_value(*id_by_name.fetch_values("9", "10.10.2")),
- project_with_cf_value(*id_by_name.fetch_values("9", "10.10.10")),
+ project_with_cf_value(*id_by_name.fetch_values("10.10.10")), # 10.10.10
+ project_with_cf_value(*id_by_name.fetch_values("9", "10.10.10")), # 10.10.10, 9
+ project_with_cf_value(*id_by_name.fetch_values("10.10.10", "9")), # 10.10.10, 9
+ project_with_cf_value(*id_by_name.fetch_values("10.10.2")), # 10.10.2
+ project_with_cf_value(*id_by_name.fetch_values("10.2", "10.10.2")), # 10.10.2, 10.2
+ project_with_cf_value(*id_by_name.fetch_values("10.10.2", "9")), # 10.10.2, 9
project # TODO: should be at index 0
]
end
- # TODO: second version is ignored, so order due to falling back on id asc
- let(:projects_desc) { projects.values_at(4, 2, 3, 1, 0) }
+ let(:projects_desc) do
+ indexes = projects.each_index.to_a
+ # order of values for a work package is ignored, so ordered by falling back on id asc
+ indexes[1...3] = indexes[1...3].reverse
+ projects.values_at(*indexes.reverse)
+ end
end
end
end
diff --git a/spec/models/query/results_cf_sorting_integration_spec.rb b/spec/models/query/results_cf_sorting_integration_spec.rb
index 9edbc8045e2..50cf643c9a4 100644
--- a/spec/models/query/results_cf_sorting_integration_spec.rb
+++ b/spec/models/query/results_cf_sorting_integration_spec.rb
@@ -324,17 +324,22 @@ RSpec.describe Query::Results, "Sorting by custom field" do
let(:work_packages) do
[
- wp_with_cf_value(id_by_name.fetch_values("10.10.10")),
- wp_with_cf_value(id_by_name.fetch_values("10.10.2")),
- # TODO: second version is ignored
- wp_with_cf_value(id_by_name.fetch_values("9", "10.10.2")),
- wp_with_cf_value(id_by_name.fetch_values("9", "10.10.10")),
+ wp_with_cf_value(id_by_name.fetch_values("10.10.10")), # 10.10.10
+ wp_with_cf_value(id_by_name.fetch_values("9", "10.10.10")), # 10.10.10, 9
+ wp_with_cf_value(id_by_name.fetch_values("10.10.10", "9")), # 10.10.10, 9
+ wp_with_cf_value(id_by_name.fetch_values("10.10.2")), # 10.10.2
+ wp_with_cf_value(id_by_name.fetch_values("10.2", "10.10.2")), # 10.10.2, 10.2
+ wp_with_cf_value(id_by_name.fetch_values("10.10.2", "9")), # 10.10.2, 9
wp_without_cf_value # TODO: should be at index 0
]
end
- # TODO: second version is ignored, so order due to falling back on id asc
- let(:work_packages_desc) { work_packages.values_at(4, 2, 3, 1, 0) }
+ let(:work_packages_desc) do
+ indexes = work_packages.each_index.to_a
+ # order of values for a project is ignored, so ordered by falling back on id asc
+ indexes[1...3] = indexes[1...3].reverse
+ work_packages.values_at(*indexes.reverse)
+ end
end
end
end