fixes sorting of assigned column [fixes SCI-1483]

This commit is contained in:
zmagod 2017-07-14 09:45:36 +02:00
parent 9eba178dce
commit 506e47a595

View file

@ -248,10 +248,7 @@ class RepositoryDatatable < AjaxDatatablesRails::Base
my_module_repository_rows.id IS NULL))" my_module_repository_rows.id IS NULL))"
).order("my_module_repository_rows.id NULLS #{direction}") ).order("my_module_repository_rows.id NULLS #{direction}")
else else
records.joins( sort_assigned_records(records, params[:order].values[0]['dir'])
'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}")
end end
elsif sorting_by_custom_column elsif sorting_by_custom_column
# Check if have to filter records first # Check if have to filter records first
@ -391,4 +388,21 @@ class RepositoryDatatable < AjaxDatatablesRails::Base
@sortable_displayed_columns = sort_order @sortable_displayed_columns = sort_order
end 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 end