rails_admin_import 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c799862da18cc280ba530ca0bdf89c492f725573
4
- data.tar.gz: 83702de0110d5d0646c3323933367020761427a8
2
+ SHA256:
3
+ metadata.gz: b0b71f8452cd6e451b12a22c3d3afb9418bd9308857e11522007db476f014e29
4
+ data.tar.gz: df443cf15365e4c8ad61d81db2f9ba49bb582388cb9022f24ebeaacea2725b83
5
5
  SHA512:
6
- metadata.gz: aa86b09270e22dcbfcc2b4b98b98597fc3d27711eab969064b1a7f87ee94316779ef5f9b751f7187464c71f30c4d23b1363ee1e93d8056ab95f4379f8482ac6d
7
- data.tar.gz: 6c2191818eb60e4ddab360ec9adcec4a9cf899acf2d8e9e96842f3e73cdc0c8d7e59174b223bed94092d7aad74a2334ebfc4cd229f387bd2dfd83e349a1dd757
6
+ metadata.gz: 901d0f3cc189f9a51acf9e7cd492f66f28958a8a205c183f31138367caecb3639b7c6d47c66e866459e7b4739a2b51b93c44b47dc1c23f5d7932fe5563cffa8b
7
+ data.tar.gz: f9187fe631aa5e2e0ac0c2fcacc7d4a966d682fff9afefcb047608aaf4e9bb8587b56a615da95de7b429df05e9b9ecdff30c00f7320b0a0e3db090894036ba04
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Change Log
2
2
 
3
+ # 2.3.0 / 2021-06-11
4
+
5
+ - Added an option to pass filename for each record. Thanks @waheedi
6
+ - Enumeration translation. Thanks @zaknafain
7
+
8
+ # 2.2.0 / 2019-02-24
9
+
10
+ - Switch to using CharlockHolmes for character detection. Thanks @codealchemy
11
+ - Readme improvements. Thanks @olleolleolle
12
+
13
+ # 2.1.0 / 2017-11-18
14
+
15
+ - French translation. Thanks @rodinux
16
+ - Check for blank headers. Thanks @JuandGirald
17
+ - Italian translation. Thanks @aprofiti
18
+ - Remove haml dependency. Thanks @prem-prakash
19
+ - Japanese translation. Thanks @higumachan
20
+ - Multiple mapping keys. Thanks @dmitrypol
21
+ - Add more hooks during import
22
+
3
23
  # 2.0.0 / 2016-05-04
4
24
 
5
25
  - Pull in the encoding list from the Encoding module instead of RailsAdmin. Thanks @baldursson, @patricklewis and @lucasff
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rails Admin Import
2
2
 
