2016-12-05 18:08:33 +08:00
|
|
|
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) }
|
2016-12-08 21:41:35 +08:00
|
|
|
|
|
|
|
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
|
2016-12-09 22:21:26 +08:00
|
|
|
org_status['ColReorder'] << index.to_s
|
2016-12-08 21:41:35 +08:00
|
|
|
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
|
2016-12-05 18:08:33 +08:00
|
|
|
end
|