Commit ffce6618 by liyijie

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

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