sp2db 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +180 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +180 -0
- data/Rakefile +10 -0
- data/lib/generators/sp2db/config_generator.rb +13 -0
- data/lib/generators/sp2db/templates/sp2db.rb +27 -0
- data/lib/sp2db/base_table.rb +238 -0
- data/lib/sp2db/client.rb +56 -0
- data/lib/sp2db/config.rb +82 -0
- data/lib/sp2db/exception_handler.rb +26 -0
- data/lib/sp2db/import_concern.rb +84 -0
- data/lib/sp2db/import_strategy.rb +157 -0
- data/lib/sp2db/logging.rb +29 -0
- data/lib/sp2db/model_table.rb +171 -0
- data/lib/sp2db/non_model_table.rb +17 -0
- data/lib/sp2db/spreadsheet.rb +19 -0
- data/lib/sp2db/version.rb +3 -0
- data/lib/sp2db.rb +55 -0
- data/lib/tasks/sp2db.rake +12 -0
- data/sp2db.gemspec +32 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fac51c488482e0d04613279cb0e38c75c2f5cfa9
|
4
|
+
data.tar.gz: fa08f175fb5b8466731420e8e86d2963484a0150
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 028cb9e83c1cbec58766f1ea0721fa63ed5f60dda8fd3b3fe821bb0da5581028c4eec68199a8ea2d9892c9ce4b0da5777de3fad6ae0209b1d06ab435699aec6d
|
7
|
+
data.tar.gz: 13c90ddfad22da59508a5a4751aec79725aa1f813f53979a97531f06ca21826d0db5ae33033a958bd6bee29ca96df27d936994b4837068773076864ef3c8937c
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at khiemns.k54@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sp2db (0.0.3)
|
5
|
+
google-api-client (~> 0.11)
|
6
|
+
google_drive (~> 2.1)
|
7
|
+
rails (~> 5.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actioncable (5.2.0)
|
13
|
+
actionpack (= 5.2.0)
|
14
|
+
nio4r (~> 2.0)
|
15
|
+
websocket-driver (>= 0.6.1)
|
16
|
+
actionmailer (5.2.0)
|
17
|
+
actionpack (= 5.2.0)
|
18
|
+
actionview (= 5.2.0)
|
19
|
+
activejob (= 5.2.0)
|
20
|
+
mail (~> 2.5, >= 2.5.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
actionpack (5.2.0)
|
23
|
+
actionview (= 5.2.0)
|
24
|
+
activesupport (= 5.2.0)
|
25
|
+
rack (~> 2.0)
|
26
|
+
rack-test (>= 0.6.3)
|
27
|
+
rails-dom-testing (~> 2.0)
|
28
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
29
|
+
actionview (5.2.0)
|
30
|
+
activesupport (= 5.2.0)
|
31
|
+
builder (~> 3.1)
|
32
|
+
erubi (~> 1.4)
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
35
|
+
activejob (5.2.0)
|
36
|
+
activesupport (= 5.2.0)
|
37
|
+
globalid (>= 0.3.6)
|
38
|
+
activemodel (5.2.0)
|
39
|
+
activesupport (= 5.2.0)
|
40
|
+
activerecord (5.2.0)
|
41
|
+
activemodel (= 5.2.0)
|
42
|
+
activesupport (= 5.2.0)
|
43
|
+
arel (>= 9.0)
|
44
|
+
activestorage (5.2.0)
|
45
|
+
actionpack (= 5.2.0)
|
46
|
+
activerecord (= 5.2.0)
|
47
|
+
marcel (~> 0.3.1)
|
48
|
+
activesupport (5.2.0)
|
49
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
50
|
+
i18n (>= 0.7, < 2)
|
51
|
+
minitest (~> 5.1)
|
52
|
+
tzinfo (~> 1.1)
|
53
|
+
addressable (2.5.2)
|
54
|
+
public_suffix (>= 2.0.2, < 4.0)
|
55
|
+
arel (9.0.0)
|
56
|
+
builder (3.2.3)
|
57
|
+
concurrent-ruby (1.0.5)
|
58
|
+
crass (1.0.4)
|
59
|
+
declarative (0.0.10)
|
60
|
+
declarative-option (0.1.0)
|
61
|
+
erubi (1.7.1)
|
62
|
+
faraday (0.15.2)
|
63
|
+
multipart-post (>= 1.2, < 3)
|
64
|
+
globalid (0.4.1)
|
65
|
+
activesupport (>= 4.2.0)
|
66
|
+
google-api-client (0.23.4)
|
67
|
+
addressable (~> 2.5, >= 2.5.1)
|
68
|
+
googleauth (>= 0.5, < 0.7.0)
|
69
|
+
httpclient (>= 2.8.1, < 3.0)
|
70
|
+
mime-types (~> 3.0)
|
71
|
+
representable (~> 3.0)
|
72
|
+
retriable (>= 2.0, < 4.0)
|
73
|
+
google_drive (2.1.3)
|
74
|
+
google-api-client (>= 0.11.0, < 1.0.0)
|
75
|
+
googleauth (>= 0.5.0, < 1.0.0)
|
76
|
+
nokogiri (>= 1.5.3, < 2.0.0)
|
77
|
+
googleauth (0.6.2)
|
78
|
+
faraday (~> 0.12)
|
79
|
+
jwt (>= 1.4, < 3.0)
|
80
|
+
logging (~> 2.0)
|
81
|
+
memoist (~> 0.12)
|
82
|
+
multi_json (~> 1.11)
|
83
|
+
os (~> 0.9)
|
84
|
+
signet (~> 0.7)
|
85
|
+
httpclient (2.8.3)
|
86
|
+
i18n (1.0.1)
|
87
|
+
concurrent-ruby (~> 1.0)
|
88
|
+
jwt (2.1.0)
|
89
|
+
little-plugger (1.1.4)
|
90
|
+
logging (2.2.2)
|
91
|
+
little-plugger (~> 1.1)
|
92
|
+
multi_json (~> 1.10)
|
93
|
+
loofah (2.2.2)
|
94
|
+
crass (~> 1.0.2)
|
95
|
+
nokogiri (>= 1.5.9)
|
96
|
+
mail (2.7.0)
|
97
|
+
mini_mime (>= 0.1.1)
|
98
|
+
marcel (0.3.2)
|
99
|
+
mimemagic (~> 0.3.2)
|
100
|
+
memoist (0.16.0)
|
101
|
+
method_source (0.9.0)
|
102
|
+
mime-types (3.1)
|
103
|
+
mime-types-data (~> 3.2015)
|
104
|
+
mime-types-data (3.2016.0521)
|
105
|
+
mimemagic (0.3.2)
|
106
|
+
mini_mime (1.0.0)
|
107
|
+
mini_portile2 (2.3.0)
|
108
|
+
minitest (5.11.3)
|
109
|
+
multi_json (1.13.1)
|
110
|
+
multipart-post (2.0.0)
|
111
|
+
nio4r (2.3.1)
|
112
|
+
nokogiri (1.8.4)
|
113
|
+
mini_portile2 (~> 2.3.0)
|
114
|
+
os (0.9.6)
|
115
|
+
public_suffix (3.0.2)
|
116
|
+
rack (2.0.5)
|
117
|
+
rack-test (1.1.0)
|
118
|
+
rack (>= 1.0, < 3)
|
119
|
+
rails (5.2.0)
|
120
|
+
actioncable (= 5.2.0)
|
121
|
+
actionmailer (= 5.2.0)
|
122
|
+
actionpack (= 5.2.0)
|
123
|
+
actionview (= 5.2.0)
|
124
|
+
activejob (= 5.2.0)
|
125
|
+
activemodel (= 5.2.0)
|
126
|
+
activerecord (= 5.2.0)
|
127
|
+
activestorage (= 5.2.0)
|
128
|
+
activesupport (= 5.2.0)
|
129
|
+
bundler (>= 1.3.0)
|
130
|
+
railties (= 5.2.0)
|
131
|
+
sprockets-rails (>= 2.0.0)
|
132
|
+
rails-dom-testing (2.0.3)
|
133
|
+
activesupport (>= 4.2.0)
|
134
|
+
nokogiri (>= 1.6)
|
135
|
+
rails-html-sanitizer (1.0.4)
|
136
|
+
loofah (~> 2.2, >= 2.2.2)
|
137
|
+
railties (5.2.0)
|
138
|
+
actionpack (= 5.2.0)
|
139
|
+
activesupport (= 5.2.0)
|
140
|
+
method_source
|
141
|
+
rake (>= 0.8.7)
|
142
|
+
thor (>= 0.18.1, < 2.0)
|
143
|
+
rake (10.5.0)
|
144
|
+
representable (3.0.4)
|
145
|
+
declarative (< 0.1.0)
|
146
|
+
declarative-option (< 0.2.0)
|
147
|
+
uber (< 0.2.0)
|
148
|
+
retriable (3.1.2)
|
149
|
+
signet (0.8.1)
|
150
|
+
addressable (~> 2.3)
|
151
|
+
faraday (~> 0.9)
|
152
|
+
jwt (>= 1.5, < 3.0)
|
153
|
+
multi_json (~> 1.10)
|
154
|
+
sprockets (3.7.2)
|
155
|
+
concurrent-ruby (~> 1.0)
|
156
|
+
rack (> 1, < 3)
|
157
|
+
sprockets-rails (3.2.1)
|
158
|
+
actionpack (>= 4.0)
|
159
|
+
activesupport (>= 4.0)
|
160
|
+
sprockets (>= 3.0.0)
|
161
|
+
thor (0.20.0)
|
162
|
+
thread_safe (0.3.6)
|
163
|
+
tzinfo (1.2.5)
|
164
|
+
thread_safe (~> 0.1)
|
165
|
+
uber (0.1.0)
|
166
|
+
websocket-driver (0.7.0)
|
167
|
+
websocket-extensions (>= 0.1.0)
|
168
|
+
websocket-extensions (0.1.3)
|
169
|
+
|
170
|
+
PLATFORMS
|
171
|
+
ruby
|
172
|
+
|
173
|
+
DEPENDENCIES
|
174
|
+
bundler (~> 1.16)
|
175
|
+
minitest (~> 5.0)
|
176
|
+
rake (~> 10.0)
|
177
|
+
sp2db!
|
178
|
+
|
179
|
+
BUNDLED WITH
|
180
|
+
1.16.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Khiêm Nguyễn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 KhiemNS
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# Sp2db
|
2
|
+
|
3
|
+
Google Spreadsheet import tool for Rails app.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'sp2db'
|
9
|
+
```
|
10
|
+
|
11
|
+
## Basic usage
|
12
|
+
|
13
|
+
### Initialized
|
14
|
+
|
15
|
+
```bash
|
16
|
+
bundle exec rails sp2db:config
|
17
|
+
```
|
18
|
+
|
19
|
+
### In model
|
20
|
+
```ruby
|
21
|
+
class ExampleModel < ApplicationRecord
|
22
|
+
|
23
|
+
include Sp2db::ImportConcern
|
24
|
+
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
### Import task
|
29
|
+
|
30
|
+
For single table or multiple tables
|
31
|
+
```bash
|
32
|
+
# Import spreadsheet to database directy
|
33
|
+
bundle exec rake sp2db:sp_to_db[table1,table2]
|
34
|
+
|
35
|
+
# Export spreadsheet to csv then import to database. Suite for data version control
|
36
|
+
bundle exec rake sp2db:sp_to_csv[table1,table2]
|
37
|
+
bundle exec rake sp2db:csv_to_db[table1,table2]
|
38
|
+
```
|
39
|
+
|
40
|
+
For all tables
|
41
|
+
```bash
|
42
|
+
bundle exec rake sp2db:sp_to_db
|
43
|
+
# Or
|
44
|
+
bundle exec rake sp2db:sp_to_csv
|
45
|
+
bundle exec rake sp2db:csv_to_db
|
46
|
+
```
|
47
|
+
|
48
|
+
## Advanced usage
|
49
|
+
|
50
|
+
### Model config
|
51
|
+
|
52
|
+
Use sp2db_options or sp2db_`option_name` to add more config to model table
|
53
|
+
|
54
|
+
Example
|
55
|
+
```
|
56
|
+
class ExampleModel < ApplicationRecord
|
57
|
+
include Sp2db::ImportConcern
|
58
|
+
|
59
|
+
sp2db_options spreadsheet_id: "ANOTHER SHEET ID",
|
60
|
+
import_strategy: :overwrite
|
61
|
+
...
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
Other options:
|
66
|
+
* find_columns: Columns to find existed record, default: [:id]
|
67
|
+
* required_columns: Columns which values must be present to be a valid row, Example: [:name, title]
|
68
|
+
* priority: table priority for import, default: 0
|
69
|
+
* import_strategy: Import strategy
|
70
|
+
* sheet_name: work sheet name
|
71
|
+
* spreadsheet_id: Spreadsheet id, use to overwrite default spreadsheet id
|
72
|
+
* data_transform: method name or lambda to tranform spreadsheet raw data to standart input
|
73
|
+
Example:
|
74
|
+
```ruby
|
75
|
+
sp2db_options data_transform: :tranform_raw_data_to_standard_method
|
76
|
+
# Or use lambda
|
77
|
+
sp2db_options data_transform: ->(raw-data, opts) {
|
78
|
+
# Logic for data tranformation
|
79
|
+
}
|
80
|
+
# Or other form
|
81
|
+
sp2db_options_data_transform do |raw_data, opts|
|
82
|
+
# Logic for data tranformation
|
83
|
+
end
|
84
|
+
```
|
85
|
+
* process_data: Use to remove invalid data or change column value before import, value: Symbol or lambda
|
86
|
+
* before_import_row: Run before each row import, value: Symbol or lambda
|
87
|
+
* after_import_row: Run after each row import, value: Symbol or lambda
|
88
|
+
* after_import_table: Run after table import, value: Symbol or lambda
|
89
|
+
|
90
|
+
### Import process and usage of hooks
|
91
|
+
|
92
|
+
* Spreadsheet raw data: [[cellA1, cellA2,...],[cellB1, cellB2],...]
|
93
|
+
|
94
|
+
⬇
|
95
|
+
|
96
|
+
* Data tranform: This step is used to tranform raw data to standard input with the first row is header and following data rows. (Use case: tranform vertical spreadsheet to horizontal spreadsheet)
|
97
|
+
|
98
|
+
⬇
|
99
|
+
|
100
|
+
* Raw data filter(private): remove columns starting with # and blank rows, check required rows(starting with "!") to remove
|
101
|
+
|
102
|
+
⬇
|
103
|
+
|
104
|
+
* Data process: Remove invalid rows, cols, change row values before importing, Output from this step will be input for file exporting
|
105
|
+
|
106
|
+
⬇
|
107
|
+
|
108
|
+
* Before row import: Run before each row importing, use case: change row data, add file for upload, ..
|
109
|
+
|
110
|
+
⬇
|
111
|
+
|
112
|
+
* After row import: Run after each row importing, use case: notification or file upload
|
113
|
+
|
114
|
+
⬇
|
115
|
+
|
116
|
+
* After table import: Run after each table importing, use case: notification or error handling
|
117
|
+
|
118
|
+
### Import strategy
|
119
|
+
|
120
|
+
Import strategy to process import, there are 4 strategies supprted by default
|
121
|
+
|
122
|
+
* truncate_all: Truncate table before importing, default
|
123
|
+
* overwrite: Overwrite when existed record found
|
124
|
+
* skip: Skip and not update when existed record found
|
125
|
+
* fill_empty: Only fill blank columns of found record
|
126
|
+
|
127
|
+
To add custom strategy
|
128
|
+
```ruby
|
129
|
+
Sp2db::ImportStrategy.add :custom_stragy do
|
130
|
+
# Then overwrite method from Sp2db::ImportStrategy::Base to define behavior
|
131
|
+
# Example for truncate all
|
132
|
+
def before_import
|
133
|
+
model.all.delete_all # Delete all record before importing
|
134
|
+
end
|
135
|
+
end
|
136
|
+
```
|
137
|
+
|
138
|
+
### Error handling while import
|
139
|
+
|
140
|
+
Data will be rollbacked when ActiveRecord::ActiveRecordError be thrown, to change this behavior change exception_handler config, there 3 options for this
|
141
|
+
* raise: raise exception when occurs, defaut
|
142
|
+
* skip: skip exception
|
143
|
+
* A lambda for customize behavior
|
144
|
+
Example
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
Sp2db.config do |conf|
|
148
|
+
...
|
149
|
+
config.exception_handler.row_import_error = -> (exception){
|
150
|
+
# Handle exeption, return true to continue or false to skip
|
151
|
+
}
|
152
|
+
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
### For non model table
|
157
|
+
|
158
|
+
Sometimes we need to export spreadsheet to file without active record model or the model is not exist
|
159
|
+
```ruby
|
160
|
+
Sp2db.config do |conf|
|
161
|
+
conf.non_model_tables = {
|
162
|
+
"table_names" => {
|
163
|
+
sheet_id: "ANOTHER SHEET ID"
|
164
|
+
...
|
165
|
+
}
|
166
|
+
}
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
## Contributing
|
171
|
+
|
172
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/khiemns54/sp2db. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
173
|
+
|
174
|
+
## License
|
175
|
+
|
176
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
177
|
+
|
178
|
+
## Code of Conduct
|
179
|
+
|
180
|
+
Everyone interacting in the Sp2db project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/khiemns54/sp2db/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Sp2db
|
2
|
+
module Generators
|
3
|
+
class ConfigGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
5
|
+
|
6
|
+
desc 'takenoko_config.rb'
|
7
|
+
|
8
|
+
def copy_config_file
|
9
|
+
template 'sp2db.rb', 'config/initializers/sp2db.rb'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Sp2db.config do |conf|
|
2
|
+
conf.credential = "path to credential file OR Json string OR Hash" # required
|
3
|
+
conf.spreadsheet_id = "SHEET ID" # Required
|
4
|
+
|
5
|
+
# Other regular optional config
|
6
|
+
|
7
|
+
# export_location: Location for exported files, default: "db/spreadsheets"
|
8
|
+
# import_strategy: Specify import strategy, default: :truncate_all, Other: :fill_empty, :skip, :overwrite
|
9
|
+
|
10
|
+
# exception_handler: Import behavior when exception occurs
|
11
|
+
# Default:
|
12
|
+
# config.exception_handler.row_import_error = :raise
|
13
|
+
# config.exception_handler.table_import_error = :raise
|
14
|
+
|
15
|
+
# download_before_import: Export spreadsheet to csv automaticaly when import to database, default: false
|
16
|
+
|
17
|
+
# non_model_tables: define non model sheet for file exporting only
|
18
|
+
# Example:
|
19
|
+
# config.non_model_tables = {
|
20
|
+
# table_names: {
|
21
|
+
# sheet_name: "sheet_name"
|
22
|
+
# }
|
23
|
+
# }
|
24
|
+
|
25
|
+
# default_find_columns: Default find column to update, default: [:id]
|
26
|
+
|
27
|
+
end
|