rails_admin_import 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +143 -13
- data/app/views/rails_admin/main/import.html.haml +6 -3
- data/config/locales/README.md +3 -0
- data/config/locales/import.en.yml +2 -3
- data/lib/rails_admin_import/config.rb +4 -0
- data/lib/rails_admin_import/config/sections/import.rb +1 -1
- data/lib/rails_admin_import/formats/csv_importer.rb +3 -1
- data/lib/rails_admin_import/formats/json_importer.rb +1 -0
- data/lib/rails_admin_import/formats/xlsx_importer.rb +4 -2
- data/lib/rails_admin_import/importer.rb +29 -7
- data/lib/rails_admin_import/version.rb +1 -1
- metadata +4 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ad5c6af8848efccee2f00133a0537a41f6ceca8
|
4
|
+
data.tar.gz: 6928c129f69d8653a0bcdd9d18f905c8191e99fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51216c41011b73a8b62c612c4690b740c9d126f0c24f9404086a7354389b05258e79384161d082a62709237936b8e0f0e081a7d40fd376df796391e07ee5e678
|
7
|
+
data.tar.gz: f6574cd2fac75ccb479d80f0749bc136675024748d8c01baefca6023e871c1d09b970274606b6054bd21d1b24c39517330cba3413d1260866f4a1038697322af
|
data/README.md
CHANGED
@@ -4,14 +4,12 @@
|
|
4
4
|
|
5
5
|
Plugin functionality to add generic import to Rails Admin from CSV, JSON and XLSX files
|
6
6
|
|
7
|
-
*This Readme is for version 1.x. If you are still using version 0.1.x, see [this branch](https://github.com/stephskardal/rails_admin_import/tree/legacy)*
|
8
|
-
|
9
7
|
## Installation
|
10
8
|
|
11
9
|
* First, add to Gemfile:
|
12
10
|
|
13
11
|
```
|
14
|
-
gem "rails_admin_import", "~> 1
|
12
|
+
gem "rails_admin_import", "~> 2.1"
|
15
13
|
```
|
16
14
|
|
17
15
|
* Define configuration in `config/initializers/rails_admin_import.rb`:
|
@@ -62,9 +60,10 @@ Both updating existing records and associating records requires the use of
|
|
62
60
|
### Mapping Keys
|
63
61
|
|
64
62
|
Every importable class has a mapping key that uniquely identifies its
|
65
|
-
instances. The
|
66
|
-
|
67
|
-
|
63
|
+
instances. The mapping key can be one or more fields. The value for
|
64
|
+
these fields can then be provided in import data, either to update the
|
65
|
+
existing record or to attach it through an association to another model.
|
66
|
+
This concept exists because `id`s are often not constant when moving
|
68
67
|
records between data stores.
|
69
68
|
|
70
69
|
For example, a `User` model may have an `email` field. When uploading a set
|
@@ -81,6 +80,9 @@ michael.bolton@initech.com,Michael,Bolton
|
|
81
80
|
would look for existing users with those emails. If one was found, its name
|
82
81
|
fields would be updated. Otherwise, a new one would be created.
|
83
82
|
|
83
|
+
For updating building owners, the mapping key could be `street_address` and
|
84
|
+
`zip_code`.
|
85
|
+
|
84
86
|
Similarly, if each user has favorite books, we could set the mapping key
|
85
87
|
for `Book` to be `isbn` and then include the isbn for their books within each
|
86
88
|
user record. The syntax for this is to use the name of the associated model as
|
@@ -104,13 +106,19 @@ RailsAdmin.config do |config|
|
|
104
106
|
config.model 'User' do
|
105
107
|
import do
|
106
108
|
mapping_key :email
|
109
|
+
# for multiple values, use mapping_key [:first_name, :last_name]
|
110
|
+
mapping_key_list [:email, :some_other_id]
|
107
111
|
end
|
108
112
|
end
|
109
113
|
end
|
110
114
|
```
|
111
115
|
|
116
|
+
Since in models with large number of fields it doesn't make sense to use
|
117
|
+
most of them as mapping values, you can add `mapping_key_list` to
|
118
|
+
restrict which fields can be selected as mapping key in the UI during import.
|
119
|
+
|
112
120
|
Note that a matched record must exist when attaching associated models, or the
|
113
|
-
|
121
|
+
imported record will fail and be skipped.
|
114
122
|
|
115
123
|
Complex associations (`has_one ..., :through` or polymorphic associations)
|
116
124
|
need to be dealt with via custom logic called by one of the import hooks
|
@@ -133,6 +141,8 @@ class Service < ActiveRecord::Base
|
|
133
141
|
end
|
134
142
|
```
|
135
143
|
|
144
|
+
Importing new records by id is not recommended since it ignores the sequences of ids in database. That will lead to `ERROR: duplicate key value violates unique constraint` in future. You can work around this issue by adding an `import_id` column to your model, renaming the `id` column in your CSV to `import_id` and using `import_id` as the update lookup field.
|
145
|
+
|
136
146
|
### File format
|
137
147
|
|
138
148
|
The format is inferred by the extension (.csv, .json or .xlsx).
|
@@ -153,6 +163,8 @@ Peter,Gibbons,IT,Management
|
|
153
163
|
Michael,Bolton,IT,
|
154
164
|
```
|
155
165
|
|
166
|
+
Blank lines will be skipped.
|
167
|
+
|
156
168
|
#### JSON
|
157
169
|
|
158
170
|
The file must be an array or an object with a root key the same name as the plural model name, i.e. the default Rails JSON output format with include_root_in_json on or off.
|
@@ -163,14 +175,41 @@ The Microsoft Excel XLM format (XLSX) is supported, but not the old binary Micro
|
|
163
175
|
|
164
176
|
The expected rows and columns are the same as for the CSV format (first line contains headers, multiple columns for "many" associations).
|
165
177
|
|
178
|
+
Blank lines will be skipped.
|
179
|
+
|
166
180
|
## Configuration
|
167
181
|
|
168
182
|
### Global configuration options
|
169
183
|
|
184
|
+
```ruby
|
185
|
+
RailsAdmin.config do |config|
|
186
|
+
config.actions do
|
187
|
+
all
|
188
|
+
import
|
189
|
+
end
|
190
|
+
|
191
|
+
# Default global RailsAdminImport options
|
192
|
+
config.configure_with(:import) do |config|
|
193
|
+
config.logging = false
|
194
|
+
config.line_item_limit = 1000
|
195
|
+
config.update_if_exists = false
|
196
|
+
config.rollback_on_error = false
|
197
|
+
config.header_converter = lambda do |header|
|
198
|
+
# check for nil/blank headers
|
199
|
+
next if header.blank?
|
200
|
+
header.parameterize.underscore
|
201
|
+
end
|
202
|
+
config.csv_options = {}
|
203
|
+
end
|
204
|
+
end
|
205
|
+
```
|
206
|
+
|
170
207
|
* __logging__ (default `false`): Save a copy of each imported file to log/import and a detailed import log to log/rails_admin_import.log
|
171
208
|
|
172
209
|
* __line_item_limit__ (default `1000`): max number of items that can be imported at one time.
|
173
210
|
|
211
|
+
* __update_if_exists__ (default `false`): default value for the "Update if exists" checkbox on the import page.
|
212
|
+
|
174
213
|
* __rollback_on_error__ (default `false`): import records in a transaction and rollback if there is one error. Only for ActiveRecord, not Mongoid.
|
175
214
|
|
176
215
|
* __header_converter__ (default `lambda { ... }`): a lambda to convert each CSV header text string to a model attribute name. The default header converter converts to lowercase and replaces spaces with underscores.
|
@@ -239,7 +278,7 @@ end
|
|
239
278
|
RailsAdmin.config do |config|
|
240
279
|
config.model 'User' do
|
241
280
|
import do
|
242
|
-
default_excluded_fields [:created_at, :updated_at]
|
281
|
+
default_excluded_fields [:created_at, :updated_at, :deleted_at, :c_at, :u_at]
|
243
282
|
end
|
244
283
|
end
|
245
284
|
end
|
@@ -247,18 +286,56 @@ end
|
|
247
286
|
|
248
287
|
## Import hooks
|
249
288
|
|
250
|
-
|
251
|
-
Define instance methods on your models to be hooked into the import process, if special/additional processing is required on the data:
|
289
|
+
Define methods on your models to be hooked into the import process, if special/additional processing is required on the data:
|
252
290
|
|
253
291
|
```ruby
|
254
292
|
# some model
|
255
293
|
class User < ActiveRecord::Base
|
294
|
+
def self.before_import
|
295
|
+
# called on the model class once before importing any individual records
|
296
|
+
end
|
297
|
+
|
298
|
+
def self.before_import_find(record)
|
299
|
+
# called on the model class before finding or creating the new record
|
300
|
+
# maybe modify the import record that will be used to find the model
|
301
|
+
# throw :skip to skip importing this record
|
302
|
+
throw :skip unless record[:email].ends_with? "@mycompany.com"
|
303
|
+
end
|
304
|
+
|
305
|
+
def before_import_attributes(record)
|
306
|
+
# called on the blank new model or the found model before fields are imported
|
307
|
+
# maybe delete fields from the import record that you don't need
|
308
|
+
# throw :skip to skip importing this record
|
309
|
+
end
|
310
|
+
|
311
|
+
def before_import_associations(record)
|
312
|
+
# called on the model with attributes but before associations are imported
|
313
|
+
# do custom import of associations
|
314
|
+
# make sure to delete association fields from the import record to avoid double import
|
315
|
+
record.delete(:my_association)
|
316
|
+
# throw :skip to skip importing this record
|
317
|
+
end
|
318
|
+
|
256
319
|
def before_import_save(record)
|
257
|
-
#
|
320
|
+
# called on the model before it is saved but after all fields and associations have been imported
|
321
|
+
# make final modifications to the record
|
322
|
+
# throw :skip to skip importing this record
|
258
323
|
end
|
259
324
|
|
260
325
|
def after_import_save(record)
|
261
|
-
#
|
326
|
+
# called on the model after it is saved
|
327
|
+
end
|
328
|
+
|
329
|
+
def after_import_association_error(record)
|
330
|
+
# called on the model when an association cannot be found
|
331
|
+
end
|
332
|
+
|
333
|
+
def after_import_error(record)
|
334
|
+
# called on the model when save fails
|
335
|
+
end
|
336
|
+
|
337
|
+
def self.after_import
|
338
|
+
# called once on the model class after importing all individual records
|
262
339
|
end
|
263
340
|
end
|
264
341
|
```
|
@@ -275,6 +352,29 @@ def before_import_save(record)
|
|
275
352
|
end
|
276
353
|
```
|
277
354
|
|
355
|
+
* Skip some validations when importing.
|
356
|
+
|
357
|
+
```
|
358
|
+
class User < ActiveRecord::Base
|
359
|
+
# Non-persistent attribute to allow creating a new user without a password
|
360
|
+
# Password will be set by the user by following a link in the invitation email
|
361
|
+
attr_accessor :allow_blank_password
|
362
|
+
|
363
|
+
devise :validatable
|
364
|
+
|
365
|
+
# Called by Devise to enable/disable password presence validation
|
366
|
+
def password_required?
|
367
|
+
allow_blank_password ? false : super
|
368
|
+
end
|
369
|
+
|
370
|
+
# Don't require a password when importing users
|
371
|
+
def before_import_save(record)
|
372
|
+
self.allow_blank_password = true
|
373
|
+
end
|
374
|
+
end
|
375
|
+
```
|
376
|
+
|
377
|
+
|
278
378
|
## ORM: ActiveRecord and Mongoid
|
279
379
|
|
280
380
|
The gem is tested to work with ActiveRecord and Mongoid.
|
@@ -292,6 +392,23 @@ If you prefer to eager load all dependecies at boot, use this line in your `Gemf
|
|
292
392
|
gem "rails_admin_import", "~> 1.2.0", require: "rails_admin_import/eager_load"
|
293
393
|
```
|
294
394
|
|
395
|
+
## Import error due to Rails class reloading
|
396
|
+
|
397
|
+
If you get an error like `Error during import: MyModel(#70286054976500) expected, got MyModel(#70286114743280)`, you need restart the rails server and redo the import. This is due to the fact that Rails reloads the ActiveRecord model classes in development when you make changes to them and Rails Admin is still using the old class.
|
398
|
+
|
399
|
+
## Customize the UI
|
400
|
+
|
401
|
+
If you want to hide all the advanced fields from the import UI, you can copy [`app/views/rails_admin/main/import.html.haml`](app/views/rails_admin/main/import.html.haml) to your project at the same path. Add `.hidden` at the end of lines you want to hide.
|
402
|
+
|
403
|
+
For example:
|
404
|
+
```
|
405
|
+
.form-group.control-group.hidden
|
406
|
+
%label.col-sm-2.control-label= t("admin.import.update_if_exists")
|
407
|
+
.col-sm-10.controls
|
408
|
+
= check_box_tag :update_if_exists, '1', true, :class => "form-control"
|
409
|
+
%p.help-block= t('admin.import.help.update_if_exists')
|
410
|
+
```
|
411
|
+
|
295
412
|
## Upgrading
|
296
413
|
|
297
414
|
* Move global config to `config.configure_with(:import)` in `config/initializers/rails_admin_import.rb`.
|
@@ -303,11 +420,17 @@ gem "rails_admin_import", "~> 1.2.0", require: "rails_admin_import/eager_load"
|
|
303
420
|
* Update model import hooks to take 1 hash argument instead of 2 arrays with values and headers.
|
304
421
|
|
305
422
|
* Support for importing file attributes was removed since I couldn't understand how it works. It should be possible to reimplement it yourself using post import hooks. Open an issue to discuss how to put back support for importing files into the gem.
|
306
|
-
|
423
|
+
|
307
424
|
## Community-contributed translations
|
308
425
|
|
309
426
|
* [Spanish translation](https://gist.github.com/yovasx2/dc0e9512e6c6243f840c) by Giovanni Alberto
|
310
427
|
|
428
|
+
* [French translation](https://github.com/rodinux/rails_admin_import.fr-MX.yml) by Rodolphe Robles. (I suggest to translate also rails admin.fr and your locales.fr to resolve an issue with DatePicker)
|
429
|
+
|
430
|
+
* [Italian translation](https://gist.github.com/aprofiti/ec3dc452898c8c48534b59eeb2701765) by Alessandro Profiti
|
431
|
+
|
432
|
+
* [Japanese translation](https://gist.github.com/higumachan/c4bf669d6446ec509386229f916ba5fc) by Yuta Hinokuma
|
433
|
+
|
311
434
|
## Run tests
|
312
435
|
|
313
436
|
1. Clone the repository to your machine
|
@@ -326,6 +449,13 @@ Original author: [Steph Skardal](https://github.com/stephskardal)
|
|
326
449
|
Maintainer (since May 2015): [Julien Vanier](https://github.com/monkbroc)
|
327
450
|
|
328
451
|
|
452
|
+
## Release
|
453
|
+
|
454
|
+
- Update `lib/rails_admin_import/version.rb`
|
455
|
+
- Update the install instructions at [the top of the readme](#installation)
|
456
|
+
- Commit to git
|
457
|
+
- `rake release`
|
458
|
+
|
329
459
|
## Contributing
|
330
460
|
|
331
461
|
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
@@ -30,14 +30,16 @@
|
|
30
30
|
.form-group.control-group
|
31
31
|
%label.col-sm-2.control-label= t("admin.import.update_if_exists")
|
32
32
|
.col-sm-10.controls
|
33
|
-
= check_box_tag :update_if_exists, '1',
|
33
|
+
= check_box_tag :update_if_exists, '1', RailsAdminImport.config.update_if_exists, :class => "form-control"
|
34
34
|
%p.help-block= t('admin.import.help.update_if_exists')
|
35
35
|
.form-group.control-group
|
36
36
|
%label.col-sm-2.control-label{for: "update_lookup"}= t("admin.import.update_lookup")
|
37
37
|
.col-sm-10.controls
|
38
38
|
= select_tag 'update_lookup',
|
39
39
|
options_for_select(@import_model.update_lookup_field_names,
|
40
|
-
|
40
|
+
Array.wrap(@import_model.config.mapping_key).map(&:to_s)),
|
41
|
+
multiple: true,
|
42
|
+
data: { enumeration: true }
|
41
43
|
|
42
44
|
- unless @import_model.association_fields.empty?
|
43
45
|
%fieldset
|
@@ -53,7 +55,8 @@
|
|
53
55
|
.col-sm-10.controls
|
54
56
|
= select_tag "associations[#{field.name}]",
|
55
57
|
options_for_select(@import_model.associated_model_fields(field),
|
56
|
-
|
58
|
+
Array.wrap(@import_model.associated_config(field).mapping_key).first.to_s),
|
59
|
+
data: { enumeration: true }
|
57
60
|
|
58
61
|
%br
|
59
62
|
.form-actions
|
@@ -0,0 +1,3 @@
|
|
1
|
+
See the [community contributed translation section of the README](https://github.com/stephskardal/rails_admin_import#community-contributed-translations) for more languages.
|
2
|
+
|
3
|
+
To contribute a new translation, please put it in a gist and submit a pull request to link your translation in the README.
|
@@ -20,7 +20,7 @@ en:
|
|
20
20
|
missing_update_lookup: "Your file must contain a column for the 'Update lookup field' you selected."
|
21
21
|
invalid_json: "The JSON data should be an array of records or an object with a key '%{root_key}' set to an array of records"
|
22
22
|
update_if_exists: "Update if exists"
|
23
|
-
update_lookup: "Update lookup field"
|
23
|
+
update_lookup: "Update lookup field(s)"
|
24
24
|
mapping: "mapping"
|
25
25
|
encoding: "Encoding"
|
26
26
|
legend:
|
@@ -47,5 +47,4 @@ en:
|
|
47
47
|
For "many" associations, you may include multiple columns with the same header in the CSV file.
|
48
48
|
update_if_exists: "Update records found with the lookup field below instead of creating new records"
|
49
49
|
file_limit: "Please limit upload file to %{limit} line items."
|
50
|
-
encoding: "Choose file encoding. Leave empty to auto-detect. Ignored for JSON."
|
51
|
-
|
50
|
+
encoding: "Choose file encoding. Leave empty to auto-detect. Ignored for JSON."
|
@@ -6,11 +6,14 @@ module RailsAdminImport
|
|
6
6
|
attr_accessor :logging
|
7
7
|
attr_accessor :line_item_limit
|
8
8
|
attr_accessor :rollback_on_error
|
9
|
+
attr_accessor :update_if_exists
|
9
10
|
attr_accessor :header_converter
|
10
11
|
attr_accessor :csv_options
|
11
12
|
|
12
13
|
# Default is to downcase headers and add underscores to convert into attribute names
|
13
14
|
HEADER_CONVERTER = lambda do |header|
|
15
|
+
# check for nil/blank headers
|
16
|
+
next if header.blank?
|
14
17
|
header.parameterize.underscore
|
15
18
|
end
|
16
19
|
|
@@ -31,6 +34,7 @@ module RailsAdminImport
|
|
31
34
|
@logging = false
|
32
35
|
@line_item_limit = 1000
|
33
36
|
@rollback_on_error = false
|
37
|
+
@update_if_exists = false
|
34
38
|
@header_converter = HEADER_CONVERTER
|
35
39
|
@csv_options = {}
|
36
40
|
end
|
@@ -4,6 +4,7 @@ module RailsAdminImport
|
|
4
4
|
module Formats
|
5
5
|
class CSVImporter < FileImporter
|
6
6
|
Formats.register(:csv, self)
|
7
|
+
Formats.register(:CSV, self)
|
7
8
|
|
8
9
|
autoload :CharDet, "rchardet"
|
9
10
|
|
@@ -16,7 +17,8 @@ module RailsAdminImport
|
|
16
17
|
# A method that yields a hash of attributes for each record to import
|
17
18
|
def each_record
|
18
19
|
CSV.foreach(filename, csv_options) do |row|
|
19
|
-
|
20
|
+
attr = convert_to_attributes(row)
|
21
|
+
yield attr unless attr.all? { |field, value| value.blank? }
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
@@ -4,6 +4,7 @@ module RailsAdminImport
|
|
4
4
|
module Formats
|
5
5
|
class XLSXImporter < FileImporter
|
6
6
|
Formats.register(:xlsx, self)
|
7
|
+
Formats.register(:XLSX, self)
|
7
8
|
|
8
9
|
autoload :SimpleXlsxReader, "simple_xlsx_reader"
|
9
10
|
|
@@ -18,7 +19,8 @@ module RailsAdminImport
|
|
18
19
|
sheet = doc.sheets.first
|
19
20
|
@headers = convert_headers(sheet.headers)
|
20
21
|
sheet.data.each do |row|
|
21
|
-
|
22
|
+
attr = convert_to_attributes(row)
|
23
|
+
yield attr unless attr.all? { |field, value| value.blank? }
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
@@ -33,7 +35,7 @@ module RailsAdminImport
|
|
33
35
|
def convert_to_attributes(row)
|
34
36
|
row_with_headers = @headers.zip(row)
|
35
37
|
row_with_headers.each_with_object({}) do |(field, value), record|
|
36
|
-
|
38
|
+
next if field.nil?
|
37
39
|
field = field.to_sym
|
38
40
|
if import_model.has_multiple_values?(field)
|
39
41
|
field = import_model.pluralize_field(field)
|
@@ -16,7 +16,6 @@ module RailsAdminImport
|
|
16
16
|
begin
|
17
17
|
init_results
|
18
18
|
|
19
|
-
|
20
19
|
if records.count > RailsAdminImport.config.line_item_limit
|
21
20
|
return results = {
|
22
21
|
success: [],
|
@@ -24,13 +23,19 @@ module RailsAdminImport
|
|
24
23
|
}
|
25
24
|
end
|
26
25
|
|
26
|
+
perform_global_callback(:before_import)
|
27
|
+
|
27
28
|
with_transaction do
|
28
29
|
records.each do |record|
|
29
|
-
|
30
|
+
catch :skip do
|
31
|
+
import_record(record)
|
32
|
+
end
|
30
33
|
end
|
31
34
|
|
32
35
|
rollback_if_error
|
33
36
|
end
|
37
|
+
|
38
|
+
perform_global_callback(:after_import)
|
34
39
|
rescue Exception => e
|
35
40
|
report_general_error("#{e} (#{e.backtrace.first})")
|
36
41
|
end
|
@@ -65,19 +70,24 @@ module RailsAdminImport
|
|
65
70
|
end
|
66
71
|
|
67
72
|
def import_record(record)
|
68
|
-
|
73
|
+
perform_model_callback(import_model.model, :before_import_find, record)
|
74
|
+
|
75
|
+
if update_lookup && !(update_lookup - record.keys).empty?
|
69
76
|
raise UpdateLookupError, I18n.t("admin.import.missing_update_lookup")
|
70
77
|
end
|
71
78
|
|
72
79
|
object = find_or_create_object(record, update_lookup)
|
80
|
+
return if object.nil?
|
73
81
|
action = object.new_record? ? :create : :update
|
74
82
|
|
75
83
|
begin
|
84
|
+
perform_model_callback(object, :before_import_associations, record)
|
76
85
|
import_single_association_data(object, record)
|
77
86
|
import_many_association_data(object, record)
|
78
87
|
rescue AssociationNotFound => e
|
79
88
|
error = I18n.t("admin.import.association_not_found", :error => e.to_s)
|
80
89
|
report_error(object, action, error)
|
90
|
+
perform_model_callback(object, :after_import_association_error, record)
|
81
91
|
return
|
82
92
|
end
|
83
93
|
|
@@ -88,12 +98,13 @@ module RailsAdminImport
|
|
88
98
|
perform_model_callback(object, :after_import_save, record)
|
89
99
|
else
|
90
100
|
report_error(object, action, object.errors.full_messages.join(", "))
|
101
|
+
perform_model_callback(object, :after_import_error, record)
|
91
102
|
end
|
92
103
|
end
|
93
104
|
|
94
105
|
def update_lookup
|
95
106
|
@update_lookup ||= if params[:update_if_exists] == "1"
|
96
|
-
params[:update_lookup].to_sym
|
107
|
+
params[:update_lookup].map(&:to_sym)
|
97
108
|
end
|
98
109
|
end
|
99
110
|
|
@@ -169,6 +180,11 @@ module RailsAdminImport
|
|
169
180
|
end
|
170
181
|
end
|
171
182
|
|
183
|
+
def perform_global_callback(method_name)
|
184
|
+
object = import_model.model
|
185
|
+
object.send(method_name) if object.respond_to?(method_name)
|
186
|
+
end
|
187
|
+
|
172
188
|
def find_or_create_object(record, update)
|
173
189
|
field_names = import_model.model_fields.map(&:name)
|
174
190
|
new_attrs = record.select do |field_name, value|
|
@@ -177,13 +193,19 @@ module RailsAdminImport
|
|
177
193
|
|
178
194
|
model = import_model.model
|
179
195
|
object = if update.present?
|
180
|
-
|
196
|
+
query = update.each_with_object({}) do
|
197
|
+
|field, query| query[field] = record[field]
|
198
|
+
end
|
199
|
+
model.where(query).first
|
181
200
|
end
|
182
201
|
|
183
202
|
if object.nil?
|
184
|
-
object = model.new
|
203
|
+
object = model.new
|
204
|
+
perform_model_callback(object, :before_import_attributes, record)
|
205
|
+
object.attributes = new_attrs
|
185
206
|
else
|
186
|
-
object
|
207
|
+
perform_model_callback(object, :before_import_attributes, record)
|
208
|
+
object.attributes = new_attrs.except(update.map(&:to_sym))
|
187
209
|
end
|
188
210
|
object
|
189
211
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_admin_import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steph Skardal
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2017-11-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -39,20 +39,6 @@ dependencies:
|
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: 0.6.6
|
42
|
-
- !ruby/object:Gem::Dependency
|
43
|
-
name: haml
|
44
|
-
requirement: !ruby/object:Gem::Requirement
|
45
|
-
requirements:
|
46
|
-
- - "~>"
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: '4.0'
|
49
|
-
type: :runtime
|
50
|
-
prerelease: false
|
51
|
-
version_requirements: !ruby/object:Gem::Requirement
|
52
|
-
requirements:
|
53
|
-
- - "~>"
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version: '4.0'
|
56
42
|
- !ruby/object:Gem::Dependency
|
57
43
|
name: rchardet
|
58
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -96,6 +82,7 @@ files:
|
|
96
82
|
- app/views/rails_admin/main/_results.html.haml
|
97
83
|
- app/views/rails_admin/main/_section.html.haml
|
98
84
|
- app/views/rails_admin/main/import.html.haml
|
85
|
+
- config/locales/README.md
|
99
86
|
- config/locales/import.en.yml
|
100
87
|
- lib/rails_admin_import.rb
|
101
88
|
- lib/rails_admin_import/action.rb
|
@@ -135,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
122
|
version: '0'
|
136
123
|
requirements: []
|
137
124
|
rubyforge_project:
|
138
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.5.1
|
139
126
|
signing_key:
|
140
127
|
specification_version: 4
|
141
128
|
summary: Import functionality for Rails Admin
|