3
- [![Build Status](https://travis-ci.org/monkbroc/rails_admin_import.svg?branch=master)](https://travis-ci.org/monkbroc/rails_admin_import)
3
+ [![Build Status](https://github.com/monkbroc/rails_admin_import/actions/workflows/ruby.yml/badge.svg)](https://github.com/monkbroc/rails_admin_import/actions/workflows/ruby.yml)
4
4
 
5
5
  Plugin functionality to add generic import to Rails Admin from CSV, JSON and XLSX files
6
6
 
@@ -8,8 +8,8 @@ Plugin functionality to add generic import to Rails Admin from CSV, JSON and XLS
8
8
 
9
9
  * First, add to Gemfile:
10
10
 
11
- ```
12
- gem "rails_admin_import", "~> 2.2"
11
+ ```ruby
12
+ gem "rails_admin_import", "~> 2.3"
13
13
  ```
14
14
 
15
15
  * Define configuration in `config/initializers/rails_admin_import.rb`:
@@ -26,8 +26,10 @@ RailsAdmin.config do |config|
26
26
 
27
27
  # Optional:
28
28
  # Configure global RailsAdminImport options
29
+ # Configure pass filename to records hashes
29
30
  config.configure_with(:import) do |config|
30
31
  config.logging = true
32
+ config.pass_filename = true
31
33
  end
32
34
 
33
35
  # Optional:
@@ -72,11 +74,13 @@ our mapping key and then provide that field on each record in our data,
72
74
  allowing us to update existing records with matching emails.
73
75
 
74
76
  Using a csv formatted example:
77
+
75
78
  ```
76
79
  Email,First name,Last name
77
80
  peter.gibbons@initech.com,Peter,Gibbons
78
81
  michael.bolton@initech.com,Michael,Bolton
79
82
  ```
83
+
80
84
  would look for existing users with those emails. If one was found, its name
81
85
  fields would be updated. Otherwise, a new one would be created.
82
86
 
@@ -91,17 +95,19 @@ a user record would have one or more fields named "Book" that include each
91
95
  associated book's ISBN.
92
96
 
93
97
  Again using a csv formatted example:
98
+
94
99
  ```
95
100
  Email, Book, Book, Book
96
101
  peter.gibbons@initech.com, 9781119997870, 9780671027032
97
102
  michael.bolton@initech.com, 9780446677479
98
103
  ```
104
+
99
105
  would look up books with those ISBNs and attach them to those users.
100
106
 
101
107
  Mapping keys can be selected on the import page. Their defaults can also be
102
108
  globally configured in the config file:
103
109
 
104
- ```
110
+ ```ruby
105
111
  RailsAdmin.config do |config|
106
112
  config.model 'User' do
107
113
  import do
@@ -128,7 +134,7 @@ existed through an intermediary model called `ServiceProvider`, we could
128
134
  provide a `user_email` field in our records and handle the actual
129
135
  association with an import hook:
130
136
 
131
- ```
137
+ ```ruby
132
138
  class Service < ActiveRecord::Base
133
139
  belongs_to :service_provider
134
140
  has_one :user, through: :service_provider
@@ -193,11 +199,10 @@ RailsAdmin.config do |config|
193
199
  config.logging = false
194
200
  config.line_item_limit = 1000
195
201
  config.update_if_exists = false
202
+ config.pass_filename = false
196
203
  config.rollback_on_error = false
197
204
  config.header_converter = lambda do |header|
198
- # check for nil/blank headers
199
- next if header.blank?
200
- header.parameterize.underscore
205
+ header.parameterize.underscore if header.present?
201
206
  end
202
207
  config.csv_options = {}
203
208
  end
@@ -215,6 +220,7 @@ end
215
220
  * __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
221
 
217
222
  * __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
223
+ * __pass_filename__ (default `false`): Access the uploaded file name in your model actions, for example if set to true, inside each record, there will be an addtional property `record[:filename_importer]` which contains the file name of the currently uploaded file.
218
224
 
219
225
  ### Model-specific configuration
220
226
 
@@ -346,7 +352,7 @@ For example, you could
346
352
 
347
353
  * Import an image into Carrierwave via a URL provided in the CSV.
348
354
 
349
- ```
355
+ ```ruby
350
356
  def before_import_save(record)
351
357
  self.remote_image_url = record[:image] if record[:image].present?
352
358
  end
@@ -354,7 +360,7 @@ end
354
360
 
355
361
  * Skip some validations when importing.
356
362
 
357
- ```
363
+ ```ruby
358
364
  class User < ActiveRecord::Base
359
365
  # Non-persistent attribute to allow creating a new user without a password
360
366
  # Password will be set by the user by following a link in the invitation email
@@ -388,7 +394,7 @@ Since the import functionality is rarely used in many applications, some gems ar
388
394
 
389
395
  If you prefer to eager load all dependecies at boot, use this line in your `Gemfile`.
390
396
 
391
- ```
397
+ ```ruby
392
398
  gem "rails_admin_import", "~> 1.2.0", require: "rails_admin_import/eager_load"
393
399
  ```
394
400
 
@@ -405,7 +411,8 @@ Another suggestion is to set `config.cache_classes = true` to true in your `deve
405
411
  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
412
 
407
413
  For example:
408
- ```
414
+
415
+ ```haml
409
416
  .form-group.control-group.hidden
410
417
  %label.col-sm-2.control-label= t("admin.import.update_if_exists")
411
418
  .col-sm-10.controls
@@ -1,3 +1,14 @@
1
+ :ruby
2
+ translations = {
3
+ add: t('admin.import.enumeration.add'),
4
+ chooseAll: t('admin.import.enumeration.choose_all'),
5
+ clearAll: t('admin.import.enumeration.clear_all'),
6
+ down: t('admin.import.enumeration.down'),
7
+ remove: t('admin.import.enumeration.remove'),
8
+ search: t('admin.import.enumeration.search'),
9
+ up: t('admin.import.enumeration.up')
10
+ }
11
+
1
12
  = render "results"
2
13
 
3
14
  = form_tag import_path(@abstract_model), :multipart => true, class: 'form-horizontal denser' do
@@ -25,7 +36,7 @@
25
36
  .col-sm-10.controls
26
37
  = select_tag 'encoding',
27
38
  options_for_select(Encoding.name_list.sort),
28
- include_blank: true, data: { enumeration: true }
39
+ include_blank: true, data: { enumeration: true, options: { regional: translations } }
29
40
  %p.help-block= t('admin.import.help.encoding', name: 'UTF-8')
30
41
  .form-group.control-group
31
42
  %label.col-sm-2.control-label= t("admin.import.update_if_exists")
@@ -39,7 +50,7 @@
39
50
  options_for_select(@import_model.update_lookup_field_names,
40
51
  Array.wrap(@import_model.config.mapping_key).map(&:to_s)),
41
52
  multiple: true,
42
- data: { enumeration: true }
53
+ data: { enumeration: true, options: { regional: translations } }
43
54
 
44
55
  - unless @import_model.association_fields.empty?
45
56
  %fieldset
@@ -56,7 +67,7 @@
56
67
  = select_tag "associations[#{field.name}]",
57
68
  options_for_select(@import_model.associated_model_fields(field),
58
69
  Array.wrap(@import_model.associated_config(field).mapping_key).first.to_s),
59
- data: { enumeration: true }
70
+ data: { enumeration: true, options: { regional: translations } }
60
71
 
61
72
  %br
62
73
  .form-actions
@@ -1,50 +1,53 @@
1
-
2
1
  en:
3
2
  admin:
4
3
  actions:
5
4
  import:
6
- title: "Import"
7
- menu: "Import"
8
- breadcrumb: "Import"
9
- link: "Import"
10
- bulk_link: "Import"
11
- done: "Imported"
5
+ breadcrumb: Import
6
+ bulk_link: Import
7
+ done: Imported
8
+ link: Import
9
+ menu: Import
10
+ title: Import
12
11
  import:
13
- model_fields: "Model fields"
14
- association_fields: "Association fields"
15
-
16
- file: "Data file"
17
- missing_file: "You must select a file"
18
- format: "File format"
19
- invalid_format: "Invalid import format."
20
- missing_update_lookup: "Your file must contain a column for the 'Update lookup field' you selected."
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
- update_if_exists: "Update if exists"
23
- update_lookup: "Update lookup field(s)"
24
- mapping: "mapping"
25
- encoding: "Encoding"
26
- legend:
27
- fields: "Fields to import"
28
- upload: "Upload file"
29
- mapping: "Related fields mapping"
30
- import_success:
31
- create: "Created %{name}"
32
- update: "Updated %{name}"
33
- import_error:
34
- create: "Failed to create %{name}: %{error}"
35
- update: "Failed to update %{name}: %{error}"
36
- general: "Error during import: %{error}"
37
- line_item_limit: "Please limit upload file to %{limit} line items."
38
- old_import_hook: >
39
- The import hook %{model}.%{method} should take only 1 argument.
40
- Data may not imported correctly.
41
- See Upgrading section readme in Rails Admin Import.
42
- association_not_found: "Association not found. %{error}"
12
+ association_fields: Association fields
13
+ association_not_found: 'Association not found. %{error}'
14
+ encoding: Encoding
15
+ enumeration:
16
+ add: Add new
17
+ choose_all: Choose all
18
+ clear_all: Clear all
19
+ down: Down
20
+ remove: Remove
21
+ search: Search
22
+ up: Up
23
+ file: Data file
24
+ format: File format
43
25
  help:
44
- model_fields: "The fields above may be included in the import file."
45
- association_fields: >
46
- These fields map to other tables in the database, lookup via attribute selected below.
47
- For "many" associations, you may include multiple columns with the same header in the CSV file.
48
- update_if_exists: "Update records found with the lookup field below instead of creating new records"
49
- file_limit: "Please limit upload file to %{limit} line items."
50
- encoding: "Choose file encoding. Leave empty to auto-detect. Ignored for JSON."
26
+ association_fields: |
27
+ These fields map to other tables in the database, lookup via attribute selected below. For "many" associations, you may include multiple columns with the same header in the CSV file.
28
+ encoding: Choose file encoding. Leave empty to auto-detect. Ignored for JSON.
29
+ file_limit: 'Please limit upload file to %{limit} line items.'
30
+ model_fields: The fields above may be included in the import file.
31
+ update_if_exists: Update records found with the lookup field below instead of creating new records
32
+ import_error:
33
+ create: 'Failed to create %{name}: %{error}'
34
+ general: 'Error during import: %{error}'
35
+ line_item_limit: 'Please limit upload file to %{limit} line items.'
36
+ old_import_hook: |
37
+ The import hook %{model}.%{method} should take only 1 argument. Data may not imported correctly. See Upgrading section readme in Rails Admin Import.
38
+ update: 'Failed to update %{name}: %{error}'
39
+ import_success:
40
+ create: 'Created %{name}'
41
+ update: 'Updated %{name}'
42
+ invalid_format: Invalid import format.
43
+ 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'
44
+ legend:
45
+ fields: Fields to import
46
+ mapping: Related fields mapping
47
+ upload: Upload file
48
+ mapping: mapping
49
+ missing_file: You must select a file
50
+ missing_update_lookup: Your file must contain a column for the 'Update lookup field' you selected.
51
+ model_fields: Model fields
52
+ update_if_exists: Update if exists
53
+ update_lookup: Update lookup field(s)
@@ -9,6 +9,7 @@ module RailsAdminImport
9
9
  attr_accessor :update_if_exists
10
10
  attr_accessor :header_converter
11
11
  attr_accessor :csv_options
12
+ attr_accessor :pass_filename
12
13
 
13
14
  # Default is to downcase headers and add underscores to convert into attribute names
14
15
  HEADER_CONVERTER = lambda do |header|
@@ -36,6 +37,7 @@ module RailsAdminImport
36
37
  @rollback_on_error = false
37
38
  @update_if_exists = false
38
39
  @header_converter = HEADER_CONVERTER
40
+ @pass_filename = false
39
41
  @csv_options = {}
40
42
  end
41
43
  end
@@ -17,7 +17,7 @@ module RailsAdminImport
17
17
 
18
18
  # A method that yields a hash of attributes for each record to import
19
19
  def each_record
20
- CSV.foreach(filename, csv_options) do |row|
20
+ CSV.foreach(filename, **csv_options) do |row|
21
21
  attr = convert_to_attributes(row)
22
22
  yield attr unless attr.all? { |field, value| value.blank? }
23
23
  end
@@ -7,7 +7,7 @@ module RailsAdminImport
7
7
  @logger = Logger.new(File.join(Rails.root, "log", log_file_name))
8
8
  end
9
9
  end
10
-
10
+
11
11
  def info(message)
12
12
  if RailsAdminImport.config.logging
13
13
  @logger.info message
@@ -70,6 +70,10 @@ module RailsAdminImport
70
70
  end
71
71
 
72
72
  def import_record(record)
73
+ if params["file"] && RailsAdminImport.config.pass_filename
74
+ record.merge!({:filename_importer => params[:file].original_filename})
75
+ end
76
+
73
77
  perform_model_callback(import_model.model, :before_import_find, record)
74
78
 
75
79
  if update_lookup && !(update_lookup - record.keys).empty?
@@ -1,3 +1,3 @@
1
1
  module RailsAdminImport
2
- VERSION = "2.2.0"
2
+ VERSION = "2.3.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steph Skardal
8
8
  - Julien Vanier
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-02-25 00:00:00.000000000 Z
12
+ date: 2021-06-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.6'
48
+ version: '0.7'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0.6'
55
+ version: '0.7'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: simple_xlsx_reader
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -67,7 +67,7 @@ dependencies:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: '1.0'
70
- description:
70
+ description:
71
71
  email:
72
72
  - steph@endpoint.com
73
73
  - jvanier@gmail.com
@@ -106,7 +106,7 @@ homepage: https://github.com/stephskardal/rails_admin_import
106
106
  licenses:
107
107
  - MIT
108
108
  metadata: {}
109
- post_install_message:
109
+ post_install_message:
110
110
  rdoc_options: []
111
111
  require_paths:
112
112
  - lib
@@ -121,9 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  - !ruby/object:Gem::Version
122
122
  version: '0'
123
123
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 2.5.1
126
- signing_key:
124
+ rubygems_version: 3.2.15
125
+ signing_key:
127
126
  specification_version: 4
128
127
  summary: Import functionality for Rails Admin
129
128
  test_files: []