From 01e75fbbcc1d49dbdbdc80208f3028053bd69e65 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Mon, 5 Dec 2016 11:08:33 +0100 Subject: [PATCH 01/14] create samples table model and migration --- app/models/organization.rb | 1 + app/models/samples_table.rb | 9 +++ app/models/user.rb | 1 + .../20161123161514_create_samples_tables.rb | 79 +++++++++++++++++++ db/schema.rb | 11 +++ 5 files changed, 101 insertions(+) create mode 100644 app/models/samples_table.rb create mode 100644 db/migrate/20161123161514_create_samples_tables.rb diff --git a/app/models/organization.rb b/app/models/organization.rb index d49e49521..cf7c749b2 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -15,6 +15,7 @@ class Organization < ActiveRecord::Base has_many :user_organizations, inverse_of: :organization, dependent: :destroy has_many :users, through: :user_organizations has_many :samples, inverse_of: :organization + has_many :samples_tables, inverse_of: :organization, dependent: :destroy has_many :sample_groups, inverse_of: :organization has_many :sample_types, inverse_of: :organization has_many :logs, inverse_of: :organization diff --git a/app/models/samples_table.rb b/app/models/samples_table.rb new file mode 100644 index 000000000..3b13bd63c --- /dev/null +++ b/app/models/samples_table.rb @@ -0,0 +1,9 @@ +class SamplesTable < ActiveRecord::Base + validates :user, :organization, presence: true + + belongs_to :user, inverse_of: :samples_tables + belongs_to :organization, inverse_of: :samples_tables + + scope :find_status, + ->(org, user) { where(user: user, organization: org).pluck(:status) } +end diff --git a/app/models/user.rb b/app/models/user.rb index 46a3197f0..a2bbdc1bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -46,6 +46,7 @@ class User < ActiveRecord::Base has_many :activities, inverse_of: :user has_many :results, inverse_of: :user has_many :samples, inverse_of: :user + has_many :samples_tables, inverse_of: :user, dependent: :destroy has_many :steps, inverse_of: :user has_many :custom_fields, inverse_of: :user has_many :reports, inverse_of: :user diff --git a/db/migrate/20161123161514_create_samples_tables.rb b/db/migrate/20161123161514_create_samples_tables.rb new file mode 100644 index 000000000..0dfedf95f --- /dev/null +++ b/db/migrate/20161123161514_create_samples_tables.rb @@ -0,0 +1,79 @@ +class CreateSamplesTables < ActiveRecord::Migration + + @@def_state = { 'time' => 0, + 'start' => 0, + 'length' => 10, + 'order' => [[2, 'desc']], + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true }, + 'columns' => [{ 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }], + 'ColReorder' => [0, 1, 2, 3, 4, 5, 6] } + + def change + create_table :samples_tables do |t| + t.jsonb :status, null: false, default: @@def_state + # Foreign keys + t.references :user, null: false + t.references :organization, null: false + + t.timestamps null: false + end + add_index :samples_tables, :user_id + add_index :samples_tables, :organization_id + + User.find_each do |user| + next unless user.organizations + user.organizations.find_each do |org| + + org_status = @@def_state.deep_dup + next unless org.custom_fields + org.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: org, status: org_status) + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 69aff9902..1504e1b50 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -497,6 +497,17 @@ ActiveRecord::Schema.define(version: 20161125153600) do add_index "samples", ["sample_type_id"], name: "index_samples_on_sample_type_id", using: :btree add_index "samples", ["user_id"], name: "index_samples_on_user_id", using: :btree + create_table "samples_tables", force: :cascade do |t| + t.jsonb "status", default: {"time"=>0, "order"=>[[2, "desc"]], "start"=>0, "length"=>10, "search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "columns"=>[{"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}, {"search"=>{"regex"=>false, "smart"=>true, "search"=>"", "caseInsensitive"=>true}, "visible"=>true}], "ColReorder"=>[0, 1, 2, 3, 4, 5, 6]}, null: false + t.integer "user_id", null: false + t.integer "organization_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "samples_tables", ["organization_id"], name: "index_samples_tables_on_organization_id", using: :btree + add_index "samples_tables", ["user_id"], name: "index_samples_tables_on_user_id", using: :btree + create_table "step_assets", force: :cascade do |t| t.integer "step_id", null: false t.integer "asset_id", null: false From 849f78b4dfd2de3395b8855489b6602d410d049d Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Mon, 5 Dec 2016 11:34:36 +0100 Subject: [PATCH 02/14] implement reordering of columns in the samples table and change from local to database storage --- .../javascripts/samples/sample_datatable.js | 56 ++++++++++++++++--- app/controllers/user_samples_controller.rb | 34 +++++++++++ app/views/shared/_samples.html.erb | 1 + config/routes.rb | 11 ++++ 4 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 app/controllers/user_samples_controller.rb diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index 1058268d2..ec2bd7361 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -9,6 +9,10 @@ var currentMode = "viewMode"; var saveAction = "update"; var selectedSample; +// Helps saving correct table state +var myData; +var loadFirstTime = true; + table = $("#samples").DataTable({ order: [[2, "desc"]], dom: "R<'row'<'col-sm-9-custom toolbar'l><'col-sm-3-custom'f>>tpi", @@ -21,7 +25,7 @@ table = $("#samples").DataTable({ type: "POST" }, colReorder: { - fixedColumnsLeft: 1000000 // Disable reordering + realtime: false // Enable reordering }, columnDefs: [{ targets: 0, @@ -91,20 +95,54 @@ table = $("#samples").DataTable({ } }, stateSaveCallback: function (settings, data) { - // Set a cookie with the table state using the organization id - localStorage.setItem('datatables_state/' + $("#samples").attr("data-organization-id"), JSON.stringify(data)); + // Send an Ajax request to the server with the state object + var org = $("#samples").attr("data-organization-id") + var user = $("#samples").attr("data-user-id") + + // Save correct data + if (loadFirstTime == true) { + data = myData; + } + + $.ajax( { + url: '/state_save/'+org+'/'+user, + data: {org: org, state: data}, + dataType: "json", + type: "POST", + success: function () {}, + error: function () {} + } ); + loadFirstTime = false; }, stateLoadCallback: function (settings) { - // Load the table state for the current organization - var state = localStorage.getItem('datatables_state/' + $("#samples").attr("data-organization-id")); - if (state !== null) { - return JSON.parse(state); - } - return null; + 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 + var org = $("#samples").attr("data-organization-id") + 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) {} + } ); + myData = o; + return o; }, preDrawCallback: function(settings) { animateSpinner(this); $(".sample_info").off("click"); + }, + fnInitComplete: function(oSettings, json) { + // Reload correct column order (if you refresh page) + oSettings._colReorder.fnOrder(myData.ColReorder); } }); diff --git a/app/controllers/user_samples_controller.rb b/app/controllers/user_samples_controller.rb new file mode 100644 index 000000000..2a3a33959 --- /dev/null +++ b/app/controllers/user_samples_controller.rb @@ -0,0 +1,34 @@ +class UserSamplesController < ApplicationController + def save_samples_table_status + samples_table = SamplesTable.where(user: @current_user, + organization: params[:org]) + if samples_table + samples_table.first.update(status: params[:state]) + else + SamplesTable.create(user: @current_user, + organization: params[:org], + status: params[:state]) + end + respond_to do |format| + format.json do + render json: { + status: :ok + } + end + end + end + + def load_samples_table_status + @samples_table_state = SamplesTable.find_status(current_user, + current_organization).first + respond_to do |format| + if @samples_table_state + format.json do + render json: { + state: @samples_table_state + } + end + end + end + end +end diff --git a/app/views/shared/_samples.html.erb b/app/views/shared/_samples.html.erb index 39b584399..02780e266 100644 --- a/app/views/shared/_samples.html.erb +++ b/app/views/shared/_samples.html.erb @@ -106,6 +106,7 @@
"application#forbidden", as: "forbidden" From 332b6c58da58a2382ca7988fac2371f591ee8026 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Mon, 5 Dec 2016 11:44:47 +0100 Subject: [PATCH 03/14] fix hound --- db/migrate/20161123161514_create_samples_tables.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/db/migrate/20161123161514_create_samples_tables.rb b/db/migrate/20161123161514_create_samples_tables.rb index 0dfedf95f..f643c4235 100644 --- a/db/migrate/20161123161514_create_samples_tables.rb +++ b/db/migrate/20161123161514_create_samples_tables.rb @@ -1,5 +1,4 @@ class CreateSamplesTables < ActiveRecord::Migration - @@def_state = { 'time' => 0, 'start' => 0, 'length' => 10, @@ -60,7 +59,6 @@ class CreateSamplesTables < ActiveRecord::Migration User.find_each do |user| next unless user.organizations user.organizations.find_each do |org| - org_status = @@def_state.deep_dup next unless org.custom_fields org.custom_fields.each_with_index do |_, index| From 12088ebfd3a7bdedc7d5aefb0c6c913c3aa4a3d2 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Tue, 6 Dec 2016 09:40:28 +0100 Subject: [PATCH 04/14] add visibility functionality after refresh --- app/assets/javascripts/samples/sample_datatable.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index ec2bd7361..502246477 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -141,11 +141,17 @@ table = $("#samples").DataTable({ $(".sample_info").off("click"); }, fnInitComplete: function(oSettings, json) { - // Reload correct column order (if you refresh page) + // Reload correct column order and visibility (if you refresh page) oSettings._colReorder.fnOrder(myData.ColReorder); + for (var i = 0; i < table.columns()[0].length; i++) { + var visibility = myData.columns[i].visible; + var vis_boolean = (visibility === "true"); + table.column(i).visible(vis_boolean); + } } }); + // Enables noSearchHidden plugin $.fn.dataTable.defaults.noSearchHidden = true From 1bc0ca9e7539d9a82a4fc0d53e310eae2ca29715 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Tue, 6 Dec 2016 13:14:29 +0100 Subject: [PATCH 05/14] fix bug with first time load --- app/assets/javascripts/samples/sample_datatable.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index 502246477..c9bdcb220 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -145,8 +145,10 @@ table = $("#samples").DataTable({ oSettings._colReorder.fnOrder(myData.ColReorder); for (var i = 0; i < table.columns()[0].length; i++) { var visibility = myData.columns[i].visible; - var vis_boolean = (visibility === "true"); - table.column(i).visible(vis_boolean); + if (typeof(visibility) === "string"){ + var visibility = (visibility === "true"); + } + table.column(i).visible(visibility); } } }); From 39b6fa66fe8f3be0f4fe1827133cd439ae265a6a Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 14:14:32 +0100 Subject: [PATCH 06/14] add SamplesTable for new users --- app/models/user_organization.rb | 17 +++++++ config/initializers/constants.rb | 47 +++++++++++++++++ .../20161123161514_create_samples_tables.rb | 50 ++----------------- 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index e964e6c91..ed4a0353d 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -15,6 +15,23 @@ class UserOrganization < ActiveRecord::Base I18n.t("user_organizations.enums.role.#{role.to_s}") end + def self.create(opt) + user = opt[:user] + org = opt[:organization] + org_status = Constants::SAMPLES_TABLE_DEFAULT_STATE + org.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: org, status: org_status) + super(opt) + end + def destroy_associations # Destroy the user from all organization's projects organization.projects.each do |project| diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index b6c48d551..2fdc408d7 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -239,4 +239,51 @@ class Constants 'Isaac Sandaljian', 'Markus Rademacher' ].freeze + + SAMPLES_TABLE_DEFAULT_STATE = { + 'time' => 0, + 'start' => 0, + 'length' => 10, + 'order' => [[2, 'desc']], + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true }, + 'columns' => [{ 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }, + { 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } }], + 'ColReorder' => [0, 1, 2, 3, 4, 5, 6] + }.freeze end diff --git a/db/migrate/20161123161514_create_samples_tables.rb b/db/migrate/20161123161514_create_samples_tables.rb index f643c4235..26cd6a69e 100644 --- a/db/migrate/20161123161514_create_samples_tables.rb +++ b/db/migrate/20161123161514_create_samples_tables.rb @@ -1,52 +1,8 @@ class CreateSamplesTables < ActiveRecord::Migration - @@def_state = { 'time' => 0, - 'start' => 0, - 'length' => 10, - 'order' => [[2, 'desc']], - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true }, - 'columns' => [{ 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }], - 'ColReorder' => [0, 1, 2, 3, 4, 5, 6] } - def change create_table :samples_tables do |t| - t.jsonb :status, null: false, default: @@def_state + t.jsonb :status, null: false, + default: Constants::SAMPLES_TABLE_DEFAULT_STATE # Foreign keys t.references :user, null: false t.references :organization, null: false @@ -59,7 +15,7 @@ class CreateSamplesTables < ActiveRecord::Migration User.find_each do |user| next unless user.organizations user.organizations.find_each do |org| - org_status = @@def_state.deep_dup + org_status = Constants::SAMPLES_TABLE_DEFAULT_STATE.deep_dup next unless org.custom_fields org.custom_fields.each_with_index do |_, index| org_status['columns'] << { 'visible' => true, From 195055f9cf2fc1c9d64cedd800142035088d7c56 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 14:51:34 +0100 Subject: [PATCH 07/14] change SAMPLES_TABLE_DEFAULT_STATE --- app/datatables/sample_datatable.rb | 25 +++++++++- app/models/user_organization.rb | 2 +- config/initializers/constants.rb | 47 ------------------- .../20161123161514_create_samples_tables.rb | 4 +- 4 files changed, 27 insertions(+), 51 deletions(-) diff --git a/app/datatables/sample_datatable.rb b/app/datatables/sample_datatable.rb index 363fa84b1..a578efa0e 100644 --- a/app/datatables/sample_datatable.rb +++ b/app/datatables/sample_datatable.rb @@ -3,7 +3,30 @@ require 'active_record' class SampleDatatable < AjaxDatatablesRails::Base include SamplesHelper - ASSIGNED_SORT_COL = "assigned" + ASSIGNED_SORT_COL = 'assigned' + + SAMPLES_TABLE_DEFAULT_STATE = { + 'time' => 0, + 'start' => 0, + 'length' => 10, + 'order' => [[2, 'desc']], + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true }, + 'columns' => [], + 'ColReorder' => [0, 1, 2, 3, 4, 5, 6] + } + 7.times do + SAMPLES_TABLE_DEFAULT_STATE['columns'] << { + 'visible' => true, + 'search' => { 'search' => '', + 'smart' => true, + 'regex' => false, + 'caseInsensitive' => true } + } + end + SAMPLES_TABLE_DEFAULT_STATE.freeze def initialize(view, organization, diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index ed4a0353d..eaf7a783e 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -18,7 +18,7 @@ class UserOrganization < ActiveRecord::Base def self.create(opt) user = opt[:user] org = opt[:organization] - org_status = Constants::SAMPLES_TABLE_DEFAULT_STATE + org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup org.custom_fields.each_with_index do |_, index| org_status['columns'] << { 'visible' => true, 'search' => { 'search' => '', diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 2fdc408d7..b6c48d551 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -239,51 +239,4 @@ class Constants 'Isaac Sandaljian', 'Markus Rademacher' ].freeze - - SAMPLES_TABLE_DEFAULT_STATE = { - 'time' => 0, - 'start' => 0, - 'length' => 10, - 'order' => [[2, 'desc']], - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true }, - 'columns' => [{ 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }, - { 'visible' => true, - 'search' => { 'search' => '', - 'smart' => true, - 'regex' => false, - 'caseInsensitive' => true } }], - 'ColReorder' => [0, 1, 2, 3, 4, 5, 6] - }.freeze end diff --git a/db/migrate/20161123161514_create_samples_tables.rb b/db/migrate/20161123161514_create_samples_tables.rb index 26cd6a69e..413448a21 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: Constants::SAMPLES_TABLE_DEFAULT_STATE + default: SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE # Foreign keys t.references :user, null: false t.references :organization, null: false @@ -15,7 +15,7 @@ class CreateSamplesTables < ActiveRecord::Migration User.find_each do |user| next unless user.organizations user.organizations.find_each do |org| - org_status = Constants::SAMPLES_TABLE_DEFAULT_STATE.deep_dup + org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup next unless org.custom_fields org.custom_fields.each_with_index do |_, index| org_status['columns'] << { 'visible' => true, From 5b5342de6f6974698e493f90770ef2acebe0ecb6 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 15:02:18 +0100 Subject: [PATCH 08/14] make first column fixed --- app/assets/javascripts/samples/sample_datatable.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index c9bdcb220..c77fc3e4a 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -25,7 +25,8 @@ table = $("#samples").DataTable({ type: "POST" }, colReorder: { - realtime: false // Enable reordering + realtime: false, // Enable reordering + fixedColumnsLeft: 1 }, columnDefs: [{ targets: 0, From 228990f9da48d0eaa1056b21a8d96f7e6e74b350 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 16:37:16 +0100 Subject: [PATCH 09/14] fix samples table state, when adding new custom column --- app/models/custom_field.rb | 17 +++++++++++++++++ app/models/user_organization.rb | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 508e87116..0aaf5debe 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -14,4 +14,21 @@ class CustomField < ActiveRecord::Base foreign_key: 'last_modified_by_id', class_name: 'User' has_many :sample_custom_fields, inverse_of: :custom_field + + def self.new(opt) + user = opt[:user] + org = opt[:organization] + samples_table = SamplesTable.where(user: user, + organization: org) + 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) + super(opt) + end end diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index eaf7a783e..5b1fc990b 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -15,7 +15,7 @@ class UserOrganization < ActiveRecord::Base I18n.t("user_organizations.enums.role.#{role.to_s}") end - def self.create(opt) + def self.new(opt) user = opt[:user] org = opt[:organization] org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup From 114a17ff0f8f33f296f2035bddf5c6ff17972dd8 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 16:57:17 +0100 Subject: [PATCH 10/14] change to create --- app/models/custom_field.rb | 2 +- app/models/user_organization.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 0aaf5debe..10dd39395 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -15,7 +15,7 @@ class CustomField < ActiveRecord::Base class_name: 'User' has_many :sample_custom_fields, inverse_of: :custom_field - def self.new(opt) + def self.create(opt) user = opt[:user] org = opt[:organization] samples_table = SamplesTable.where(user: user, diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index 5b1fc990b..eaf7a783e 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -15,7 +15,7 @@ class UserOrganization < ActiveRecord::Base I18n.t("user_organizations.enums.role.#{role.to_s}") end - def self.new(opt) + def self.create(opt) user = opt[:user] org = opt[:organization] org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup From 87dc76f4e3fa7b86a6db694ab2b546bd43a16857 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 17:25:42 +0100 Subject: [PATCH 11/14] change to update samples table state after initialize --- app/models/custom_field.rb | 9 ++++----- app/models/user_organization.rb | 12 ++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 10dd39395..522037ca2 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -15,11 +15,11 @@ class CustomField < ActiveRecord::Base class_name: 'User' has_many :sample_custom_fields, inverse_of: :custom_field - def self.create(opt) - user = opt[:user] - org = opt[:organization] + after_initialize :update_samples_table_state, if: :new_record? + + def update_samples_table_state samples_table = SamplesTable.where(user: user, - organization: org) + organization: organization) org_status = samples_table.first['status'] index = org_status['columns'].count org_status['columns'][index] = { 'visible' => true, @@ -29,6 +29,5 @@ class CustomField < ActiveRecord::Base 'caseInsensitive' => true } } org_status['ColReorder'] << index samples_table.first.update(status: org_status) - super(opt) end end diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index eaf7a783e..639f76e71 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -10,16 +10,15 @@ class UserOrganization < ActiveRecord::Base belongs_to :organization, inverse_of: :user_organizations before_destroy :destroy_associations + after_initialize :create_samples_table_state, if: :new_record? def role_str I18n.t("user_organizations.enums.role.#{role.to_s}") end - def self.create(opt) - user = opt[:user] - org = opt[:organization] + def create_samples_table_state org_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup - org.custom_fields.each_with_index do |_, index| + organization.custom_fields.each_with_index do |_, index| org_status['columns'] << { 'visible' => true, 'search' => { 'search' => '', 'smart' => true, @@ -28,8 +27,9 @@ class UserOrganization < ActiveRecord::Base org_status['ColReorder'] << (7 + index) end - SamplesTable.create(user: user, organization: org, status: org_status) - super(opt) + SamplesTable.create(user: user, + organization: organization, + status: org_status) end def destroy_associations From 227fe9c284b1012b469034b14ed3862fb743f851 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 17:43:00 +0100 Subject: [PATCH 12/14] change to after_create --- app/models/custom_field.rb | 2 +- app/models/user_organization.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 522037ca2..f350abccc 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -15,7 +15,7 @@ class CustomField < ActiveRecord::Base class_name: 'User' has_many :sample_custom_fields, inverse_of: :custom_field - after_initialize :update_samples_table_state, if: :new_record? + after_create :update_samples_table_state def update_samples_table_state samples_table = SamplesTable.where(user: user, diff --git a/app/models/user_organization.rb b/app/models/user_organization.rb index 639f76e71..2bf94cdc0 100644 --- a/app/models/user_organization.rb +++ b/app/models/user_organization.rb @@ -10,7 +10,7 @@ class UserOrganization < ActiveRecord::Base belongs_to :organization, inverse_of: :user_organizations before_destroy :destroy_associations - after_initialize :create_samples_table_state, if: :new_record? + after_create :create_samples_table_state def role_str I18n.t("user_organizations.enums.role.#{role.to_s}") From 7d88e247fc7bd410acaeb888ab415432804a8c38 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Thu, 8 Dec 2016 14:41:35 +0100 Subject: [PATCH 13/14] 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 From 1b2a438ed44edf48948a4e578d06a6bce670edf8 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Thu, 8 Dec 2016 15:24:54 +0100 Subject: [PATCH 14/14] remove fixed columns --- app/assets/javascripts/samples/sample_datatable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/assets/javascripts/samples/sample_datatable.js b/app/assets/javascripts/samples/sample_datatable.js index 19b577398..8d70c69eb 100644 --- a/app/assets/javascripts/samples/sample_datatable.js +++ b/app/assets/javascripts/samples/sample_datatable.js @@ -26,7 +26,6 @@ table = $("#samples").DataTable({ }, colReorder: { realtime: false, // Enable reordering - fixedColumnsLeft: 1 }, columnDefs: [{ targets: 0,