scinote-web/app/models/samples_table.rb

57 lines
2 KiB
Ruby
Raw Normal View History

2017-06-23 21:19:08 +08:00
class SamplesTable < ApplicationRecord
2017-01-24 23:34:21 +08:00
validates :user, :team, presence: true
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
scope :find_status,
2017-01-24 23:34:21 +08:00
->(user, team) { where(user: user, team: team).pluck(:status) }
2016-12-08 21:41:35 +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)
team_status = samples_table.first['status']
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|
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)
else
index
end
end
2017-01-24 23:34:21 +08:00
team_status['ColReorder'].delete(column_index)
team_status['ColReorder'].map! do |index|
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::
SAMPLES_TABLE_DEFAULT_STATE['columns'].first
2017-01-24 23:34:21 +08:00
team_status['ColReorder'].insert(2, index)
end
2017-01-24 23:34:21 +08:00
samples_table.first.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
end