col_active_importer_starter 0.1.0 → 0.1.1
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 +48 -16
- data/lib/col_active_importer_starter/lib.rb +65 -10
- data/lib/col_active_importer_starter/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbc87cc0fb9f6774deb81aa67430a579307573abd5d76da037ae1c85ab066135
|
4
|
+
data.tar.gz: 33c5a66a2f5e70c00890be462dc79f885b055c2b8db3c10c94e425e5182f91f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2783cf206f6e21215126258c2428b61e994e85456f85f335660fdd0b30a08a0ffc9c5fbd552c986d9518377ae5d067afd1d4d938f30f3b1dfedea035fbb15c80
|
7
|
+
data.tar.gz: d3433a4093a35ee77142a497239d3438c4c0c2a960edc02e8106da8258048cf733ebe41d0dd0262c651a764d4c72dd91a6cc3503754c65b887009f693b65de03
|
data/README.md
CHANGED
@@ -7,9 +7,10 @@ col_active_importer_starter is a starter(or wrapper) to [active_importer - https
|
|
7
7
|
`col_active_importer_starter` makes full use of `active_importer` gem to import tabular data from spreadsheets or similar sources into Active Record models.
|
8
8
|
|
9
9
|
- The best practices of `active_importer`, such as:
|
10
|
+
- [Custom parameters · continuum/active_importer Wiki - https://github.com/continuum/active_importer/wiki/Custom-parameters](https://github.com/continuum/active_importer/wiki/Custom-parameters)
|
10
11
|
- [Events and callbacks - https://github.com/continuum/active_importer/wiki/Callbacks](https://github.com/continuum/active_importer/wiki/Callbacks).
|
11
12
|
- Introduce a optional cache to retain data in the memory
|
12
|
-
-
|
13
|
+
- Use rubyXL to write out a file with the original data and more information for result
|
13
14
|
|
14
15
|
## Installation
|
15
16
|
|
@@ -37,7 +38,7 @@ Suppose there is a ActiveRecord model `Article`:
|
|
37
38
|
|
38
39
|
```ruby
|
39
40
|
class Article < ApplicationRecord
|
40
|
-
|
41
|
+
|
41
42
|
end
|
42
43
|
```
|
43
44
|
and tabular data file `data/Articles.xlsx`
|
@@ -54,38 +55,38 @@ and tabular data file `data/Articles.xlsx`
|
|
54
55
|
|
55
56
|
class ArticleImporter < ColActiveImporterStarter::BaseImporter
|
56
57
|
imports Article
|
57
|
-
|
58
|
+
|
58
59
|
transactional
|
59
60
|
module ColumnName
|
60
61
|
Title = "Title"
|
61
62
|
Body = "Body"
|
62
63
|
end
|
63
|
-
|
64
|
+
|
64
65
|
column ColumnName::Title, :title
|
65
66
|
column ColumnName::Body, :body
|
66
|
-
|
67
|
+
|
67
68
|
def handle_fetch_model
|
68
69
|
params = {
|
69
70
|
title: row[ColumnName::Title],
|
70
71
|
}
|
71
|
-
|
72
|
+
|
72
73
|
model = Article.find_or_initialize_by(params)
|
73
|
-
|
74
|
+
|
74
75
|
model
|
75
76
|
end
|
76
|
-
|
77
|
+
|
77
78
|
fetch_model do
|
78
79
|
handle_fetch_model
|
79
80
|
end
|
80
|
-
|
81
|
+
|
81
82
|
def handle_skip_rows_if?
|
82
83
|
row[ColumnName::Title].blank?
|
83
84
|
end
|
84
|
-
|
85
|
+
|
85
86
|
skip_rows_if do
|
86
87
|
handle_skip_rows_if?
|
87
88
|
end
|
88
|
-
|
89
|
+
|
89
90
|
# ArticleImporter.execute
|
90
91
|
def self.execute(file = "#{Rails.root}/data/Articles.1.xlsx")
|
91
92
|
params = {
|
@@ -95,7 +96,7 @@ class ArticleImporter < ColActiveImporterStarter::BaseImporter
|
|
95
96
|
}
|
96
97
|
import(file, params: params)
|
97
98
|
end
|
98
|
-
|
99
|
+
|
99
100
|
private
|
100
101
|
end
|
101
102
|
```
|
@@ -103,7 +104,12 @@ end
|
|
103
104
|
2. Import data from a file.
|
104
105
|
|
105
106
|
```ruby
|
106
|
-
ArticleImporter.
|
107
|
+
ArticleImporter.import("#{Rails.root}/data/Articles.1.xlsx")
|
108
|
+
|
109
|
+
# Or use ArticleImporter instance.
|
110
|
+
# importer = ArticleImporter.new(file, {params: params(file)})
|
111
|
+
# importer.import
|
112
|
+
# puts importer.row_count
|
107
113
|
```
|
108
114
|
|
109
115
|
Or specify more arguments.
|
@@ -112,12 +118,19 @@ Or specify more arguments.
|
|
112
118
|
params = {
|
113
119
|
cache: {},
|
114
120
|
file: "#{Rails.root}/data/Articles.1.xlsx",
|
115
|
-
result_index:
|
121
|
+
result_index: 3,
|
116
122
|
}
|
117
123
|
|
118
|
-
ArticleImporter.import(file, params: params)
|
124
|
+
ArticleImporter.import(file, {params: params})
|
125
|
+
|
126
|
+
# Or use ArticleImporter instance.
|
127
|
+
# importer = ArticleImporter.new(file, {params: params(file)})
|
128
|
+
# importer.import
|
129
|
+
# puts importer.row_count
|
119
130
|
```
|
120
131
|
|
132
|
+
For more examples to see [./test/dummy/test/importers/article_importer_test.rb](./test/dummy/test/importers/article_importer_test.rb).
|
133
|
+
|
121
134
|
3. Then, check `tmp/importers` directory to find the result file.
|
122
135
|
|
123
136
|
| Title | Body | | Result ID | Result Message |
|
@@ -125,6 +138,14 @@ ArticleImporter.import(file, params: params)
|
|
125
138
|
| Article.1.title | Article.1.body | | 1 | success |
|
126
139
|
| Article.2.title | Article.2.body | | 2 | success |
|
127
140
|
|
141
|
+
## Testing
|
142
|
+
|
143
|
+
Run `rails test` to execute all test cases in `test/dummy/test` directory.
|
144
|
+
|
145
|
+
```shell
|
146
|
+
rails test test/dummy/test
|
147
|
+
```
|
148
|
+
|
128
149
|
## Inspire
|
129
150
|
|
130
151
|
Inspire by [active_importer - https://github.com/continuum/active_importer](https://github.com/continuum/active_importer).
|
@@ -146,4 +167,15 @@ The gem is available as open source under the terms of the [MIT License - https:
|
|
146
167
|
|
147
168
|
## References
|
148
169
|
|
149
|
-
[1] [
|
170
|
+
[1] [CloudoLife-Rails/col_active_importer_starter: col_active_importer_starter is a starter(or wrapper) to [active_importer. - https://github.com/CloudoLife-Rails/col_active_importer_starter](https://github.com/CloudoLife-Rails/col_active_importer_starter)
|
171
|
+
|
172
|
+
[2] [col_active_importer_starter | RubyGems.org | your community gem host - https://rubygems.org/gems/col_active_importer_starter](https://rubygems.org/gems/col_active_importer_starter)
|
173
|
+
|
174
|
+
[3] [continuum/active_importer: Define importers that load tabular data from spreadsheets or CSV files into any ActiveRecord-like ORM. - https://github.com/continuum/active_importer](https://github.com/continuum/active_importer)
|
175
|
+
|
176
|
+
[4] [weshatheleopard/rubyXL: Ruby lib for reading/writing/modifying .xlsx and .xlsm files - https://github.com/weshatheleopard/rubyXL](https://github.com/weshatheleopard/rubyXL)
|
177
|
+
|
178
|
+
[5] [rubyXL | RubyGems.org | your community gem host - https://rubygems.org/gems/rubyXL/](https://rubygems.org/gems/rubyXL/)
|
179
|
+
|
180
|
+
[6] [RubyGems.org | your community gem host - https://rubygems.org/](https://rubygems.org/)
|
181
|
+
|
@@ -41,16 +41,25 @@ module ColActiveImporterStarter
|
|
41
41
|
|
42
42
|
class BaseImporter < ActiveImporter::Base
|
43
43
|
|
44
|
+
attr_reader :start_at, :end_at, :time_consuming
|
45
|
+
|
44
46
|
def get_result_id_column_index
|
47
|
+
return -1 if params.blank?
|
48
|
+
|
45
49
|
params[ResultIndex::ID]
|
46
50
|
end
|
47
51
|
|
48
52
|
def initialize_result_exporter
|
53
|
+
return if params.blank?
|
54
|
+
|
49
55
|
exporter = RubyXL::Parser.parse(params[:file])
|
50
56
|
exporter_sheet1 = exporter[0]
|
51
57
|
|
52
58
|
header_index = @header_index - 1
|
53
59
|
column_index = get_result_id_column_index
|
60
|
+
|
61
|
+
return if invalid_column_index?(column_index)
|
62
|
+
|
54
63
|
exporter_sheet1.add_cell(header_index, (column_index += 1), ResultColumnName::ID)
|
55
64
|
exporter_sheet1.add_cell(header_index, (column_index += 1), ResultColumnName::Message)
|
56
65
|
|
@@ -59,30 +68,39 @@ module ColActiveImporterStarter
|
|
59
68
|
end
|
60
69
|
|
61
70
|
def initialize_model_cache
|
71
|
+
return if params.blank?
|
72
|
+
|
62
73
|
model_cache = params.try(:[], :model_cache) || {}
|
63
74
|
|
64
75
|
@model_cache = model_cache
|
65
76
|
end
|
66
77
|
|
67
78
|
on :import_started do
|
79
|
+
# initialize_count
|
80
|
+
|
68
81
|
initialize_result_exporter
|
69
82
|
initialize_model_cache
|
83
|
+
|
84
|
+
start_time_consuming
|
70
85
|
end
|
71
86
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
87
|
+
on :row_skipped do
|
88
|
+
# sheet1 = @sheet1
|
89
|
+
|
90
|
+
# row_index = @row_index - @header_index
|
91
|
+
# column_index = ColumnIndex::Imported
|
92
|
+
# column_index += 1
|
93
|
+
# sheet1[row_index, (column_index += 1)] = ResultColumnName::Skip
|
94
|
+
end
|
80
95
|
|
81
96
|
def handle_row_success
|
82
97
|
exporter_sheet1 = @exporter_sheet1
|
83
98
|
|
84
99
|
row_index = @row_index - @header_index
|
85
100
|
column_index = get_result_id_column_index
|
101
|
+
|
102
|
+
return if invalid_column_index?(column_index)
|
103
|
+
|
86
104
|
column_index += 1
|
87
105
|
exporter_sheet1.add_cell(row_index, (column_index += 1), ResultValue::Success)
|
88
106
|
end
|
@@ -96,6 +114,9 @@ module ColActiveImporterStarter
|
|
96
114
|
|
97
115
|
row_index = @row_index - @header_index
|
98
116
|
column_index = get_result_id_column_index
|
117
|
+
|
118
|
+
return if invalid_column_index?(column_index)
|
119
|
+
|
99
120
|
column_index += 1
|
100
121
|
exporter_sheet1.add_cell(row_index, (column_index += 1), ResultValue::Fail)
|
101
122
|
exporter_sheet1.add_cell(row_index, (column_index += 1), e.message)
|
@@ -110,6 +131,9 @@ module ColActiveImporterStarter
|
|
110
131
|
|
111
132
|
row_index = @row_index - @header_index
|
112
133
|
column_index = get_result_id_column_index
|
134
|
+
|
135
|
+
return if invalid_column_index?(column_index)
|
136
|
+
|
113
137
|
exporter_sheet1.add_cell(row_index, (column_index += 1), model&.id)
|
114
138
|
end
|
115
139
|
|
@@ -117,10 +141,11 @@ module ColActiveImporterStarter
|
|
117
141
|
handle_row_processed
|
118
142
|
end
|
119
143
|
|
120
|
-
|
121
144
|
def handle_import_finished
|
122
145
|
exporter = @exporter
|
123
146
|
|
147
|
+
return if params.blank?
|
148
|
+
|
124
149
|
file = params[:file]
|
125
150
|
|
126
151
|
extname = '.xlsx'
|
@@ -136,6 +161,36 @@ module ColActiveImporterStarter
|
|
136
161
|
|
137
162
|
on :import_finished do
|
138
163
|
handle_import_finished
|
164
|
+
|
165
|
+
end_start_time_consuming
|
166
|
+
end
|
167
|
+
|
168
|
+
def info
|
169
|
+
{
|
170
|
+
start_at: start_at,
|
171
|
+
end_at: end_at,
|
172
|
+
time_consuming: time_consuming,
|
173
|
+
|
174
|
+
row_count: row_count,
|
175
|
+
row_processed_count: row_processed_count,
|
176
|
+
row_success_count: row_success_count,
|
177
|
+
row_error_count: row_error_count,
|
178
|
+
}
|
179
|
+
end
|
180
|
+
|
181
|
+
private
|
182
|
+
|
183
|
+
def invalid_column_index?(column_index)
|
184
|
+
column_index == -1
|
185
|
+
end
|
186
|
+
|
187
|
+
def start_time_consuming
|
188
|
+
@start_at = Time.now
|
189
|
+
end
|
190
|
+
|
191
|
+
def end_start_time_consuming
|
192
|
+
@end_at = Time.now
|
193
|
+
@time_consuming = @end_at - @start_at
|
139
194
|
end
|
140
195
|
end
|
141
|
-
end
|
196
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: col_active_importer_starter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin CloudoLife
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -76,13 +76,13 @@ files:
|
|
76
76
|
- lib/col_active_importer_starter/lib.rb
|
77
77
|
- lib/col_active_importer_starter/version.rb
|
78
78
|
- lib/tasks/col_active_importer_starter_tasks.rake
|
79
|
-
homepage: https://github.com/CloudoLife-
|
79
|
+
homepage: https://github.com/CloudoLife-Rails/col_active_importer_starter
|
80
80
|
licenses: []
|
81
81
|
metadata:
|
82
82
|
allowed_push_host: https://rubygems.org
|
83
|
-
homepage_uri: https://github.com/CloudoLife-
|
84
|
-
source_code_uri: https://github.com/CloudoLife-
|
85
|
-
changelog_uri: https://github.com/CloudoLife-
|
83
|
+
homepage_uri: https://github.com/CloudoLife-Rails/col_active_importer_starter
|
84
|
+
source_code_uri: https://github.com/CloudoLife-Rails/col_active_importer_starter
|
85
|
+
changelog_uri: https://github.com/CloudoLife-Rails/col_active_importer_starter
|
86
86
|
post_install_message:
|
87
87
|
rdoc_options: []
|
88
88
|
require_paths:
|