refactoring

This commit is contained in:
Mojca Lorber 2016-12-08 14:41:35 +01:00
parent 227fe9c284
commit 7d88e247fc
6 changed files with 43 additions and 46 deletions

View file

@ -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

View file

@ -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'] << {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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