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
handle ordering of multi value user custom fields
Ivan Kuchin40af26bbe71c
diff --git a/app/models/custom_field/order_statements.rb b/app/models/custom_field/order_statements.rbindex 54a0fe232d5..939ad6f57ac 100644--- a/app/models/custom_field/order_statements.rb+++ b/app/models/custom_field/order_statements.rb@@ -123,10 +123,20 @@ module CustomField::OrderStatementsenddef order_by_user_sql- <<-SQL- (SELECT ARRAY[cv_user.lastname, cv_user.firstname, cv_user.mail] FROM #{User.quoted_table_name} cv_user- WHERE cv_user.id = #{select_custom_value_as_decimal}- LIMIT 1)+ columns_array = "ARRAY[cv_user.lastname, cv_user.firstname, cv_user.mail]"++ columns = multi_value? ? "array_agg(#{columns_array} ORDER BY #{columns_array})" : columns_array+ limit = multi_value? ? "" : "LIMIT 1"++ <<-SQL.squish+ (+ SELECT #{columns}+ FROM #{User.quoted_table_name} cv_user+ INNER JOIN #{CustomValue.quoted_table_name} cv_sort+ ON cv_sort.value IS NOT NULL AND cv_sort.value != '' AND cv_user.id = cv_sort.value::bigint+ WHERE #{cv_sort_only_custom_field_condition_sql}+ #{limit}+ )SQLenddiff --git a/spec/models/queries/projects/project_query_custom_field_order_spec.rb b/spec/models/queries/projects/project_query_custom_field_order_spec.rbindex e47e4fa9f05..5311586c176 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@@ -222,10 +222,10 @@ RSpec.describe ProjectQuery, "order using CustomFieldOrder" docontext "for user format" doshared_let(:users) do[- create(:user, lastname: "B", firstname: "B", login: "bb1"),- create(:user, lastname: "B", firstname: "B", login: "bb2"),- create(:user, lastname: "B", firstname: "A", login: "ba"),- create(:user, lastname: "A", firstname: "X", login: "ax")+ create(:user, lastname: "B", firstname: "B", login: "bb1", mail: "[email protected]"),+ create(:user, lastname: "B", firstname: "B", login: "bb2", mail: "[email protected]"),+ create(:user, lastname: "B", firstname: "A", login: "ba", mail: "[email protected]"),+ create(:user, lastname: "A", firstname: "X", login: "ax", mail: "[email protected]")]endshared_let(:id_by_login) { users.to_h { [_1.login, _1.id] } }@@ -272,11 +272,12 @@ RSpec.describe ProjectQuery, "order using CustomFieldOrder" dolet(:custom_field_values) do[- id_by_login.fetch_values("ax"),- id_by_login.fetch_values("ba"),- # TODO: second user is ignored- id_by_login.fetch_values("bb1", "ba"),- id_by_login.fetch_values("bb1", "ax"),+ id_by_login.fetch_values("ax"), # ax+ id_by_login.fetch_values("bb1", "ax"), # ax, bb1+ id_by_login.fetch_values("ax", "bb1"), # ax, bb1+ id_by_login.fetch_values("ba"), # ba+ id_by_login.fetch_values("bb1", "ba"), # ba, bb1+ id_by_login.fetch_values("ba", "bb2"), # ba, bb2[] # TODO: should be at index 0]end@@ -287,8 +288,12 @@ RSpec.describe ProjectQuery, "order using CustomFieldOrder" doendend- # TODO: second user 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)+ endendendenddiff --git a/spec/models/query/results_cf_sorting_integration_spec.rb b/spec/models/query/results_cf_sorting_integration_spec.rbindex 7e2d0f1ec1f..9edbc8045e2 100644--- a/spec/models/query/results_cf_sorting_integration_spec.rb+++ b/spec/models/query/results_cf_sorting_integration_spec.rb@@ -233,10 +233,10 @@ RSpec.describe Query::Results, "Sorting by custom field" docontext "for user format" doshared_let(:users) do[- create(:user, lastname: "B", firstname: "B", login: "bb1"),- create(:user, lastname: "B", firstname: "B", login: "bb2"),- create(:user, lastname: "B", firstname: "A", login: "ba"),- create(:user, lastname: "A", firstname: "X", login: "ax")+ create(:user, lastname: "B", firstname: "B", login: "bb1", mail: "[email protected]"),+ create(:user, lastname: "B", firstname: "B", login: "bb2", mail: "[email protected]"),+ create(:user, lastname: "B", firstname: "A", login: "ba", mail: "[email protected]"),+ create(:user, lastname: "A", firstname: "X", login: "ax", mail: "[email protected]")]endshared_let(:id_by_login) { users.to_h { [_1.login, _1.id] } }@@ -271,17 +271,22 @@ RSpec.describe Query::Results, "Sorting by custom field" dolet(:work_packages) do[- wp_with_cf_value(id_by_login.fetch_values("ax")),- wp_with_cf_value(id_by_login.fetch_values("ba")),- # TODO: second user is ignored- wp_with_cf_value(id_by_login.fetch_values("bb1", "ba")),- wp_with_cf_value(id_by_login.fetch_values("bb1", "ax")),+ wp_with_cf_value(id_by_login.fetch_values("ax")), # ax+ wp_with_cf_value(id_by_login.fetch_values("bb1", "ax")), # ax, bb1+ wp_with_cf_value(id_by_login.fetch_values("ax", "bb1")), # ax, bb1+ wp_with_cf_value(id_by_login.fetch_values("ba")), # ba+ wp_with_cf_value(id_by_login.fetch_values("bb1", "ba")), # ba, bb1+ wp_with_cf_value(id_by_login.fetch_values("ba", "bb2")), # ba, bb2wp_without_cf_value # TODO: should be at index 0]end- # TODO: second user 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)+ endendendend