Skip to content

Commit

Permalink
handle ordering of multi value version custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Sep 24, 2024
1 parent c9d8395 commit f44d9ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
16 changes: 12 additions & 4 deletions app/models/custom_field/order_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,18 @@ def order_by_user_sql
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,22 @@ def project_without_cf_value

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
Expand Down
19 changes: 12 additions & 7 deletions spec/models/query/results_cf_sorting_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,22 @@ def wp_without_cf_value

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
Expand Down

0 comments on commit f44d9ce

Please sign in to comment.