2017-06-23 21:19:08 +08:00
|
|
|
class SamplesTable < ApplicationRecord
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :user, inverse_of: :samples_tables, optional: true
|
|
|
|
belongs_to :team, inverse_of: :samples_tables, optional: true
|
2016-12-05 18:08:33 +08:00
|
|
|
|
2018-03-09 22:34:06 +08:00
|
|
|
validates :user, :team, presence: true
|
|
|
|
|
2018-03-13 01:07:35 +08:00
|
|
|
scope :find_status, (lambda do |user, team|
|
|
|
|
where(user: user, team: team)
|
|
|
|
.order(:id).pluck(:status).first
|
|
|
|
end)
|
2016-12-08 21:41:35 +08:00
|
|
|
|
2016-12-14 22:23:00 +08:00
|
|
|
def self.update_samples_table_state(custom_field, column_index)
|
2016-12-08 21:41:35 +08:00
|
|
|
samples_table = SamplesTable.where(user: custom_field.user,
|
2017-01-24 23:34:21 +08:00
|
|
|
team: custom_field.team)
|
2018-03-13 01:07:35 +08:00
|
|
|
.order(:id)
|
|
|
|
.first
|
|
|
|
team_status = samples_table['status']
|
2016-12-14 22:23:00 +08:00
|
|
|
if column_index
|
2016-12-15 18:20:40 +08:00
|
|
|
# delete column
|
2017-01-24 23:34:21 +08:00
|
|
|
team_status['columns'].delete(column_index)
|
|
|
|
team_status['columns'].keys.each do |index|
|
2016-12-14 22:23:00 +08:00
|
|
|
if index.to_i > column_index.to_i
|
2017-01-24 23:34:21 +08:00
|
|
|
team_status['columns'][(index.to_i - 1).to_s] =
|
|
|
|
team_status['columns'].delete(index)
|
2016-12-14 22:23:00 +08:00
|
|
|
else
|
|
|
|
index
|
|
|
|
end
|
|
|
|
end
|
2017-01-24 23:34:21 +08:00
|
|
|
team_status['ColReorder'].delete(column_index)
|
|
|
|
team_status['ColReorder'].map! do |index|
|
2016-12-14 22:23:00 +08:00
|
|
|
if index.to_i > column_index.to_i
|
|
|
|
(index.to_i - 1).to_s
|
|
|
|
else
|
|
|
|
index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2016-12-15 18:20:40 +08:00
|
|
|
# add column
|
2017-01-24 23:34:21 +08:00
|
|
|
index = team_status['columns'].count
|
|
|
|
team_status['columns'][index] = SampleDatatable::
|
2016-12-14 22:23:00 +08:00
|
|
|
SAMPLES_TABLE_DEFAULT_STATE['columns'].first
|
2017-01-24 23:34:21 +08:00
|
|
|
team_status['ColReorder'].insert(2, index)
|
2016-12-14 22:23:00 +08:00
|
|
|
end
|
2018-03-16 18:05:38 +08:00
|
|
|
samples_table.update(status: team_status)
|
2016-12-08 21:41:35 +08:00
|
|
|
end
|
|
|
|
|
2017-01-24 23:34:21 +08:00
|
|
|
def self.create_samples_table_state(user_team)
|
2016-12-08 21:41:35 +08:00
|
|
|
default_columns_num = SampleDatatable::
|
|
|
|
SAMPLES_TABLE_DEFAULT_STATE['columns'].count
|
2017-01-24 23:34:21 +08:00
|
|
|
team_status = SampleDatatable::SAMPLES_TABLE_DEFAULT_STATE.deep_dup
|
|
|
|
user_team.team.custom_fields.each_with_index do |_, index|
|
|
|
|
team_status['columns'] << SampleDatatable::
|
2016-12-08 21:41:35 +08:00
|
|
|
SAMPLES_TABLE_DEFAULT_STATE['columns'].first
|
2017-01-24 23:34:21 +08:00
|
|
|
team_status['ColReorder'] << (default_columns_num + index)
|
2016-12-08 21:41:35 +08:00
|
|
|
end
|
2017-01-24 23:34:21 +08:00
|
|
|
SamplesTable.create(user: user_team.user,
|
|
|
|
team: user_team.team,
|
|
|
|
status: team_status)
|
2016-12-08 21:41:35 +08:00
|
|
|
end
|
2016-12-05 18:08:33 +08:00
|
|
|
end
|