Commit a2c2f5d0 by liyijie

feat: 导入和导出支持proc

parent 555ed997
......@@ -29,6 +29,7 @@ module TalltyImportExport
# json: model_payload,代表存储在某一个列中
# select: [{ label: '已报备', value: 'submitted'}, ...],需要转换的枚举类型
# source: true,如果source为true,代表从association_record 进行属性查询
# proc: proc或者lamda,支持call,传入 record 和 context
def export_xlsx records, **options
records = with_scope records
......@@ -174,6 +175,8 @@ module TalltyImportExport
try_chain(record, header[:chain])
elsif header[:json]
record.send(header[:json])[header[:key]]
elsif header[:proc] && header[:proc].respond_to?(:call)
header[:proc].call(record)
else
try_method(record, header[:key])
end
......
......@@ -16,6 +16,7 @@ module TalltyImportExport
# primary_key: 是否是主键
# json: model_payload,代表存储在某一个列中
# finder: 执行代码,可以查找到相关的对象
# proc: proc或者lamda,支持call,传入 val 和 context
# xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
def import_xlsx xlsx_file, associations, **options
......@@ -55,6 +56,7 @@ module TalltyImportExport
@headers = options.delete(:headers) || import_headers
@primary_keys = options.delete(:primary_keys) || @headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
@params = options
context.params = @params
@excel_hash = @headers.reduce({}) do |h, header|
h[header[:key].to_sym] = header[:name]
......@@ -67,6 +69,7 @@ module TalltyImportExport
def process_line_info line_info, associations
# 去除空行内容
return unless line_info.values.any?(&:present?)
context.line_info = line_info
# 转换处理导入的数据格式
line_info = convert_data(line_info)
......@@ -88,6 +91,8 @@ module TalltyImportExport
if header[:json]
h[header[:json]] ||= {}
h[header[:json]][k] = val
elsif header[:proc] && header[:proc].respond_to?(:call)
h[k.to_sym] = header[:proc].call(val, context)
elsif header[:finder]
# $SAFE = 2
h[k.to_sym] = eval header[:finder]
......
module TalltyImportExport
VERSION = "1.0.26"
VERSION = "1.0.29"
end
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