rails_admin_import_no_encoding 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5cf491737b86ee1f78ee503e342f51ba3d20eddc254f725ce479b9863477af03
4
+ data.tar.gz: 2f4d5a76c259980f831fab6f32a0d7544dc6a48ca182f9c686bc68c31353b1ee
5
+ SHA512:
6
+ metadata.gz: b02e48ee3b7fa83ab77c088369bd7069342504e92a0a22c97dc289d941de57058fc403b752614ce73f3af8a2d1307ddb3fbae0dbc9497215665909a05599d7ec
7
+ data.tar.gz: 34f8b4730db3221e3bb57b3323c4ef54d8d8eb3eda7bf9ebe990e94bec6b4afe259067c062586b52a41a67403908fa341037310a92ec6e6eced6c294991d5c85
data/CHANGELOG.md ADDED
@@ -0,0 +1,48 @@
1
+ # Change Log
2
+
3
+ # 2.0.0 / 2016-05-04
4
+
5
+ - Pull in the encoding list from the Encoding module instead of RailsAdmin. Thanks @baldursson, @patricklewis and @lucasff
6
+
7
+ # 1.4.0 / 2016-02-26
8
+
9
+ - Implement row limit (default maximum is 1000 rows). Thanks @codealchemy!
10
+ - Document mapping keys. Thanks @zrisher!
11
+ - Ignore columns with blank headers in CSV files
12
+
13
+ # 1.3.1 / 2016-02-11
14
+
15
+ - Bugfix: Create log/import directory when logging config option is enabled. Thanks @yovasx2!
16
+
17
+ # 1.3.0 / 2015-11-24
18
+
19
+ - Use `where.first` instead of `find_by` so update works for Mongoid
20
+
21
+ ## 1.2.0 / 2015-08-27
22
+
23
+ - Existing records can now be updated based on a belongs_to foreign key. Thanks Diego Carrion!
24
+ - Add Excel file format support
25
+ - Autoload the Excel import and encoding detection gems
26
+
27
+ ## 1.1.0 / 2015-08-04
28
+
29
+ - `csv_options` config added. Thanks Maksim Burnin!
30
+
31
+ ## 1.0.0 / 2015-06-09
32
+
33
+ Major rework of the gem by new maintainer.
34
+
35
+ - Support for Mongoid
36
+ - Changed model import hooks to take 1 hash argument
37
+ - Use Rails Admin abstract model instead of ActiveRecord reflection for better compatibility with custom associations
38
+ - Support CSV and JSON
39
+ - Update styling for Bootstrap 3
40
+ - Added tests
41
+
42
+
43
+ ## 0.1.9 / 2014-05-22
44
+
45
+ - Updated/corrected README
46
+ - Merged ImportLogger work
47
+ - Merged modifications to import view
48
+ - Merged post save hook on models
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Steph Skardal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,476 @@
1
+ # Rails Admin Import
2
+
3
+ [![Build Status](https://travis-ci.org/monkbroc/rails_admin_import.svg?branch=master)](https://travis-ci.org/monkbroc/rails_admin_import)
4
+
5
+ Plugin functionality to add generic import to Rails Admin from CSV, JSON and XLSX files
6
+
7
+ ## Installation
8
+
9
+ * First, add to Gemfile:
10
+
11
+ ```
12
+ gem "rails_admin_import", "~> 2.2"
13
+ ```
14
+
15
+ * Define configuration in `config/initializers/rails_admin_import.rb`:
16
+
17
+ ```ruby
18
+ RailsAdmin.config do |config|
19
+ # REQUIRED:
20
+ # Include the import action
21
+ # See https://github.com/sferik/rails_admin/wiki/Actions
22
+ config.actions do
23
+ all
24
+ import
25
+ end
26
+
27
+ # Optional:
28
+ # Configure global RailsAdminImport options
29
+ config.configure_with(:import) do |config|
30
+ config.logging = true
31
+ end
32
+
33
+ # Optional:
34
+ # Configure model-specific options using standard RailsAdmin DSL
35
+ # See https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
36
+ config.model 'User' do
37
+ import do
38
+ include_all_fields
39
+ exclude_fields :secret_token
40
+ end
41
+ end
42
+ end
43
+ ```
44
+
45
+ * If you are using CanCanCan for authorization, add to ability.rb to specify which models can be imported:
46
+
47
+ ```ruby
48
+ cannot :import, :all
49
+ can :import, [User, Model1, Model2]
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Model instances can be both created and updated from import data. Any fields
55
+ can be imported as long as they are allowed by the model's configuration.
56
+ Associated records can be looked up for both singular and plural relationships.
57
+ Both updating existing records and associating records requires the use of
58
+ **mapping keys**.
59
+
60
+ ### Mapping Keys
61
+
62
+ Every importable class has a mapping key that uniquely identifies its
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
67
+ records between data stores.
68
+
69
+ For example, a `User` model may have an `email` field. When uploading a set
70
+ of users where some already exist in our database, we can select "email" as
71
+ our mapping key and then provide that field on each record in our data,
72
+ allowing us to update existing records with matching emails.
73
+
74
+ Using a csv formatted example:
75
+ ```
76
+ Email,First name,Last name
77
+ peter.gibbons@initech.com,Peter,Gibbons
78
+ michael.bolton@initech.com,Michael,Bolton
79
+ ```
80
+ would look for existing users with those emails. If one was found, its name
81
+ fields would be updated. Otherwise, a new one would be created.
82
+
83
+ For updating building owners, the mapping key could be `street_address` and
84
+ `zip_code`.
85
+
86
+ Similarly, if each user has favorite books, we could set the mapping key
87
+ for `Book` to be `isbn` and then include the isbn for their books within each
88
+ user record. The syntax for this is to use the name of the associated model as
89
+ the field name, no matter what actual mapping key has been selected. So
90
+ a user record would have one or more fields named "Book" that include each
91
+ associated book's ISBN.
92
+
93
+ Again using a csv formatted example:
94
+ ```
95
+ Email, Book, Book, Book
96
+ peter.gibbons@initech.com, 9781119997870, 9780671027032
97
+ michael.bolton@initech.com, 9780446677479
98
+ ```
99
+ would look up books with those ISBNs and attach them to those users.
100
+
101
+ Mapping keys can be selected on the import page. Their defaults can also be
102
+ globally configured in the config file:
103
+
104
+ ```
105
+ RailsAdmin.config do |config|
106
+ config.model 'User' do
107
+ import do
108
+ mapping_key :email
109
+ # for multiple values, use mapping_key [:first_name, :last_name]
110
+ mapping_key_list [:email, :some_other_id]
111
+ end
112
+ end
113
+ end
114
+ ```
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
+
120
+ Note that a matched record must exist when attaching associated models, or the
121
+ imported record will fail and be skipped.
122
+
123
+ Complex associations (`has_one ..., :through` or polymorphic associations)
124
+ need to be dealt with via custom logic called by one of the import hooks
125
+ (see below for more detail on using hooks). If we wanted to import
126
+ `Service`s and attach them to a `User`, but the user relationship
127
+ existed through an intermediary model called `ServiceProvider`, we could
128
+ provide a `user_email` field in our records and handle the actual
129
+ association with an import hook:
130
+
131
+ ```
132
+ class Service < ActiveRecord::Base
133
+ belongs_to :service_provider
134
+ has_one :user, through: :service_provider
135
+
136
+ def before_import_save(record)
137
+ if (email = record[:user_email]) && (user = User.find_by_email(email))
138
+ self.service_provider = user.service_provider
139
+ end
140
+ end
141
+ end
142
+ ```
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
+
146
+ ### File format
147
+
148
+ The format is inferred by the extension (.csv, .json or .xlsx).
149
+
150
+ #### CSV
151
+
152
+ The first line must contain attribute names. They will be converted to lowercase and underscored (First Name ==> first_name).
153
+
154
+ For "many" associations, you may include multiple columns with the same header in the CSV file.
155
+
156
+ The repeated header may be singular or plural. For example, for a "children" association, you may have multiple "child" columns or multiple "children" column, each containing one lookup value for an associated record. Blank values are ignored.
157
+
158
+ Example
159
+
160
+ ```
161
+ First name,Last name,Team,Team
162
+ Peter,Gibbons,IT,Management
163
+ Michael,Bolton,IT,
164
+ ```
165
+
166
+ Blank lines will be skipped.
167
+
168
+ #### JSON
169
+
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.
171
+
172
+ #### XLSX
173
+
174
+ The Microsoft Excel XLM format (XLSX) is supported, but not the old binary Microsoft Excel format (XLS).
175
+
176
+ The expected rows and columns are the same as for the CSV format (first line contains headers, multiple columns for "many" associations).
177
+
178
+ Blank lines will be skipped.
179
+
180
+ ## Configuration
181
+
182
+ ### Global configuration options
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
+
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
208
+
209
+ * __line_item_limit__ (default `1000`): max number of items that can be imported at one time.
210
+
211
+ * __update_if_exists__ (default `false`): default value for the "Update if exists" checkbox on the import page.
212
+
213
+ * __rollback_on_error__ (default `false`): import records in a transaction and rollback if there is one error. Only for ActiveRecord, not Mongoid.
214
+
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.
216
+
217
+ * __csv_options__ (default `{}`): a hash of options that will be passed to a new [CSV](http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html) instance
218
+
219
+ ### Model-specific configuration
220
+
221
+ Use [standard RailsAdmin DSL](https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL) to add or remove fields.
222
+
223
+ * To change the default attribute that will be used to find associations on import, set `mapping_key` (default attribute is `name`)
224
+
225
+ ```ruby
226
+ RailsAdmin.config do |config|
227
+ config.model 'Ball' do
228
+ import do
229
+ mapping_key :color
230
+ end
231
+ end
232
+ end
233
+ ```
234
+
235
+ * To include a specific list of fields:
236
+
237
+ ```ruby
238
+ RailsAdmin.config do |config|
239
+ config.model 'User' do
240
+ import do
241
+ field :first_name
242
+ field :last_name
243
+ field :email
244
+ end
245
+ end
246
+ end
247
+ ```
248
+
249
+ * To exclude specific fields:
250
+
251
+ ```ruby
252
+ RailsAdmin.config do |config|
253
+ config.model 'User' do
254
+ import do
255
+ include_all_fields
256
+ exclude_fields :secret_token
257
+ end
258
+ end
259
+ end
260
+ ```
261
+
262
+ * To add extra fields that will be set as attributes on the model and that will be passed to the import hook methods:
263
+
264
+ ```ruby
265
+ RailsAdmin.config do |config|
266
+ config.model 'User' do
267
+ import do
268
+ include_all_fields
269
+ fields :special_import_token
270
+ end
271
+ end
272
+ end
273
+ ```
274
+
275
+ * To overwrite the [default excluded fields](https://github.com/stephskardal/rails_admin_import/blob/master/lib/rails_admin_import/config/sections/import.rb#L13) and allow matching to `:id` on import
276
+
277
+ ```ruby
278
+ RailsAdmin.config do |config|
279
+ config.model 'User' do
280
+ import do
281
+ default_excluded_fields [:created_at, :updated_at, :deleted_at, :c_at, :u_at]
282
+ end
283
+ end
284
+ end
285
+ ```
286
+
287
+ ## Import hooks
288
+
289
+ Define methods on your models to be hooked into the import process, if special/additional processing is required on the data:
290
+
291
+ ```ruby
292
+ # some model
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
+
319
+ def before_import_save(record)
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
323
+ end
324
+
325
+ def after_import_save(record)
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
339
+ end
340
+ end
341
+ ```
342
+
343
+ For example, you could
344
+
345
+ * Set an attribute on a Devise User model to skip checking for a password when importing a new model.
346
+
347
+ * Import an image into Carrierwave via a URL provided in the CSV.
348
+
349
+ ```
350
+ def before_import_save(record)
351
+ self.remote_image_url = record[:image] if record[:image].present?
352
+ end
353
+ ```
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
+
378
+ ## ORM: ActiveRecord and Mongoid
379
+
380
+ The gem is tested to work with ActiveRecord and Mongoid.
381
+
382
+ Support for Mongoid is early, so if you can suggest improvements (especially around importing embedded models), open an issue.
383
+
384
+
385
+ ## Eager loading
386
+
387
+ Since the import functionality is rarely used in many applications, some gems are autoloaded when first used during an import in order to save memory at boot.
388
+
389
+ If you prefer to eager load all dependecies at boot, use this line in your `Gemfile`.
390
+
391
+ ```
392
+ gem "rails_admin_import", "~> 1.2.0", require: "rails_admin_import/eager_load"
393
+ ```
394
+
395
+ ## Import error due to Rails class reloading
396
+
397
+ ![error due to class reloading](https://user-images.githubusercontent.com/2566348/51355874-0f83ad00-1a7e-11e9-8e58-46bc4699f2e6.jpg)
398
+
399
+ 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.
400
+
401
+ Another suggestion is to set `config.cache_classes = true` to true in your `development.rb` for Rails Admin Import to work around the ActiveRecord model class reloading issue. See [this comment](https://github.com/stephskardal/rails_admin_import/issues/88#issuecomment-455374671) for more information.
402
+
403
+ ## Customize the UI
404
+
405
+ 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.
406
+
407
+ For example:
408
+ ```
409
+ .form-group.control-group.hidden
410
+ %label.col-sm-2.control-label= t("admin.import.update_if_exists")
411
+ .col-sm-10.controls
412
+ = check_box_tag :update_if_exists, '1', true, :class => "form-control"
413
+ %p.help-block= t('admin.import.help.update_if_exists')
414
+ ```
415
+
416
+ ## Upgrading
417
+
418
+ * Move global config to `config.configure_with(:import)` in `config/initializers/rails_admin_import.rb`.
419
+
420
+ * Move the field definitions to `config.model 'User' do; import do; // ...` in `config/initializers/rails_admin_import.rb`.
421
+
422
+ * No need to mount RailsAdminImport in `config/routes.rb` (RailsAdmin must still be mounted).
423
+
424
+ * Update model import hooks to take 1 hash argument instead of 2 arrays with values and headers.
425
+
426
+ * 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.
427
+
428
+ ## Community-contributed translations
429
+
430
+ * [Spanish translation](https://gist.github.com/yovasx2/dc0e9512e6c6243f840c) by Giovanni Alberto
431
+
432
+ * [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)
433
+
434
+ * [Italian translation](https://gist.github.com/aprofiti/ec3dc452898c8c48534b59eeb2701765) by Alessandro Profiti
435
+
436
+ * [Japanese translation](https://gist.github.com/higumachan/c4bf669d6446ec509386229f916ba5fc) by Yuta Hinokuma
437
+
438
+ * [Brazilian Portuguese translation](https://gist.github.com/tteurs/5a87ff4bc5f24692dab05b3cde0ca9df) by Matheo Gracia Pegoraro
439
+
440
+ ## Run tests
441
+
442
+ 1. Clone the repository to your machine
443
+
444
+ git clone https://github.com/stephskardal/rails_admin_import
445
+
446
+ 2. Run `bundle install`
447
+ 3. Run `bundle exec rspec`
448
+
449
+ The structure of the tests is taken from the Rails Admin gem.
450
+
451
+ ## Authors
452
+
453
+ Original author: [Steph Skardal](https://github.com/stephskardal)
454
+
455
+ Maintainer (since May 2015): [Julien Vanier](https://github.com/monkbroc)
456
+
457
+
458
+ ## Release
459
+
460
+ - Update `lib/rails_admin_import/version.rb`
461
+ - Update the install instructions at [the top of the readme](#installation)
462
+ - Commit to git
463
+ - `rake release`
464
+
465
+ ## Contributing
466
+
467
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
468
+
469
+ - [Report bugs](https://github.com/stephskardal/rails_admin_import/issues)
470
+ - Fix bugs and [submit pull requests](https://github.com/stephskardal/rails_admin_import/pulls)
471
+ - Write, clarify, or fix documentation
472
+ - Suggest or add new features
473
+
474
+ ## Copyright
475
+
476
+ Copyright (c) 2015 End Point, Steph Skardal and contributors. See LICENSE.txt for further details.