2019-05-08 20:53:34 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Table < ApplicationRecord
|
2016-07-21 19:11:15 +08:00
|
|
|
include SearchableModel
|
2016-09-21 21:35:23 +08:00
|
|
|
|
2017-01-17 00:11:08 +08:00
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :name,
|
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :contents,
|
2016-09-16 17:39:37 +08:00
|
|
|
presence: true,
|
2016-10-05 23:45:20 +08:00
|
|
|
length: { maximum: Constants::TABLE_JSON_MAX_SIZE_MB.megabytes }
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :created_by,
|
|
|
|
foreign_key: 'created_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: 'last_modified_by_id',
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
|
|
|
belongs_to :team, optional: true
|
2020-07-02 23:03:29 +08:00
|
|
|
has_one :step_table, inverse_of: :table, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
has_one :step, through: :step_table
|
|
|
|
|
2023-08-11 20:48:20 +08:00
|
|
|
has_one :result_table, inverse_of: :table, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
has_one :result, through: :result_table
|
|
|
|
has_many :report_elements, inverse_of: :table, dependent: :destroy
|
|
|
|
|
|
|
|
after_save :update_ts_index
|
2018-11-14 18:57:53 +08:00
|
|
|
after_save { result&.touch; step&.touch }
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2023-07-19 17:53:47 +08:00
|
|
|
def metadata
|
|
|
|
attributes['metadata'].is_a?(String) ? JSON.parse(attributes['metadata']) : attributes['metadata']
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def contents_utf_8
|
|
|
|
contents.present? ? contents.force_encoding(Encoding::UTF_8) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_ts_index
|
2017-07-04 20:35:51 +08:00
|
|
|
if saved_change_to_contents?
|
2016-02-12 23:52:43 +08:00
|
|
|
sql = "UPDATE tables " +
|
|
|
|
"SET data_vector = " +
|
|
|
|
"to_tsvector(substring(encode(contents::bytea, 'escape'), 9)) " +
|
|
|
|
"WHERE id = " + Integer(id).to_s
|
|
|
|
Table.connection.execute(sql)
|
|
|
|
end
|
|
|
|
end
|
2018-08-29 06:31:11 +08:00
|
|
|
|
|
|
|
def to_csv
|
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
data = JSON.parse(contents)['data']
|
|
|
|
CSV.generate do |csv|
|
|
|
|
data.each do |row|
|
2018-09-23 18:06:06 +08:00
|
|
|
csv << row
|
|
|
|
end
|
2018-08-29 06:31:11 +08:00
|
|
|
end
|
|
|
|
end
|
2022-08-22 20:02:19 +08:00
|
|
|
|
2023-08-23 21:05:31 +08:00
|
|
|
def duplicate(parent, user, position = nil)
|
|
|
|
case parent
|
|
|
|
when Step
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
new_table = parent.tables.create!(
|
|
|
|
name: name,
|
|
|
|
contents: contents.encode('UTF-8', 'UTF-8'),
|
|
|
|
team: parent.protocol.team,
|
|
|
|
created_by: user,
|
|
|
|
metadata: metadata,
|
|
|
|
last_modified_by: user
|
|
|
|
)
|
|
|
|
|
|
|
|
parent.step_orderable_elements.create!(
|
|
|
|
position: position || parent.step_orderable_elements.length,
|
|
|
|
orderable: new_table.step_table
|
|
|
|
)
|
|
|
|
|
|
|
|
new_table
|
|
|
|
end
|
|
|
|
when Result
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
new_table = parent.tables.create!(
|
|
|
|
name: name,
|
|
|
|
contents: contents.encode('UTF-8', 'UTF-8'),
|
|
|
|
team: parent.my_module.team,
|
|
|
|
created_by: user,
|
|
|
|
metadata: metadata,
|
|
|
|
last_modified_by: user
|
|
|
|
)
|
|
|
|
|
|
|
|
parent.result_orderable_elements.create!(
|
2023-08-30 20:54:38 +08:00
|
|
|
position: position || parent.result_orderable_elements.length,
|
2023-08-23 21:05:31 +08:00
|
|
|
orderable: new_table.result_table
|
|
|
|
)
|
|
|
|
|
|
|
|
new_table
|
|
|
|
end
|
2022-08-30 21:26:52 +08:00
|
|
|
end
|
2022-08-22 20:02:19 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|