tallty_import_export 1.0.29 → 1.0.33

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: 0043f0699fb0631c48a0c1dcd018ccad9cdda7dfa376c0a702bf7857338e04a5
4
- data.tar.gz: 7a8dd851866ea1bc55f87ac8ec443587e6776cfd3c52b8b76fcd9f982aeba513
3
+ metadata.gz: 27824038474eaaac3d03896f311952b08a41bec424d8a5e360ee8814cf439e02
4
+ data.tar.gz: b357c47d9b2ac449b58d9bc8c15c524e3ab4cf65342ca7b1df54ef834d48636b
5
5
  SHA512:
6
- metadata.gz: 406e6ac1fcf1623b2c0de6bd6fb415e15012e2e8f56fd9bf8ec1a552b5b15e528048ea8d9f6f5bce0b16f491941b20dd91a9d246459f4c397f891cf175a95806
7
- data.tar.gz: 6af4e08406d4092107d372f0e8eaa6e5305dff67b9e5db7620151f55fb3b2f7ab3661817187ef4b92fbc85b32d030cd96c33f3c153037683d97fbc2839e7e317
6
+ metadata.gz: 46b8963859ceac6a7cfaaf734aa5a32421f0d8f20800db4eaef6b21aa3a54c4d892ffa5d2f42c31224d50cff958d7acb661777c877f8bb13e790cdb009b9edb2
7
+ data.tar.gz: 0c8f53e6a57eace206218376aa038f589bedc80fe4e1043ec2a85f238150216a888152942e0a68216d2d80d7b4c191fc845e16b0483b264f35e80c9248109c33
@@ -17,6 +17,7 @@ module TalltyImportExport
17
17
  # json: model_payload,代表存储在某一个列中
18
18
  # finder: 执行代码,可以查找到相关的对象
19
19
  # proc: proc或者lamda,支持call,传入 val 和 context
20
+ # skip: 用来综合使用的数据,但是不导入
20
21
 
21
22
  # xlsx_file 为 file path or file object or TalltyImportExport::Excel.new
22
23
  def import_xlsx xlsx_file, associations, **options
@@ -26,7 +27,7 @@ module TalltyImportExport
26
27
 
27
28
  if TalltyImportExport::Excel === xlsx_file
28
29
  xlsx_file.rows.each_with_excel_hash(@excel_hash) do |line_info|
29
- process_line_info(line_info, associations)
30
+ process_line_info(line_info.with_indifferent_access, associations)
30
31
  end
31
32
  else
32
33
  file_path = xlsx_file.is_a?(String) ? xlsx_file : xlsx_file.path
@@ -34,7 +35,7 @@ module TalltyImportExport
34
35
  xlsx.each_with_pagename do |_sheetname, sheet|
35
36
  sheet.each(**@excel_hash).with_index do |line_info, index|
36
37
  next if index == 0
37
- process_line_info(line_info, associations)
38
+ process_line_info(line_info.with_indifferent_access, associations)
38
39
  end
39
40
  end
40
41
  end
@@ -43,7 +44,7 @@ module TalltyImportExport
43
44
  def import_data data, associations, **options
44
45
  process_options(options)
45
46
  TalltyImportExport::Excel::Rows.new(data).each_with_excel_hash(@excel_hash) do |line_info|
46
- process_line_info(line_info, associations)
47
+ process_line_info(line_info.with_indifferent_access, associations)
47
48
  end
48
49
  end
49
50
 
@@ -55,6 +56,7 @@ module TalltyImportExport
55
56
  options = import_options.merge(options).with_indifferent_access
56
57
  @headers = options.delete(:headers) || import_headers
57
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
58
60
  @params = options
59
61
  context.params = @params
60
62
 
@@ -108,8 +110,19 @@ module TalltyImportExport
108
110
  true
109
111
  end
110
112
 
113
+ # TODO: 这里,对于import_headers,后面还是不要传参数了
114
+ # 需要合并proc,前端没有办法把proc传过来
111
115
  def import_headers_result
112
- @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.dig(key, :proc).present?
120
+ header[:proc] = import_header_hash.dig(key, :proc)
121
+ end
122
+ header
123
+ end
124
+ rescue
125
+ @headers
113
126
  end
114
127
 
115
128
  def import_options
@@ -152,9 +165,9 @@ module TalltyImportExport
152
165
  def import_record line_info, associations
153
166
  if primary_keys.present?
154
167
  _record = associations.find_or_initialize_by(line_info.clone.extract!(*primary_keys))
155
- _record.update!(line_info.clone.except!(*primary_keys))
168
+ _record.update!(line_info.clone.except!(*primary_keys, *@skip_keys))
156
169
  else
157
- associations.create!(line_info)
170
+ associations.create!(line_info.clone.except!(*@skip_keys))
158
171
  end
159
172
  end
160
173
  end
@@ -1,3 +1,3 @@
1
1
  module TalltyImportExport
2
- VERSION = "1.0.29"
2
+ VERSION = "1.0.33"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tallty_import_export
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.29
4
+ version: 1.0.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - liyijie