Commit 9a8394c5 by Ivan Lan

feat: 使用 value_handler 替换直接取值

parent 6386eaf8
......@@ -62,8 +62,8 @@ module TalltyImportExport
export_workbook workbook, records, **options
end
file_path = File.join(Rails.root, 'public', 'export')
# file_path = File.join('/Users/mushu/', 'export')
# file_path = File.join(Rails.root, 'public', 'export')
file_path = File.join('/Users/mushu/', 'export')
FileUtils.mkdir_p(file_path) unless Dir.exist?(file_path)
file_name = "#{Time.now.strftime('%Y%m%d%H%M%S')}#{@filename}.xlsx"
file_path_with_name = File.join(file_path, file_name)
......@@ -184,7 +184,7 @@ module TalltyImportExport
end
# 处理一个记录的数据
def handle_data record, header, index=0
def handle_data record, header, index=0, **opts
data =
if header[:key] == '_index'
index
......@@ -197,9 +197,9 @@ module TalltyImportExport
else
try_method(record, header[:key], prefix: [header[:prefix], header[:json]].compact.join('.'))
end
data = handle_format(data, header)
data = handle_data_type(data)
data = handle_select(data, header)
data = handle_format(data, header, **opts)
data = handle_data_type(data, **opts)
data = handle_select(data, header, **opts)
rescue
''
end
......@@ -215,7 +215,7 @@ module TalltyImportExport
end
# 根据数据类型 attr_type 进行数据的格式化
def handle_format data, header
def handle_format data, header, **opts
case header[:attr_type].to_s
when 'string'
data.to_s
......@@ -228,7 +228,7 @@ module TalltyImportExport
end
end
def handle_data_type data
def handle_data_type data, **opts
if data.is_a?(Time)
data.in_time_zone.strftime('%F %H:%M')
elsif data.is_a?(Date)
......@@ -237,7 +237,7 @@ module TalltyImportExport
'是'
elsif data.is_a?(FalseClass)
'否'
elsif data.is_a?(Array)
elsif data.is_a?(Array) && !opts[:keep_array]
if data.first.is_a?(Hash) && data.first&.dig('url').present?
# 文件array
data.map do |item|
......@@ -251,7 +251,7 @@ module TalltyImportExport
end
end
def handle_select data, header
def handle_select data, header, **opts
if header[:select].present?
select_option = header[:select].find { |option| option[:value].to_s == data.to_s }
select_option.present? ? select_option[:label] : data
......
......@@ -63,10 +63,10 @@ module TalltyImportExport
records.each do |record|
value_living_alone_col_index_to_value_count = {}
payload = TalltyImportExport::ExportPayload.new(record, header: header_obj) do |payload, header|
payload = TalltyImportExport::ExportPayload.new(record, header: header_obj) do |payload, header, **opts|
_data = header[:source] ?
handle_data(association_record, header, index) :
handle_data(payload, header, index)
handle_data(association_record, header, index, **opts) :
handle_data(payload, header, index, **opts)
end
......
......@@ -42,8 +42,8 @@ class TalltyImportExport::ExportPayload
# 填充各个矩阵至等高
detached_matrixes = matrixes.map do |ragged_col|
col = ragged_col.uniq
while col.count < @max_matrix_height && col[0]
col.push(col[0].count.times.to_a)
while col.count < @max_matrix_height
col.push((col[0].try(:count) || 1).times.to_a)
end
Matrix[*col]
end
......
......@@ -44,8 +44,8 @@ class TalltyImportExport::ExportPayload::Cell
def divise
if @header.children.present? && @value.value
new_cells = @header.children.map do |child_header|
if (child_header.children.present?)
val = @value.value[child_header.key.to_sym]
val = @value_handler.call(@value.value, child_header.as_json.symbolize_keys, keep_array: true)
if (child_header.children.present? && val.is_a?(Array))
val.map do |val|
cell = TalltyImportExport::ExportPayload::Cell.new(
child_header,
......
......@@ -9,8 +9,9 @@ class TalltyImportExport::ExportPayload::CellColumn
@cell_clusters = []
@value_handler = value_handler
if header.children && @value.value
@cell_clusters = (@value.value[header.key.to_sym] || []).map do |val|
val = @value_handler.call(@value.value, @header.as_json.symbolize_keys, keep_array: true)
if header.children && val.is_a?(Array)
@cell_clusters = val.map do |val|
TalltyImportExport::ExportPayload::Cell.new(
@header,
TalltyImportExport::ExportPayload::Value.new(val, @value.chain),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment