From 87dc76f4e3fa7b86a6db694ab2b546bd43a16857 Mon Sep 17 00:00:00 2001 From: Mojca Lorber Date: Wed, 7 Dec 2016 17:25:42 +0100 Subject: [PATCH] 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