From 7d88e247fc7bd410acaeb888ab415432804a8c38 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Thu, 8 Dec 2016 14:41:35 +0100 Subject: [PATCH] refactoring --- .../javascripts/samples/sample_datatable.js | 34 ++++++++----------- app/datatables/sample_datatable.rb | 2 +- app/models/custom_field.rb | 12 +------ app/models/samples_table.rb | 25 ++++++++++++++ app/models/user_organization.rb | 14 +------- .../20161123161514_create_samples_tables.rb | 2 +- 6 files changed, 43 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index c77fc3e4a..19b577398 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -102,21 +102,18 @@ table = $("#samples").DataTable({ // Save correct data if (loadFirstTime == true) { - data = myData; + data = myData; } $.ajax( { - url: '/state_save/'+org+'/'+user, - data: {org: org, state: data}, - dataType: "json", - type: "POST", - success: function () {}, - error: function () {} + url: '/state_save/'+org+'/'+user, + data: {org: org, state: data}, + dataType: "json", + type: "POST" } ); loadFirstTime = false; }, stateLoadCallback: function (settings) { - var o; // Send an Ajax request to the server to get the data. Note that // this is a synchronous request since the data is expected back from the // function @@ -124,18 +121,16 @@ table = $("#samples").DataTable({ var user = $("#samples").attr("data-user-id") $.ajax( { - url: '/state_load/'+org+'/'+user, - data: {org: org}, - async: false, - dataType: "json", - type: "POST", - success: function (json) { - o = json.state; - }, - error: function (json) {} + url: '/state_load/'+org+'/'+user, + data: {org: org}, + async: false, + dataType: "json", + type: "POST", + success: function (json) { + myData = json.state; + } } ); - myData = o; - return o; + return myData }, preDrawCallback: function(settings) { animateSpinner(this); @@ -154,7 +149,6 @@ table = $("#samples").DataTable({ } }); - // Enables noSearchHidden plugin $.fn.dataTable.defaults.noSearchHidden = true diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index a578efa0e..b3b129744 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -15,7 +15,7 @@ class SampleDatatable < AjaxDatatablesRails::Base 'regex' => false, 'caseInsensitive' => true }, 'columns' => [], - 'ColReorder' => [0, 1, 2, 3, 4, 5, 6] + 'ColReorder' => [*0..6] } 7.times do SAMPLES_TABLE_DEFAULT_STATE['columns'] << { diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index f350abccc..e06ac8baa 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -18,16 +18,6 @@ class CustomField < ActiveRecord::Base after_create :update_samples_table_state def update_samples_table_state - samples_table = SamplesTable.where(user: user, - organization: organization) - org_status = samples_table.first['status'] - index = org_status['columns'].count - org_status['columns'][index] = { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } } - org_status['ColReorder'] << index - samples_table.first.update(status: org_status) + SamplesTable.update_samples_table_state(self) end end diff --git a/app/models/samples_table.rb b/app/models/samples_table.rb index 3b13bd63c..5d4f92fde 100644 --- a/app/models/samples_table.rb +++ b/app/models/samples_table.rb @@ -6,4 +6,29 @@ class SamplesTable < ActiveRecord::Base scope :find_status, ->(org, user) { where(user: user, organization: org).pluck(:status) } + + def self.update_samples_table_state(custom_field) + 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 + samples_table.first.update(status: org_status) + end + + def self.create_samples_table_state(user_org) + default_columns_num = SampleDatatable:: + SAMPLES_TABLE_DEFAULT_STATE['columns'].count + org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup + user_org.organization.custom_fields.each_with_index do |_, index| + org_status['columns'] << SampleDatatable:: + SAMPLES_TABLE_DEFAULT_STATE['columns'].first + org_status['ColReorder'] << (default_columns_num + index) + end + SamplesTable.create(user: user_org.user, + organization: user_org.organization, + status: org_status) + end end diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index 2bf94cdc0..40c38d4a4 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -17,19 +17,7 @@ class UserOrganization < ActiveRecord::Base end def create_samples_table_state - org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup - organization.custom_fields.each_with_index do |_, index| - org_status['columns'] << { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } } - org_status['ColReorder'] << (7 + index) - end - - SamplesTable.create(user: user, - organization: organization, - status: org_status) + SamplesTable.create_samples_table_state(self) end def destroy_associations diff --git a/db/migrate/20161123161514_create_samples_tables.rb b/db/migrate/20161123161514_create_samples_tables.rb index 413448a21..a535ab0b4 100644 --- a/db/migrate/20161123161514_create_samples_tables.rb +++ b/db/migrate/20161123161514_create_samples_tables.rb @@ -2,7 +2,7 @@ class CreateSamplesTables < ActiveRecord::Migration def change create_table :samples_tables do |t| t.jsonb :status, null: false, - default: SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE + default: SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE # Foreign keys t.references :user, null: false t.references :organization, null: false