Commit 9a8394c5 by Ivan Lan

feat: 使用 value_handler 替换直接取值

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