2019-01-16 15:00:01 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-16 15:37:05 +08:00
|
|
|
require 'fileutils'
|
|
|
|
|
2019-01-16 15:00:01 +08:00
|
|
|
module ModelExporters
|
|
|
|
class ModelExporter
|
2019-02-12 17:56:15 +08:00
|
|
|
attr_accessor :assets_to_copy
|
|
|
|
attr_accessor :tiny_mce_assets_to_copy
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@assets_to_copy = []
|
|
|
|
@tiny_mce_assets_to_copy = []
|
|
|
|
end
|
|
|
|
|
2019-01-16 15:00:01 +08:00
|
|
|
def copy_files(assets, attachment_name, dir_name)
|
|
|
|
assets.flatten.each do |a|
|
2019-07-26 18:40:36 +08:00
|
|
|
next unless a.public_send(attachment_name).attached?
|
2019-01-16 15:00:01 +08:00
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
dir = FileUtils.mkdir_p(File.join(dir_name, a.id.to_s)).first
|
2019-07-26 18:40:36 +08:00
|
|
|
|
|
|
|
tempfile = Tempfile.new
|
|
|
|
tempfile.binmode
|
|
|
|
a.public_send(attachment_name).blob.download { |chunk| tempfile.write(chunk) }
|
|
|
|
tempfile.flush
|
|
|
|
tempfile.rewind
|
|
|
|
FileUtils.cp(
|
|
|
|
tempfile.path,
|
|
|
|
File.join(dir, a.file_name)
|
|
|
|
)
|
|
|
|
tempfile.close!
|
2019-01-16 15:00:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_to_dir
|
|
|
|
raise NotImplementedError, '#export_to_dir method not implemented.'
|
|
|
|
end
|
2019-02-12 17:56:15 +08:00
|
|
|
|
2019-02-12 20:38:59 +08:00
|
|
|
def protocol(protocol)
|
|
|
|
{
|
|
|
|
protocol: protocol,
|
|
|
|
protocol_protocol_keywords: protocol.protocol_protocol_keywords,
|
|
|
|
steps: protocol.steps.map { |s| step(s) }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def step(step)
|
|
|
|
@assets_to_copy.push(step.assets.to_a) if step.assets.present?
|
|
|
|
{
|
|
|
|
step: step,
|
|
|
|
checklists: step.checklists.map { |c| checklist(c) },
|
|
|
|
step_comments: step.step_comments,
|
|
|
|
step_assets: step.step_assets,
|
2019-07-26 18:40:36 +08:00
|
|
|
assets: step.assets.map { |a| assets_data(a) },
|
2019-02-12 20:38:59 +08:00
|
|
|
step_tables: step.step_tables,
|
|
|
|
tables: step.tables.map { |t| table(t) }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-26 18:40:36 +08:00
|
|
|
def assets_data(asset)
|
|
|
|
return unless asset.file.attached?
|
|
|
|
|
|
|
|
{
|
|
|
|
asset: asset,
|
|
|
|
asset_blob: asset.file.blob
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-02-12 17:56:15 +08:00
|
|
|
def checklist(checklist)
|
|
|
|
{
|
|
|
|
checklist: checklist,
|
|
|
|
checklist_items: checklist.checklist_items
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def table(table)
|
|
|
|
return {} if table.nil?
|
|
|
|
|
|
|
|
table_json = table.as_json(except: %i(contents data_vector))
|
|
|
|
table_json['contents'] = Base64.encode64(table.contents)
|
|
|
|
table_json['data_vector'] = Base64.encode64(table.data_vector)
|
|
|
|
table_json
|
|
|
|
end
|
2019-01-16 15:00:01 +08:00
|
|
|
end
|
|
|
|
end
|