From 506e47a595930ba77f96146e8035b20918e64b7d Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 14 Jul 2017 09:45:36 +0200 Subject: [PATCH] fixes sorting of assigned column [fixes SCI-1483] --- app/datatables/repository_datatable.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/datatables/repository_datatable.rb b/app/datatables/repository_datatable.rb index 710ba08cf..93d27554d 100644 --- a/app/datatables/repository_datatable.rb +++ b/app/datatables/repository_datatable.rb @@ -248,10 +248,7 @@ class RepositoryDatatable < AjaxDatatablesRails::Base my_module_repository_rows.id IS NULL))" ).order("my_module_repository_rows.id NULLS #{direction}") else - records.joins( - 'LEFT OUTER JOIN my_module_repository_rows ON - (repository_rows.id = my_module_repository_rows.repository_row_id)' - ).order("my_module_repository_rows.id NULLS #{direction}") + sort_assigned_records(records, params[:order].values[0]['dir']) end elsif sorting_by_custom_column # Check if have to filter records first @@ -391,4 +388,21 @@ class RepositoryDatatable < AjaxDatatablesRails::Base @sortable_displayed_columns = sort_order end + + def sort_assigned_records(records, direction) + assigned = records.joins(:my_module_repository_rows).distinct.pluck(:id) + unassigned = records.where.not(id: assigned).pluck(:id) + if direction == 'asc' + ids = assigned + unassigned + elsif direction == 'desc' + ids = unassigned + assigned + end + + order_by_index = ActiveRecord::Base.send( + :sanitize_sql_array, + ["position((',' || repository_rows.id || ',') in ?)", + ids.join(',') + ','] + ) + records.order(order_by_index) + end end