From e414af2fc360498be039fb1938c5e0ac602997dd Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Tue, 13 Dec 2016 16:21:17 +0100 Subject: [PATCH 1/3] Fix columns ordering when deleting custom fields [SCI-783] --- app/assets/javascripts/samples/sample_datatable.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index 362b90234..e082f29bf 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -1147,6 +1147,8 @@ function changeToEditMode() { form .on('ajax:success', function() { + // Preserve columns ordering before destroying the table + var colOrder = table.colReorder.order(); // Destroy datatable table.destroy(); @@ -1159,6 +1161,10 @@ function changeToEditMode() { // Remove column from table (=table header) & rows var th = originalHeader.find('#' + id); var index = th.index(); + var colIndex = $('#samples thead').find('#' + id).index(); + if (colIndex >= 0) { + colOrder.splice(colIndex, 1); + } th.remove(); $('#samples tbody td:nth-child(' + (index + 1) + ')').remove(); @@ -1175,6 +1181,8 @@ function changeToEditMode() { // Re-initialize datatable table = dataTableInit(); + // Restore columns ordering + table.colReorder.order(colOrder); loadColumnsNames(); // Hide modal From fa1cf1f04d0e5d9fb811b3dbcbdbfadc1706833d Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 14 Dec 2016 15:23:00 +0100 Subject: [PATCH 2/3] Better handling of columns deleting with fixing columns ordering [SCI-783] --- .../javascripts/samples/sample_datatable.js | 10 ++---- app/controllers/custom_fields_controller.rb | 5 ++- app/models/custom_field.rb | 2 +- app/models/samples_table.rb | 31 ++++++++++++++++--- .../_delete_custom_field_modal_body.html.erb | 3 +- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index e082f29bf..692abd490 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -1120,11 +1120,13 @@ function changeToEditMode() { var self = $(this); var li = self.closest('li'); var url = li.attr('data-destroy-html-url'); + var colIndex = originalHeader.find('#' + li.attr('data-id')).index(); $.ajax({ url: url, type: 'GET', dataType: 'json', + data: {column_index: colIndex}, success: function(data) { var modalBody = modal.find('.modal-body'); @@ -1147,8 +1149,6 @@ function changeToEditMode() { form .on('ajax:success', function() { - // Preserve columns ordering before destroying the table - var colOrder = table.colReorder.order(); // Destroy datatable table.destroy(); @@ -1161,10 +1161,6 @@ function changeToEditMode() { // Remove column from table (=table header) & rows var th = originalHeader.find('#' + id); var index = th.index(); - var colIndex = $('#samples thead').find('#' + id).index(); - if (colIndex >= 0) { - colOrder.splice(colIndex, 1); - } th.remove(); $('#samples tbody td:nth-child(' + (index + 1) + ')').remove(); @@ -1181,8 +1177,6 @@ function changeToEditMode() { // Re-initialize datatable table = dataTableInit(); - // Restore columns ordering - table.colReorder.order(colOrder); loadColumnsNames(); // Hide modal diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index f549e60a2..27e585664 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -53,7 +53,8 @@ class CustomFieldsController < ApplicationController format.json do render json: { html: render_to_string( - partial: 'samples/delete_custom_field_modal_body.html.erb' + partial: 'samples/delete_custom_field_modal_body.html.erb', + locals: { column_index: params[:column_index] } ) } end @@ -61,9 +62,11 @@ class CustomFieldsController < ApplicationController end def destroy + @del_custom_field = @custom_field.dup respond_to do |format| format.json do if @custom_field.destroy + SamplesTable.update_samples_table_state(@del_custom_field, params[:custom_field][:column_index]) render json: { status: :ok } else render json: { status: :unprocessable_entity } diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index f43975f57..039a0a3aa 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -18,6 +18,6 @@ class CustomField < ActiveRecord::Base after_create :update_samples_table_state def update_samples_table_state - SamplesTable.update_samples_table_state(self) + SamplesTable.update_samples_table_state(self, nil) end end diff --git a/app/models/samples_table.rb b/app/models/samples_table.rb index 40ce10181..2eb6cb147 100644 --- a/app/models/samples_table.rb +++ b/app/models/samples_table.rb @@ -7,14 +7,35 @@ class SamplesTable < ActiveRecord::Base scope :find_status, ->(org, user) { where(user: user, organization: org).pluck(:status) } - def self.update_samples_table_state(custom_field) + def self.update_samples_table_state(custom_field, column_index) samples_table = SamplesTable.where(user: custom_field.user, organization: custom_field.organization) org_status = samples_table.first['status'] - index = org_status['columns'].count - org_status['columns'][index] = SampleDatatable:: - SAMPLES_TABLE_DEFAULT_STATE['columns'].first - org_status['ColReorder'] << index.to_s + if column_index + org_status['columns'].delete(column_index) + org_status['columns'].keys.each do |index| + p index + if index.to_i > column_index.to_i + org_status['columns'][(index.to_i - 1).to_s] = + org_status['columns'].delete(index) + else + index + end + end + org_status['ColReorder'].delete(column_index) + org_status['ColReorder'].map! do |index| + if index.to_i > column_index.to_i + (index.to_i - 1).to_s + else + index + end + end + else + index = org_status['columns'].count + org_status['columns'][index] = SampleDatatable:: + SAMPLES_TABLE_DEFAULT_STATE['columns'].first + org_status['ColReorder'] << index.to_s + end samples_table.first.update(status: org_status) end diff --git a/app/views/samples/_delete_custom_field_modal_body.html.erb b/app/views/samples/_delete_custom_field_modal_body.html.erb index 0df63cde8..d0e64a4f5 100644 --- a/app/views/samples/_delete_custom_field_modal_body.html.erb +++ b/app/views/samples/_delete_custom_field_modal_body.html.erb @@ -1,4 +1,5 @@ <%= bootstrap_form_for @custom_field, url: organization_custom_field_path(@organization, @custom_field, format: :json), remote: :true, method: :delete, data: { role: "destroy-custom-field-form", id: @custom_field.id } do |f| %> + <%= f.hidden_field :column_index, value: column_index %>

<%= t("samples.modal_delete_custom_field.message", cf: @custom_field.name) %>

-<% end %> \ No newline at end of file +<% end %> From de8eed0a19b5bb9cd5798bff71917f5bf8ed8126 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 14 Dec 2016 15:27:24 +0100 Subject: [PATCH 3/3] Fix hound warning [SCI-783] --- app/controllers/custom_fields_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index 27e585664..04b64a91c 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -66,7 +66,10 @@ class CustomFieldsController < ApplicationController respond_to do |format| format.json do if @custom_field.destroy - SamplesTable.update_samples_table_state(@del_custom_field, params[:custom_field][:column_index]) + SamplesTable.update_samples_table_state( + @del_custom_field, + params[:custom_field][:column_index] + ) render json: { status: :ok } else render json: { status: :unprocessable_entity }