Commit d0a65122 by liyijie

fix: 解决try_chain报错

parent 3fb7db33
......@@ -2,7 +2,7 @@ module TalltyImportExport
class Export
attr_reader :klass, :context
def initialize klass
def initialize(klass)
@klass = klass
@context = Context.new({})
end
......@@ -34,7 +34,7 @@ module TalltyImportExport
# proc: proc或者lamda,支持call,传入 record 和 context
# list: 对于list布局的,进行嵌套,生成子表格
def export_xlsx records, **options
def export_xlsx(records, **options)
records = with_scope records
process_options(options)
......@@ -46,14 +46,16 @@ module TalltyImportExport
if @group_by.present?
if records.is_a?(Array)
records.group_by { |record| record.send(@group_by)}.each do |key, group_records|
records.group_by { |record| record.send(@group_by) }.each do |key, group_records|
next unless key.present?
@group_key = key
export_workbook workbook, group_records, **options
end
else
records.group(@group_by).count.keys.each do |key|
next unless key.present?
@group_key = key
export_workbook workbook, records.ransack("#{@group_where}" => key).result, **options
end
......@@ -72,16 +74,16 @@ module TalltyImportExport
end
end
def export_workbook workbook, association_records, **options
def export_workbook(workbook, association_records, **options)
# excel导出样式
alignment = { vertical: :center, horizontal: :center }
border = { color: '969696', style: :thin }
title1 = workbook.styles.add_style(alignment: alignment, border: border, sz: 12, b: true)
title2 = workbook.styles.add_style(alignment: alignment, border: border, bg_color: "2a5caa", sz: 12, fg_color: "fffffb")
title2 = workbook.styles.add_style(alignment: alignment, border: border, bg_color: '2a5caa', sz: 12, fg_color: 'fffffb')
title3 = workbook.styles.add_style(alignment: alignment.merge(wrap_text: true), border: border, sz: 10)
headers = export_headers_result **options
headers = export_headers_result(**options)
_sheet_name = respond_to?(:sheet_name) ? self.sheet_name : nil
_sheet_name = respond_to?(:sheet_name) ? sheet_name : nil
workbook.add_worksheet(name: _sheet_name) do |sheet|
if respond_to?(:first_header)
......@@ -90,7 +92,7 @@ module TalltyImportExport
sheet.add_row [first_header], style: title1, height: 30
end
sheet.add_row headers.map{|header| header[:name]}, style: title2, height: 25
sheet.add_row headers.map { |header| header[:name] }, style: title2, height: 25
last_row = nil
merge_column_hash = {}
......@@ -133,12 +135,12 @@ module TalltyImportExport
merge_column_hash.each do |col_index, row_arr|
row_arr.each do |arr|
sheet.merge_cells(
Axlsx::cell_r(col_index, arr.first) + ':' + Axlsx::cell_r(col_index, arr.last)
Axlsx.cell_r(col_index, arr.first) + ':' + Axlsx.cell_r(col_index, arr.last),
)
end
end
end
sheet.column_widths(*headers.map{|header| (header[:width] || @width).to_f})
sheet.column_widths(*headers.map { |header| (header[:width] || @width).to_f })
end
end
......@@ -147,7 +149,7 @@ module TalltyImportExport
{}
end
def process_options options = {}
def process_options(options = {})
options = export_options.merge(options).with_indifferent_access
@row_height ||= options.delete(:row_height) || 25
......@@ -161,30 +163,30 @@ module TalltyImportExport
context.params = @params
end
def with_scope records
def with_scope(records)
records
end
def export_headers_result **options
def export_headers_result(**options)
if @headers.present? && @group_key.blank?
headers_hash = @headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access
export_headers(**options.symbolize_keys).select do |_header|
_header.with_indifferent_access[:key].to_s.in?(headers_hash.keys)
end.map do |_header|
_header = _header.with_indifferent_access
_header.merge(headers_hash[_header[:key]].delete_if { |k, v| v.blank? })
_header.merge(headers_hash[_header[:key]].delete_if { |_k, v| v.blank? })
end
else
@headers = export_headers(**options.symbolize_keys)
end
end
def export_headers **args
def export_headers(**args)
@headers || klass.try(:headers) || klass.try(:model_headers)
end
# 处理一个记录的数据
def handle_data record, header, index=0, **opts
def handle_data(record, header, index = 0, **opts)
data =
if header[:key] == '_index'
index
......@@ -200,28 +202,36 @@ module TalltyImportExport
data = handle_format(data, header, **opts)
data = handle_data_type(data, **opts)
data = handle_select(data, header, **opts)
rescue
rescue StandardError
''
end
def try_chain record, arr
def try_chain(record, arr)
arr.reduce(record) do |r, m|
if r.is_a?(Array)
r.try(m) || r.try(:[], m.to_i)
else
r.try(:[], m) || r.try(:[], m.to_sym) || r.try(m)
begin
r.try(:[], m)
rescue StandardError
nil
end || begin
r.try(:[], m.to_sym)
rescue StandardError
nil
end || r.try(m)
end
end
end
def try_method record, method, prefix: nil
def try_method(record, method, prefix: nil)
prefix_arr = prefix.to_s.split(/\./)
arr = method.to_s.split(/\./)
try_chain record, prefix_arr + arr
end
# 根据数据类型 attr_type 进行数据的格式化
def handle_format data, header, **opts
def handle_format(data, header, **opts)
case header[:attr_type].to_s
when 'string'
data.to_s
......@@ -234,7 +244,7 @@ module TalltyImportExport
end
end
def handle_data_type data, **opts
def handle_data_type(data, **opts)
if data.is_a?(Time)
data.in_time_zone.strftime('%F %H:%M')
elsif data.is_a?(Date)
......@@ -257,7 +267,7 @@ module TalltyImportExport
end
end
def handle_select data, header, **opts
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
......
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