ntq_excelsior 0.4.0 → 0.5.0
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/README.md +37 -2
- data/lib/ntq_excelsior/importer.rb +54 -8
- data/lib/ntq_excelsior/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5a967c15c3f10e6a7e22795e99b73d9be8e2ba2c98ea756672f587bf2c0c348
|
4
|
+
data.tar.gz: 6a90997fc04c6a6b7a45cc38d27be78be86266c90dec229427fb71f72f556adc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d26fe0bd71a850e06cc17941700c4d9b0abe6f1b7d329d3bc74e872b1cd138f359d8357a98e67278f63be585073ae46dc3e70de83dde800a5bf5cc22073960fa
|
7
|
+
data.tar.gz: be8aa18475f1a4520ca5ad4aa22eb6b06bb98c564a6ef7c26d35a95b8650055e5bddc745a5ba24684f95ffc6288157d37a288ed9be6d076ede64f2337ac6acf1
|
data/README.md
CHANGED
@@ -87,9 +87,39 @@ send_data stream, type: 'application/xlsx', filename: "filename.xlsx"
|
|
87
87
|
```ruby
|
88
88
|
class UserImporter < NtqExcelsior::Importer
|
89
89
|
|
90
|
-
model_klass User
|
90
|
+
model_klass "User"
|
91
|
+
|
92
|
+
primary_key :email
|
93
|
+
|
94
|
+
# autosave false
|
95
|
+
|
96
|
+
structure [{
|
97
|
+
header: "Email",
|
98
|
+
description: "Email de l'utilisateur a créer ou modifier",
|
99
|
+
required: true,
|
100
|
+
},
|
101
|
+
{
|
102
|
+
header: "Actif",
|
103
|
+
description: "Utilisateur activé",
|
104
|
+
required: true,
|
105
|
+
values: [
|
106
|
+
{
|
107
|
+
header: "True",
|
108
|
+
description: "Activé",
|
109
|
+
}, {
|
110
|
+
header: "False",
|
111
|
+
description: "Inactivé",
|
112
|
+
}
|
113
|
+
]
|
114
|
+
}, {
|
115
|
+
header: "Prénom",
|
116
|
+
required: true,
|
117
|
+
}, {
|
118
|
+
header: "Nom",
|
119
|
+
required: true,
|
120
|
+
}]
|
91
121
|
|
92
|
-
|
122
|
+
sample_file "/import_samples/users.xlsx"
|
93
123
|
|
94
124
|
schema({
|
95
125
|
email: 'Email',
|
@@ -112,6 +142,11 @@ class UserImporter < NtqExcelsior::Importer
|
|
112
142
|
end
|
113
143
|
end
|
114
144
|
end
|
145
|
+
|
146
|
+
importer = UserImporter.new
|
147
|
+
importer.file = file
|
148
|
+
result = importer.import
|
149
|
+
# { success_count: 2, error_lines: [] }
|
115
150
|
```
|
116
151
|
|
117
152
|
## Development
|
@@ -7,6 +7,14 @@ module NtqExcelsior
|
|
7
7
|
|
8
8
|
class << self
|
9
9
|
|
10
|
+
def autosave(value = nil)
|
11
|
+
@autosave ||= value
|
12
|
+
end
|
13
|
+
|
14
|
+
def autoset(value = nil)
|
15
|
+
@autoset ||= value
|
16
|
+
end
|
17
|
+
|
10
18
|
def spreadsheet_options(value = nil)
|
11
19
|
@spreadsheet_options ||= value
|
12
20
|
end
|
@@ -61,10 +69,10 @@ module NtqExcelsior
|
|
61
69
|
spreadsheet.sheet(spreadsheet.sheets[0]).parse(header_search: required_headers)
|
62
70
|
end
|
63
71
|
|
64
|
-
def detect_header_scheme
|
72
|
+
def detect_header_scheme
|
65
73
|
return @header_scheme if @header_scheme
|
66
74
|
@header_scheme = {}
|
67
|
-
l =
|
75
|
+
l = spreadsheet_data[0].dup
|
68
76
|
|
69
77
|
self.class.schema.each do |field, column_config|
|
70
78
|
header = column_config.is_a?(Hash) ? column_config[:header] : column_config
|
@@ -84,7 +92,7 @@ module NtqExcelsior
|
|
84
92
|
def parse_line(line)
|
85
93
|
parsed_line = {}
|
86
94
|
line.each do |header, value|
|
87
|
-
header_scheme = detect_header_scheme
|
95
|
+
header_scheme = detect_header_scheme
|
88
96
|
if header.to_s == self.class.primary_key.to_s
|
89
97
|
parsed_line[self.class.primary_key] = value
|
90
98
|
next
|
@@ -109,20 +117,58 @@ module NtqExcelsior
|
|
109
117
|
# id for default query in model
|
110
118
|
# line in case an override is needed to find correct record
|
111
119
|
def find_or_initialize_record(line)
|
112
|
-
|
120
|
+
return nil unless self.class.primary_key && self.class.model_klass
|
121
|
+
|
122
|
+
if line[self.class.primary_key.to_sym].present?
|
123
|
+
if self.class.primary_key.to_sym == :id
|
124
|
+
record = self.class.model_klass.constantize.find_by id: line[self.class.primary_key.to_sym]
|
125
|
+
else
|
126
|
+
record = self.class.model_klass.constantize.find_or_initialize_by("#{self.class.primary_key}": line[self.class.primary_key.to_sym])
|
127
|
+
end
|
128
|
+
end
|
129
|
+
record = self.class.model_klass.constantize.new unless record
|
130
|
+
record
|
131
|
+
end
|
113
132
|
|
114
|
-
|
133
|
+
def record_attributes(record)
|
134
|
+
return @record_attributes if @record_attributes
|
135
|
+
|
136
|
+
@record_attributes = self.class.schema.keys.select{|k| k.to_sym != :id && record.respond_to?(:"#{k}=") }
|
137
|
+
end
|
138
|
+
|
139
|
+
def set_record_fields(record, line)
|
140
|
+
attributes_to_set = record_attributes(record)
|
141
|
+
attributes_to_set.each do |attribute|
|
142
|
+
record.send(:"#{attribute}=", line[attribute])
|
143
|
+
end
|
144
|
+
record
|
115
145
|
end
|
116
146
|
|
117
147
|
def import_line(line, save: true)
|
118
148
|
record = find_or_initialize_record(line)
|
149
|
+
@success = false
|
150
|
+
@action = nil
|
151
|
+
@errors = []
|
152
|
+
|
153
|
+
if (!self.class.autoset.nil? || self.class.autoset)
|
154
|
+
record = set_record_fields(record, line)
|
155
|
+
end
|
119
156
|
|
120
157
|
yield(record, line) if block_given?
|
121
158
|
|
122
|
-
|
123
|
-
|
159
|
+
if (self.class.autosave.nil? || self.class.autosave)
|
160
|
+
@action = record.persisted? ? 'update' : 'create'
|
161
|
+
if save
|
162
|
+
@success = record.save
|
163
|
+
else
|
164
|
+
@success = record.valid?
|
165
|
+
end
|
166
|
+
@errors = record.errors.full_messages.concat(@errors) if record.errors.any?
|
167
|
+
end
|
168
|
+
|
169
|
+
return { status: :success, action: @action } if @success
|
124
170
|
|
125
|
-
return { status: :error, errors:
|
171
|
+
return { status: :error, errors: @errors.join(", ") }
|
126
172
|
end
|
127
173
|
|
128
174
|
def import(save: true, status_tracker: nil)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ntq_excelsior
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: caxlsx
|