tallty_import_export 1.0.29 → 1.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/tallty_import_export/import.rb +19 -6
- data/lib/tallty_import_export/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27824038474eaaac3d03896f311952b08a41bec424d8a5e360ee8814cf439e02
|
4
|
+
data.tar.gz: b357c47d9b2ac449b58d9bc8c15c524e3ab4cf65342ca7b1df54ef834d48636b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|