tallty_import_export 1.0.28 → 1.0.32

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d689c40b8f11354501a465563d14f5ab1060137fb2221180db05c8a18fe2b0d
4
- data.tar.gz: 9cbd15c9a1eae1a0e036dd4557fb5197e3f595067b035dd01119167becee6d3c
3
+ metadata.gz: fc07ae6791e3abfee248a3dd1413dad877cdc1e262bf0b93f3d79d73ba37e22e
4
+ data.tar.gz: b90999b8e70f6d3ecdf96ecfa2eb086ac2ff0a99abeaec0e11f45f887fc5ce14
5
5
  SHA512:
6
- metadata.gz: a11b8561c856588abf871fe6cac330e3a9412e468c6ff2d3480be75b8976b22a7037a655c51df7c884a367d9efa7c8b362c1c46ca3a1e5824f8777ccf0bb1204
7
- data.tar.gz: d178c5d5ae84107a04862e7b5b929ba8546776b1400ed79bde6c518f656d6ac7c573bfaaad3d60b499ec18d1cf9df3a09962d0669c251acbc24f905939ab884e
6
+ metadata.gz: fa462f47d3f0b4b09d3a759a9c64b3b3e358d4398e2e7f6667cd929dd8d0f7b5b825ea7428a8c8d6c0d26c068399e9745fc8d3137619ebc30810f8a2dc338958
7
+ data.tar.gz: 7fd7387e232a4b5be306f4e1c9d5b5836ab257e78b9ed45cc3092ce4e85201a91323c9cffda041f356951f59aa02bcda3c7ff36ce8834e970b9f02d00098f0ac
@@ -29,6 +29,7 @@ module TalltyImportExport
29
29
  # json: model_payload,代表存储在某一个列中
30
30
  # select: [{ label: '已报备', value: 'submitted'}, ...],需要转换的枚举类型
31
31
  # source: true,如果source为true,代表从association_record 进行属性查询
32
+ # proc: proc或者lamda,支持call,传入 record 和 context
32
33
 
33
34
  def export_xlsx records, **options
34
35
  records = with_scope records
@@ -174,6 +175,8 @@ module TalltyImportExport
174
175
  try_chain(record, header[:chain])
175
176
  elsif header[:json]
176
177
  record.send(header[:json])[header[:key]]
178
+ elsif header[:proc] && header[:proc].respond_to?(:call)
179
+ header[:proc].call(record)
177
180
  else
178
181
  try_method(record, header[:key])
179
182
  end
@@ -16,6 +16,8 @@ module TalltyImportExport
16
16
  # primary_key: 是否是主键
17
17
  # json: model_payload,代表存储在某一个列中
18
18
  # finder: 执行代码,可以查找到相关的对象
19
+ # proc: proc或者lamda,支持call,传入 val 和 context
20
+ # skip: 用来综合使用的数据,但是不导入
19
21
 
20
22
  # xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
21
23
  def import_xlsx xlsx_file, associations, **options
@@ -54,7 +56,9 @@ module TalltyImportExport
54
56
  options = import_options.merge(options).with_indifferent_access
55
57
  @headers = options.delete(:headers) || import_headers
56
58
  @primary_keys = options.delete(:primary_keys) || @headers.map { |header| header[:primary_key] ? header[:key].to_sym : nil }.compact
59
+ @skip_keys = options.delete(:skip_keys) || @headers.map { |header| header[:skip] ? header[:key].to_sym : nil }.compact
57
60
  @params = options
61
+ context.params = @params
58
62
 
59
63
  @excel_hash = @headers.reduce({}) do |h, header|
60
64
  h[header[:key].to_sym] = header[:name]
@@ -67,6 +71,7 @@ module TalltyImportExport
67
71
  def process_line_info line_info, associations
68
72
  # 去除空行内容
69
73
  return unless line_info.values.any?(&:present?)
74
+ context.line_info = line_info
70
75
  # 转换处理导入的数据格式
71
76
  line_info = convert_data(line_info)
72
77
 
@@ -88,6 +93,8 @@ module TalltyImportExport
88
93
  if header[:json]
89
94
  h[header[:json]] ||= {}
90
95
  h[header[:json]][k] = val
96
+ elsif header[:proc] && header[:proc].respond_to?(:call)
97
+ h[k.to_sym] = header[:proc].call(val, context)
91
98
  elsif header[:finder]
92
99
  # $SAFE = 2
93
100
  h[k.to_sym] = eval header[:finder]
@@ -103,8 +110,19 @@ module TalltyImportExport
103
110
  true
104
111
  end
105
112
 
113
+ # TODO: 这里,对于import_headers,后面还是不要传参数了
114
+ # 需要合并proc,前端没有办法把proc传过来
106
115
  def import_headers_result
107
- @headers ||= import_headers&.with_indifferent_access
116
+ import_header_hash = import_headers.to_h { |header| [header.with_indifferent_access[:key], header] }.with_indifferent_access
117
+ @headers.map do |header|
118
+ key = header[:key]
119
+ if import_header_hash.fetch(key)&.fetch(:proc).present?
120
+ header[:proc] = import_header_hash.fetch(key)&.fetch(:proc)
121
+ end
122
+ header
123
+ end
124
+ rescue
125
+ @headers
108
126
  end
109
127
 
110
128
  def import_options
@@ -147,9 +165,9 @@ module TalltyImportExport
147
165
  def import_record line_info, associations
148
166
  if primary_keys.present?
149
167
  _record = associations.find_or_initialize_by(line_info.clone.extract!(*primary_keys))
150
- _record.update!(line_info.clone.except!(*primary_keys))
168
+ _record.update!(line_info.clone.except!(*primary_keys, *@skip_keys))
151
169
  else
152
- associations.create!(line_info)
170
+ associations.create!(line_info.clone.except!(*@skip_keys))
153
171
  end
154
172
  end
155
173
  end
@@ -1,3 +1,3 @@
1
1
  module TalltyImportExport
2
- VERSION = "1.0.28"
2
+ VERSION = "1.0.32"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tallty_import_export
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.28
4
+ version: 1.0.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - liyijie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-08 00:00:00.000000000 Z
11
+ date: 2021-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zip-zip