rails_admin_import 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +91 -5
- data/Rakefile +3 -1
- data/config/locales/import.en.yml +1 -0
- data/lib/rails_admin_import/formats/csv_importer.rb +1 -1
- data/lib/rails_admin_import/importer.rb +10 -8
- data/lib/rails_admin_import/version.rb +1 -1
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8f4b64d5b102c3aaa1a7e40b526a4a44f7b1633
|
4
|
+
data.tar.gz: 188508962de1312370416f325deb729f7f3233e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95db49681fed111f535b241775b64cd7e302f10ade105473fc5e930c194f180dcdfdd6e82b93ee817438d74e97da33edcef91b5c9e0d109099678a43d1900874
|
7
|
+
data.tar.gz: 830926cf365344e76ac1fb5bec2a77b6ab85bf03f318253372f52e152a53fc08da97055a76d1bb599a41382fe0c9cc3175c2c8d892921c747b168617c1cf6f7d
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
# 1.4.0 / 2016-02-26
|
4
|
+
|
5
|
+
- Implement row limit (default maximum is 1000 rows). Thanks @codealchemy!
|
6
|
+
- Document mapping keys. Thanks @zrisher!
|
7
|
+
- Ignore columns with blank headers in CSV files
|
8
|
+
|
3
9
|
# 1.3.1 / 2016-02-11
|
4
10
|
|
5
11
|
- Bugfix: Create log/import directory when logging config option is enabled. Thanks @yovasx2!
|
data/README.md
CHANGED
@@ -51,11 +51,93 @@ cannot :import, :all
|
|
51
51
|
can :import, [User, Model1, Model2]
|
52
52
|
```
|
53
53
|
|
54
|
-
##
|
54
|
+
## Usage
|
55
|
+
|
56
|
+
Model instances can be both created and updated from import data. Any fields
|
57
|
+
can be imported as long as they are allowed by the model's configuration.
|
58
|
+
Associated records can be looked up for both singular and plural relationships.
|
59
|
+
Both updating existing records and associating records requires the use of
|
60
|
+
**mapping keys**.
|
61
|
+
|
62
|
+
### Mapping Keys
|
63
|
+
|
64
|
+
Every importable class has a mapping key that uniquely identifies its
|
65
|
+
instances. The value for this field can then be provided in import data, either
|
66
|
+
to update the existing record or to attach it through an association to another
|
67
|
+
model. This concept exists because `id`s are often not constant when moving
|
68
|
+
records between data stores.
|
69
|
+
|
70
|
+
For example, a `User` model may have an `email` field. When uploading a set
|
71
|
+
of users where some already exist in our database, we can select "email" as
|
72
|
+
our mapping key and then provide that field on each record in our data,
|
73
|
+
allowing us to update existing records with matching emails.
|
74
|
+
|
75
|
+
Using a csv formatted example:
|
76
|
+
```
|
77
|
+
Email,First name,Last name
|
78
|
+
peter.gibbons@initech.com,Peter,Gibbons
|
79
|
+
michael.bolton@initech.com,Michael,Bolton
|
80
|
+
```
|
81
|
+
would look for existing users with those emails. If one was found, its name
|
82
|
+
fields would be updated. Otherwise, a new one would be created.
|
83
|
+
|
84
|
+
Similarly, if each user has favorite books, we could set the mapping key
|
85
|
+
for `Book` to be `isbn` and then include the isbn for their books within each
|
86
|
+
user record. The syntax for this is to use the name of the associated model as
|
87
|
+
the field name, no matter what actual mapping key has been selected. So
|
88
|
+
a user record would have one or more fields named "Book" that include each
|
89
|
+
associated book's ISBN.
|
90
|
+
|
91
|
+
Again using a csv formatted example:
|
92
|
+
```
|
93
|
+
Email, Book, Book, Book
|
94
|
+
peter.gibbons@initech.com, 9781119997870, 9780671027032
|
95
|
+
michael.bolton@initech.com, 9780446677479
|
96
|
+
```
|
97
|
+
would look up books with those ISBNs and attach them to those users.
|
98
|
+
|
99
|
+
Mapping keys can be selected on the import page. Their defaults can also be
|
100
|
+
globally configured in the config file:
|
101
|
+
|
102
|
+
```
|
103
|
+
RailsAdmin.config do |config|
|
104
|
+
config.model 'User' do
|
105
|
+
import do
|
106
|
+
mapping_key :email
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
Note that a matched record must exist when attaching associated models, or the
|
113
|
+
imported record will fail and be skipped.
|
114
|
+
|
115
|
+
Complex associations (`has_one ..., :through` or polymorphic associations)
|
116
|
+
need to be dealt with via custom logic called by one of the import hooks
|
117
|
+
(see below for more detail on using hooks). If we wanted to import
|
118
|
+
`Service`s and attach them to a `User`, but the user relationship
|
119
|
+
existed through an intermediary model called `ServiceProvider`, we could
|
120
|
+
provide a `user_email` field in our records and handle the actual
|
121
|
+
association with an import hook:
|
122
|
+
|
123
|
+
```
|
124
|
+
class Service < ActiveRecord::Base
|
125
|
+
belongs_to :service_provider
|
126
|
+
has_one :user, through: :service_provider
|
127
|
+
|
128
|
+
def before_import_save(record)
|
129
|
+
if (email = record[:user_email]) && (user = User.find_by_email(email))
|
130
|
+
self.service_provider = user.service_provider
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
### File format
|
55
137
|
|
56
138
|
The format is inferred by the extension (.csv, .json or .xlsx).
|
57
139
|
|
58
|
-
|
140
|
+
#### CSV
|
59
141
|
|
60
142
|
The first line must contain attribute names. They will be converted to lowercase and underscored (First Name ==> first_name).
|
61
143
|
|
@@ -71,11 +153,11 @@ Peter,Gibbons,IT,Management
|
|
71
153
|
Michael,Bolton,IT,
|
72
154
|
```
|
73
155
|
|
74
|
-
|
156
|
+
#### JSON
|
75
157
|
|
76
158
|
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.
|
77
159
|
|
78
|
-
|
160
|
+
#### XLSX
|
79
161
|
|
80
162
|
The Microsoft Excel XLM format (XLSX) is supported, but not the old binary Microsoft Excel format (XLS).
|
81
163
|
|
@@ -87,7 +169,7 @@ The expected rows and columns are the same as for the CSV format (first line con
|
|
87
169
|
|
88
170
|
* __logging__ (default `false`): Save a copy of each imported file to log/import and a detailed import log to log/rails_admin_import.log
|
89
171
|
|
90
|
-
* __line_item_limit__ (default `1000`): max number of items that can be imported at one time.
|
172
|
+
* __line_item_limit__ (default `1000`): max number of items that can be imported at one time.
|
91
173
|
|
92
174
|
* __rollback_on_error__ (default `false`): import records in a transaction and rollback if there is one error. Only for ActiveRecord, not Mongoid.
|
93
175
|
|
@@ -216,6 +298,10 @@ gem "rails_admin_import", "~> 1.2.0", require: "rails_admin_import/eager_load"
|
|
216
298
|
* Update model import hooks to take 1 hash argument instead of 2 arrays with values and headers.
|
217
299
|
|
218
300
|
* 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.
|
301
|
+
|
302
|
+
## Community-contributed translations
|
303
|
+
|
304
|
+
* [Spanish translation](https://gist.github.com/yovasx2/dc0e9512e6c6243f840c) by Giovanni Alberto
|
219
305
|
|
220
306
|
## Run tests
|
221
307
|
|
data/Rakefile
CHANGED
@@ -34,6 +34,7 @@ en:
|
|
34
34
|
create: "Failed to create %{name}: %{error}"
|
35
35
|
update: "Failed to update %{name}: %{error}"
|
36
36
|
general: "Error during import: %{error}"
|
37
|
+
line_item_limit: "Please limit upload file to %{limit} line items."
|
37
38
|
old_import_hook: >
|
38
39
|
The import hook %{model}.%{method} should take only 1 argument.
|
39
40
|
Data may not imported correctly.
|
@@ -61,7 +61,7 @@ module RailsAdminImport
|
|
61
61
|
|
62
62
|
def convert_to_attributes(row)
|
63
63
|
row.each_with_object({}) do |(field, value), record|
|
64
|
-
|
64
|
+
next if field.blank?
|
65
65
|
field = field.to_sym
|
66
66
|
if import_model.has_multiple_values?(field)
|
67
67
|
field = import_model.pluralize_field(field)
|
@@ -16,10 +16,13 @@ module RailsAdminImport
|
|
16
16
|
begin
|
17
17
|
init_results
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
|
20
|
+
if records.count > RailsAdminImport.config.line_item_limit
|
21
|
+
return results = {
|
22
|
+
success: [],
|
23
|
+
error: [I18n.t('admin.import.import_error.line_item_limit', limit: RailsAdminImport.config.line_item_limit)]
|
24
|
+
}
|
25
|
+
end
|
23
26
|
|
24
27
|
with_transaction do
|
25
28
|
records.each do |record|
|
@@ -64,7 +67,7 @@ module RailsAdminImport
|
|
64
67
|
def import_record(record)
|
65
68
|
if update_lookup && !record.has_key?(update_lookup)
|
66
69
|
raise UpdateLookupError, I18n.t("admin.import.missing_update_lookup")
|
67
|
-
end
|
70
|
+
end
|
68
71
|
|
69
72
|
object = find_or_create_object(record, update_lookup)
|
70
73
|
action = object.new_record? ? :create : :update
|
@@ -142,7 +145,7 @@ module RailsAdminImport
|
|
142
145
|
name: result_count,
|
143
146
|
action: I18n.t("admin.actions.import.done"))
|
144
147
|
end
|
145
|
-
|
148
|
+
|
146
149
|
def perform_model_callback(object, method_name, record)
|
147
150
|
if object.respond_to?(method_name)
|
148
151
|
# Compatibility: Old import hook took 2 arguments.
|
@@ -175,7 +178,7 @@ module RailsAdminImport
|
|
175
178
|
model = import_model.model
|
176
179
|
object = if update.present?
|
177
180
|
model.where(update => record[update]).first
|
178
|
-
end
|
181
|
+
end
|
179
182
|
|
180
183
|
if object.nil?
|
181
184
|
object = model.new(new_attrs)
|
@@ -221,4 +224,3 @@ module RailsAdminImport
|
|
221
224
|
end
|
222
225
|
end
|
223
226
|
end
|
224
|
-
|
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: 1.
|
4
|
+
version: 1.4.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: 2016-02-
|
12
|
+
date: 2016-02-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -140,4 +140,3 @@ signing_key:
|
|
140
140
|
specification_version: 4
|
141
141
|
summary: Import functionality for Rails Admin
|
142
142
|
test_files: []
|
143
|
-
has_rdoc:
|