From e2723a288cee1389ef065b33f5eb01513dcef08b Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 21 Dec 2016 14:32:28 +0100 Subject: [PATCH 1/3] fixes bug with samples sorting [fixes SCI-812] --- .../samples/sample_datatable.js.erb | 7 ++ app/controllers/experiments_controller.rb | 3 +- app/controllers/my_modules_controller.rb | 11 ++- app/controllers/projects_controller.rb | 13 ++-- app/datatables/sample_datatable.rb | 69 +++++++++++++------ 5 files changed, 73 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js.erb b/app/assets/javascripts/samples/sample_datatable.js.erb index ba911e63e..5dd37df73 100644 --- a/app/assets/javascripts/samples/sample_datatable.js.erb +++ b/app/assets/javascripts/samples/sample_datatable.js.erb @@ -220,6 +220,13 @@ table = dataTableInit(); // Enables noSearchHidden plugin $.fn.dataTable.defaults.noSearchHidden = true; +// update datatable step on colReorder +(function() { + table.on('column-reorder', function() { + table.state.save(); + }); +})(); + // Updates "Select all" control in a data table function updateDataTableSelectAllCtrl(table) { var $table = table.table().node(); diff --git a/app/controllers/experiments_controller.rb b/app/controllers/experiments_controller.rb index 30d8d8d0b..8e1b4ebaf 100644 --- a/app/controllers/experiments_controller.rb +++ b/app/controllers/experiments_controller.rb @@ -275,7 +275,8 @@ class ExperimentsController < ApplicationController @organization, nil, nil, - @experiment) + @experiment, + current_user) end end end diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index a222066b4..26334ca73 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -325,9 +325,14 @@ class MyModulesController < ApplicationController respond_to do |format| format.html - format.json { - render json: ::SampleDatatable.new(view_context, @organization, nil, @my_module) - } + format.json do + render json: ::SampleDatatable.new(view_context, + @organization, + nil, + @my_module, + nil, + current_user) + end end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index daafa4761..e146bee8d 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -280,12 +280,17 @@ class ProjectsController < ApplicationController def samples_index @organization = @project.organization - + @user = current_user respond_to do |format| format.html - format.json { - render json: ::SampleDatatable.new(view_context, @organization, @project, nil) - } + format.json do + render json: ::SampleDatatable.new(view_context, + @organization, + @project, + nil, + nil, + @user) + end end end diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index 6984f9aac..ec39c41d8 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -33,38 +33,42 @@ class SampleDatatable < AjaxDatatablesRails::Base organization, project = nil, my_module = nil, - experiment = nil) + experiment = nil, + user = nil) super(view) @organization = organization @project = project @my_module = my_module @experiment = experiment + @user = user end # Define sortable columns, so 1st column will be sorted by attribute in sortable_columns[0] def sortable_columns sort_array = [ ASSIGNED_SORT_COL, - "Sample.name", - "SampleType.name", - "SampleGroup.name", - "Sample.created_at", - "User.full_name", + 'Sample.name', + 'SampleType.name', + 'SampleGroup.name', + 'Sample.created_at', + 'User.full_name' ] + sort_array.push(*custom_fields_sort_by) - @sortable_columns ||= sort_array + @sortable_columns = sort_array end # Define attributes on which we perform search def searchable_columns search_array = [ - "Sample.name", - "SampleType.name", - "SampleGroup.name", - "Sample.created_at", - "User.full_name" + 'Sample.name', + 'SampleType.name', + 'SampleGroup.name', + 'Sample.created_at', + 'User.full_name' ] + search_array.push(*custom_fields_sort_by) @searchable_columns ||= filter_search_array search_array end @@ -72,7 +76,7 @@ class SampleDatatable < AjaxDatatablesRails::Base private # filters the search array by checking if the the column is visible - def filter_search_array input_array + def filter_search_array(input_array) param_index = 2 filtered_array =[] input_array.each do |col| @@ -90,7 +94,7 @@ class SampleDatatable < AjaxDatatablesRails::Base array = [] for _ in 0..num_cf - array << "SampleCustomField.value" + array << 'SampleCustomField.value' end array end @@ -99,11 +103,11 @@ class SampleDatatable < AjaxDatatablesRails::Base def data records.map do |record| sample = { - "DT_RowId": record.id, - "1": assigned_cell(record), - "2": record.name, - "3": record.sample_type.nil? ? I18n.t("samples.table.no_type") : record.sample_type.name, - "4": record.sample_group.nil? ? + 'DT_RowId': record.id, + '1': assigned_cell(record), + '2': record.name, + '3': record.sample_type.nil? ? I18n.t('samples.table.no_type') : record.sample_type.name, + '4': record.sample_group.nil? ? " " + I18n.t("samples.table.no_group") : " " + record.sample_group.name, "5": I18n.l(record.created_at, format: :full), @@ -211,12 +215,13 @@ class SampleDatatable < AjaxDatatablesRails::Base # Override default sort method if needed def sort_records(records) - if params[:order].present? and params[:order].length == 1 + if params[:order].present? && params[:order].length == 1 if sort_column(params[:order].values[0]) == ASSIGNED_SORT_COL # If "assigned" column is sorted if @my_module then # Depending on the sort, order nulls first or # nulls last on sample_my_modules association + records.order("sample_my_modules.id NULLS #{sort_null_direction(params[:order].values[0])}") elsif @experiment # A very elegant solution to sort assigned samples at a experiment level @@ -250,6 +255,7 @@ class SampleDatatable < AjaxDatatablesRails::Base :sanitize_sql_array, ["position((',' || samples.id || ',') in ?)", ids.join(',') + ','] ) + records.where(id: ids).order(order_by_index) elsif @project # A very elegant solution to sort assigned samples at a project level @@ -331,7 +337,7 @@ class SampleDatatable < AjaxDatatablesRails::Base super(records) end end -#(I18n.t('time.formats.datatables_date')).gsub("\"", '\'') + # A hack that overrides the new_search_contition method default behavior of the ajax-datatables-rails gem # now the method checks if the column is the created_at and generate a custom SQL to parse @@ -361,7 +367,7 @@ class SampleDatatable < AjaxDatatablesRails::Base end def sorting_by_custom_column - params[:order].values[0]["column"].to_i > 6 + sort_column(params[:order].values[0]) == 'SampleCustomField.value' end # Escapes special characters in search query @@ -371,4 +377,23 @@ class SampleDatatable < AjaxDatatablesRails::Base params[:search][:value]) if params[:search] .present? end + + def new_sort_column(item) + coli = item[:column].to_i - 1 + model, column = sortable_columns[sortable_displayed_columns[coli].to_i] + .split('.') + col = [model.constantize.table_name, column].join('.') + end + + def generate_sortable_displayed_columns + sort_order = SamplesTable.where(user: @user, + organization: @organization) + .pluck(:status) + .first['ColReorder'] + + sort_order.shift + sort_order.map! { |i| (i.to_i - 1).to_s } + + @sortable_displayed_columns = sort_order + end end From a5a8bbf27a6aeb3d1edd52425b82de53fd463df8 Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 21 Dec 2016 15:35:05 +0100 Subject: [PATCH 2/3] fixed assigned column bug --- app/assets/javascripts/samples/sample_datatable.js.erb | 7 ------- app/datatables/sample_datatable.rb | 2 ++ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js.erb b/app/assets/javascripts/samples/sample_datatable.js.erb index 5dd37df73..ba911e63e 100644 --- a/app/assets/javascripts/samples/sample_datatable.js.erb +++ b/app/assets/javascripts/samples/sample_datatable.js.erb @@ -220,13 +220,6 @@ table = dataTableInit(); // Enables noSearchHidden plugin $.fn.dataTable.defaults.noSearchHidden = true; -// update datatable step on colReorder -(function() { - table.on('column-reorder', function() { - table.state.save(); - }); -})(); - // Updates "Select all" control in a data table function updateDataTableSelectAllCtrl(table) { var $table = table.table().node(); diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index ec39c41d8..ad185182e 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -382,6 +382,8 @@ class SampleDatatable < AjaxDatatablesRails::Base coli = item[:column].to_i - 1 model, column = sortable_columns[sortable_displayed_columns[coli].to_i] .split('.') + + return model if model == ASSIGNED_SORT_COL col = [model.constantize.table_name, column].join('.') end From 01849a46dbfa6a1fdb9d68143b33460e27870aad Mon Sep 17 00:00:00 2001 From: zmagod Date: Wed, 21 Dec 2016 15:40:07 +0100 Subject: [PATCH 3/3] removed unneeded lines --- app/datatables/sample_datatable.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index ad185182e..90af5ec62 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -221,11 +221,9 @@ class SampleDatatable < AjaxDatatablesRails::Base if @my_module then # Depending on the sort, order nulls first or # nulls last on sample_my_modules association - records.order("sample_my_modules.id NULLS #{sort_null_direction(params[:order].values[0])}") elsif @experiment # A very elegant solution to sort assigned samples at a experiment level - # grabs the ids of samples which has a modules that belongs to this project assigned = Sample .joins('LEFT OUTER JOIN "sample_my_modules" ON "sample_my_modules"."sample_id" = "samples"."id"')