Commit ffce6618 by liyijie

feat: 根据导入和导出的headers,需要保证原来定义的顺序和配置项不被前端覆盖掉

parent d882700f
module TalltyImportExport
class Export
attr_reader :klass
attr_reader :klass, :context
def initialize klass
@klass = klass
@context = Context.new({})
end
# [
......@@ -150,6 +151,8 @@ module TalltyImportExport
@group_where = "#{@group_by}_eq" if @group_by.present?
@headers ||= options.delete(:headers)
@each_method ||= options.delete(:each_method)
@params = options
context.params = @params
end
def with_scope records
......@@ -157,7 +160,17 @@ module TalltyImportExport
end
def export_headers_result
@headers ||= export_headers
if @headers.present?
headers_hash = @headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access
export_headers.select do |_header|
_header.with_indifferent_access[:key].to_s.in?(headers_hash.keys)
end.map do |_header|
_header.with_indifferent_access!
_header.merge(headers_hash[_header[:key]].delete_if { |k, v| v.blank? })
end
else
@headers = export_headers
end
end
def export_headers **args
......@@ -176,7 +189,7 @@ module TalltyImportExport
elsif header[:json]
record.send(header[:json])[header[:key]]
elsif header[:proc] && header[:proc].respond_to?(:call)
header[:proc].call(record)
header[:proc].call(record, context)
else
try_method(record, header[:key])
end
......
......@@ -136,13 +136,12 @@ module TalltyImportExport
# 需要合并proc,前端没有办法把proc传过来
def import_headers_result
if @headers.present?
import_header_hash = import_headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access
@headers.map do |header|
key = header[:key]
if import_header_hash.dig(key, :proc).present?
header[:proc] = import_header_hash.dig(key, :proc)
end
header
headers_hash = @headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access
import_headers.select do |_header|
_header.with_indifferent_access[:key].to_s.in?(headers_hash.keys)
end.map do |_header|
_header.with_indifferent_access!
_header.merge(headers_hash[_header[:key]].delete_if { |k, v| v.blank? })
end
else
@headers = import_headers
......
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