rails_admin_import 0.1.9 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{RELEASE_NOTES → CHANGELOG.md} +0 -0
- data/MIT-LICENSE +1 -1
- data/README.md +193 -50
- data/Rakefile +8 -32
- data/app/views/rails_admin/main/_results.html.haml +18 -0
- data/app/views/rails_admin/main/_section.html.haml +10 -0
- data/app/views/rails_admin/main/import.html.haml +72 -0
- data/config/locales/import.en.yml +39 -0
- data/lib/rails_admin_import/action.rb +45 -0
- data/lib/rails_admin_import/config/legacy_model.rb +42 -0
- data/lib/rails_admin_import/config/sections/import.rb +30 -0
- data/lib/rails_admin_import/config.rb +16 -37
- data/lib/rails_admin_import/formats/csv_importer.rb +75 -0
- data/lib/rails_admin_import/formats/dummy_importer.rb +16 -0
- data/lib/rails_admin_import/formats/file_importer.rb +45 -0
- data/lib/rails_admin_import/formats/json_importer.rb +31 -0
- data/lib/rails_admin_import/formats.rb +28 -0
- data/lib/rails_admin_import/import_logger.rb +7 -4
- data/lib/rails_admin_import/import_model.rb +94 -0
- data/lib/rails_admin_import/importer.rb +224 -0
- data/lib/rails_admin_import/rails_admin_plugin.rb +23 -0
- data/lib/rails_admin_import/version.rb +1 -1
- data/lib/rails_admin_import.rb +5 -39
- metadata +83 -13
- data/app/views/rails_admin/main/import.html.erb +0 -143
- data/lib/rails_admin_import/config/base.rb +0 -62
- data/lib/rails_admin_import/config/model.rb +0 -23
- data/lib/rails_admin_import/import.rb +0 -218
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7926c16cbb6726c90f684e87620b811157065162
|
4
|
+
data.tar.gz: 99dbbe4c5a95dc163a870ceeff25bcba0801e97d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 620e0bf33570d47b6b93ea5b8b41f96524153d57b302bd08af316733cb936869f92cb64013009bdb228317b2478961a48a05e4ce90a15bcc729fe2e74b1dff7a
|
7
|
+
data.tar.gz: bd7a26c0ee8300e915ba4cf95146220e24b6b56998b4c43bb19b8d68a5237561ebfb78551059ba10a156571972f69bb809c47b5a50e1ff34723015508289fafb
|
File without changes
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,74 +1,217 @@
|
|
1
|
-
|
2
|
-
========
|
1
|
+
# Rails Admin Import
|
3
2
|
|
4
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/monkbroc/rails_admin_import.svg?branch=master)](https://travis-ci.org/monkbroc/rails_admin_import)
|
5
4
|
|
5
|
+
Plugin functionality to add generic import to Rails Admin from CSV and JSON files
|
6
6
|
|
7
|
-
|
8
|
-
========
|
7
|
+
*This Readme is for version 1.0. If you are still using version 0.1.x, see [this branch](https://github.com/stephskardal/rails_admin_import/tree/legacy)*
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
Installation
|
13
|
-
========
|
9
|
+
## Installation
|
14
10
|
|
15
11
|
* First, add to Gemfile:
|
16
|
-
|
17
|
-
gem "rails_admin_import"
|
18
12
|
|
19
|
-
|
13
|
+
```
|
14
|
+
gem "rails_admin_import", "~> 1.0.0"
|
15
|
+
```
|
16
|
+
|
17
|
+
* Define configuration in `config/initializers/rails_admin_import.rb`:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
RailsAdmin.config do |config|
|
21
|
+
# REQUIRED:
|
22
|
+
# Include the import action
|
23
|
+
# See https://github.com/sferik/rails_admin/wiki/Actions
|
24
|
+
config.actions do
|
25
|
+
all
|
26
|
+
import
|
27
|
+
end
|
28
|
+
|
29
|
+
# Optional:
|
30
|
+
# Configure global RailsAdminImport options
|
31
|
+
config.configure_with(:import) do |config|
|
32
|
+
config.logging = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# Optional:
|
36
|
+
# Configure model-specific options using standard RailsAdmin DSL
|
37
|
+
# See https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL
|
38
|
+
config.model 'User' do
|
39
|
+
import do
|
40
|
+
include_all_fields
|
41
|
+
exclude_fields :secret_token
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
* If you are using CanCanCan for authorization, add to ability.rb to specify which models can be imported:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
can :import, [User, Model1, Model2]
|
51
|
+
```
|
52
|
+
|
53
|
+
## File format
|
54
|
+
|
55
|
+
### CSV
|
56
|
+
|
57
|
+
The first line must contain attribute names. They will be converted to lowercase and underscored (First Name ==> first_name).
|
58
|
+
|
59
|
+
For "many" associations, you may include multiple columns with the same header in the CSV file.
|
60
|
+
|
61
|
+
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.
|
62
|
+
|
63
|
+
Example
|
64
|
+
|
65
|
+
```
|
66
|
+
First name,Last name,Team,Team
|
67
|
+
Peter,Gibbons,IT,Management
|
68
|
+
Michael,Bolton,IT,
|
69
|
+
```
|
70
|
+
|
71
|
+
### JSON
|
72
|
+
|
73
|
+
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.
|
74
|
+
|
75
|
+
|
76
|
+
## Configuration
|
77
|
+
|
78
|
+
### Global configuration options
|
79
|
+
|
80
|
+
* __logging__ (default `false`): Save a copy of each imported file to log/import and a detailed import log to log/rails_admin_import.log
|
81
|
+
|
82
|
+
* __line_item_limit__ (default `1000`): max number of items that can be imported at one time. TODO: Currently this is suggested but not enforced.
|
83
|
+
|
84
|
+
* __rollback_on_error__ (default `false`): import records in a transaction and rollback if there is one error. Only for ActiveRecord, not Mongoid.
|
85
|
+
|
86
|
+
* __header_converter__ (default `nil`): 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.
|
87
|
+
|
88
|
+
### Model-specific configuration
|
20
89
|
|
21
|
-
|
90
|
+
Use [standard RailsAdmin DSL](https://github.com/sferik/rails_admin/wiki/Railsadmin-DSL) to add or remove fields.
|
22
91
|
|
23
|
-
*
|
92
|
+
* To change the default attribute that will be used to find associations on import, set `mapping_key` (default attribute is `name`)
|
24
93
|
|
25
|
-
|
94
|
+
```ruby
|
95
|
+
RailsAdmin.config do |config|
|
96
|
+
config.model 'Ball' do
|
97
|
+
import do
|
98
|
+
mapping_key :color
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
```
|
26
103
|
|
27
|
-
*
|
104
|
+
* To include a specific list of fields:
|
28
105
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
106
|
+
```ruby
|
107
|
+
RailsAdmin.config do |config|
|
108
|
+
config.model 'User' do
|
109
|
+
import do
|
110
|
+
field :first_name
|
111
|
+
field :last_name
|
112
|
+
field :email
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
40
117
|
|
41
|
-
*
|
118
|
+
* To exclude specific fields:
|
42
119
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
120
|
+
```ruby
|
121
|
+
RailsAdmin.config do |config|
|
122
|
+
config.model 'User' do
|
123
|
+
import do
|
124
|
+
include_all_fields
|
125
|
+
exclude_fields :secret_token
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
47
130
|
|
48
|
-
|
49
|
-
|
50
|
-
|
131
|
+
* To add extra fields that will be set as attributes on the model and that will be passed to the import hook methods:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
RailsAdmin.config do |config|
|
135
|
+
config.model 'User' do
|
136
|
+
import do
|
137
|
+
include_all_fields
|
138
|
+
fields :special_import_token
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
## Import hooks
|
145
|
+
|
146
|
+
|
147
|
+
Define instance methods on your models to be hooked into the import process, if special/additional processing is required on the data:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
# some model
|
151
|
+
class User < ActiveRecord::Base
|
152
|
+
def before_import_save(record)
|
153
|
+
# Your custom special sauce
|
154
|
+
end
|
155
|
+
|
156
|
+
def after_import_save(record)
|
157
|
+
# Your custom special sauce
|
158
|
+
end
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
For example, you could
|
163
|
+
|
164
|
+
* Set an attribute on a Devise User model to skip checking for a password when importing a new model.
|
165
|
+
|
166
|
+
* Download a file based on a URL from the import file and set a Paperclip file attribute on the model.
|
167
|
+
|
168
|
+
|
169
|
+
## ORM: ActiveRecord and Mongoid
|
170
|
+
|
171
|
+
The gem is tested to work with ActiveRecord and Mongoid.
|
172
|
+
|
173
|
+
Support for Mongoid is early, so if you can suggest improvements (especially around importing embedded models), open an issue.
|
174
|
+
|
175
|
+
|
176
|
+
## Upgrading
|
177
|
+
|
178
|
+
* Move global config to `config.configure_with(:import)` in `config/initializers/rails_admin_import.rb`.
|
179
|
+
|
180
|
+
* Move the field definitions to `config.model 'User' do; import do; // ...` in `config/initializers/rails_admin_import.rb`.
|
181
|
+
|
182
|
+
* No need to mount RailsAdminImport in `config/routes.rb` (RailsAdmin must still be mounted).
|
183
|
+
|
184
|
+
* Update model import hooks to take 1 hash argument instead of 2 arrays with values and headers.
|
185
|
+
|
186
|
+
* 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.
|
187
|
+
|
188
|
+
## Run tests
|
189
|
+
|
190
|
+
1. Clone the repository to your machine
|
191
|
+
|
192
|
+
git clone https://github.com/stephskardal/rails_admin_import
|
193
|
+
|
194
|
+
2. Run `bundle install`
|
195
|
+
3. Run `rspec`
|
196
|
+
|
197
|
+
The structure of the tests is taken from the Rails Admin gem.
|
51
198
|
|
52
|
-
|
199
|
+
## Authors
|
53
200
|
|
54
|
-
|
55
|
-
...
|
56
|
-
import
|
57
|
-
...
|
58
|
-
end
|
201
|
+
Original author: [Steph Skardal](https://github.com/stephskardal)
|
59
202
|
|
60
|
-
|
203
|
+
Maintainer (since May 2015): [Julien Vanier](https://github.com/monkbroc)
|
61
204
|
|
62
205
|
|
63
|
-
|
64
|
-
mapping needs to be updated to use klasses instead of symbols
|
206
|
+
## Contributing
|
65
207
|
|
66
|
-
|
67
|
-
========
|
208
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
68
209
|
|
69
|
-
|
210
|
+
- [Report bugs](https://github.com/ankane/blazer/issues)
|
211
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/blazer/pulls)
|
212
|
+
- Write, clarify, or fix documentation
|
213
|
+
- Suggest or add new features
|
70
214
|
|
71
|
-
Copyright
|
72
|
-
========
|
215
|
+
## Copyright
|
73
216
|
|
74
|
-
Copyright (c)
|
217
|
+
Copyright (c) 2015 End Point, Steph Skardal and contributors. See LICENSE.txt for further details.
|
data/Rakefile
CHANGED
@@ -1,39 +1,15 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
|
-
begin
|
3
|
-
require 'bundler/setup'
|
4
|
-
rescue LoadError
|
5
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
-
end
|
7
|
-
begin
|
8
|
-
require 'rdoc/task'
|
9
|
-
rescue LoadError
|
10
|
-
require 'rdoc/rdoc'
|
11
|
-
require 'rake/rdoctask'
|
12
|
-
RDoc::Task = Rake::RDocTask
|
13
|
-
end
|
14
|
-
|
15
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
-
rdoc.rdoc_dir = 'rdoc'
|
17
|
-
rdoc.title = 'RailsAdminImport'
|
18
|
-
rdoc.options << '--line-numbers'
|
19
|
-
rdoc.rdoc_files.include('README.rdoc')
|
20
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
-
end
|
22
|
-
|
23
|
-
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
24
|
-
|
25
|
-
|
26
2
|
|
3
|
+
require 'bundler'
|
27
4
|
Bundler::GemHelper.install_tasks
|
28
5
|
|
29
|
-
|
6
|
+
# Piggyback off the Rails Admin rake tasks to set up the CI environment
|
7
|
+
spec = Gem::Specification.find_by_name 'rails_admin'
|
8
|
+
Dir["#{spec.gem_dir}/lib/tasks/*.rake"].each { |rake| load rake }
|
30
9
|
|
31
|
-
|
32
|
-
|
33
|
-
t.libs << 'test'
|
34
|
-
t.pattern = 'test/**/*_test.rb'
|
35
|
-
t.verbose = false
|
36
|
-
end
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
37
12
|
|
13
|
+
task test: :spec
|
38
14
|
|
39
|
-
task :
|
15
|
+
task default: :spec
|
@@ -0,0 +1,18 @@
|
|
1
|
+
- if @results && @results[:success].any?
|
2
|
+
.alert.alert-success.form-horizontal.denser
|
3
|
+
%fieldset
|
4
|
+
%legend
|
5
|
+
%i.icon-chevron-right
|
6
|
+
= @results[:success_message]
|
7
|
+
%ul.control-group{style: 'display: none'}
|
8
|
+
- @results[:success].each do |message|
|
9
|
+
%li= message
|
10
|
+
- if @results && @results[:error].any?
|
11
|
+
.alert.alert-danger.form-horizontal.denser
|
12
|
+
%fieldset
|
13
|
+
%legend
|
14
|
+
%i.icon-chevron-down
|
15
|
+
= @results[:error_message]
|
16
|
+
%ul.control-group
|
17
|
+
- @results[:error].each do |message|
|
18
|
+
%li= message
|
@@ -0,0 +1,10 @@
|
|
1
|
+
- if fields.any?
|
2
|
+
.form-group.control-group
|
3
|
+
%label.col-sm-2.control-label= t("admin.import.#{section}")
|
4
|
+
.col-sm-10.controls
|
5
|
+
%ul.list-unstyled
|
6
|
+
- fields.each do |field|
|
7
|
+
%li
|
8
|
+
%label= capitalize_first_letter(field.label)
|
9
|
+
%p.help-block= t("admin.import.help.#{section}")
|
10
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
= render "results"
|
2
|
+
|
3
|
+
= form_tag import_path(@abstract_model), :multipart => true, class: 'form-horizontal denser' do
|
4
|
+
|
5
|
+
%input{name: "send_data", type: "hidden", value: "true"}/
|
6
|
+
%fieldset
|
7
|
+
%legend
|
8
|
+
%i.icon-chevron-down
|
9
|
+
= t('admin.import.legend.fields')
|
10
|
+
|
11
|
+
= render "section", section: "model_fields", fields: @import_model.model_fields
|
12
|
+
= render "section", section: "association_fields", fields: @import_model.association_fields
|
13
|
+
|
14
|
+
%fieldset
|
15
|
+
%legend
|
16
|
+
%i.icon-chevron-down
|
17
|
+
= t('admin.import.legend.upload')
|
18
|
+
.form-group.control-group
|
19
|
+
%label.col-sm-2.control-label{for: "file"}= t("admin.import.file")
|
20
|
+
.col-sm-10.controls
|
21
|
+
= file_field_tag :file, :class => "form-control"
|
22
|
+
%p.help-block= t('admin.import.help.file_limit', limit: RailsAdminImport.config.line_item_limit)
|
23
|
+
.form-group.control-group
|
24
|
+
%label.col-sm-2.control-label{for: "import_format"}= t("admin.import.format")
|
25
|
+
.col-sm-10.controls
|
26
|
+
= select_tag 'import_format',
|
27
|
+
options_for_select(RailsAdminImport::Formats.all),
|
28
|
+
data: { enumeration: true }
|
29
|
+
.form-group.control-group
|
30
|
+
%label.col-sm-2.control-label{for: "encoding"}= t("admin.import.encoding")
|
31
|
+
.col-sm-10.controls
|
32
|
+
= select_tag 'encoding',
|
33
|
+
options_for_select(RailsAdmin::CSVConverter::TARGET_ENCODINGS),
|
34
|
+
include_blank: true, data: { enumeration: true }
|
35
|
+
%p.help-block= t('admin.import.help.encoding', name: 'UTF-8')
|
36
|
+
.form-group.control-group
|
37
|
+
%label.col-sm-2.control-label= t("admin.import.update_if_exists")
|
38
|
+
.col-sm-10.controls
|
39
|
+
= check_box_tag :update_if_exists, '1', false, :class => "form-control"
|
40
|
+
%p.help-block= t('admin.import.help.update_if_exists')
|
41
|
+
.form-group.control-group
|
42
|
+
%label.col-sm-2.control-label{for: "update_lookup"}= t("admin.import.update_lookup")
|
43
|
+
.col-sm-10.controls
|
44
|
+
= select_tag 'update_lookup',
|
45
|
+
options_for_select(@import_model.model_fields.map(&:name),
|
46
|
+
@import_model.config.mapping_key.to_s), data: { enumeration: true }
|
47
|
+
|
48
|
+
- unless @import_model.association_fields.empty?
|
49
|
+
%fieldset
|
50
|
+
%legend
|
51
|
+
%i.icon-chevron-down
|
52
|
+
= t('admin.import.legend.mapping')
|
53
|
+
|
54
|
+
- @import_model.association_fields.each do |field|
|
55
|
+
.form-group.control-group
|
56
|
+
%label.col-sm-2.control-label
|
57
|
+
= capitalize_first_letter(field.label)
|
58
|
+
= t("admin.import.mapping")
|
59
|
+
.col-sm-10.controls
|
60
|
+
= select_tag "associations[#{field.name}]",
|
61
|
+
options_for_select(@import_model.associated_model_fields(field).map(&:name),
|
62
|
+
@import_model.associated_config(field).mapping_key.to_s), data: { enumeration: true }
|
63
|
+
|
64
|
+
%br
|
65
|
+
.form-actions
|
66
|
+
%input{type: :hidden, name: 'return_to', value: (request.params[:return_to].presence || request.referer)}
|
67
|
+
%button.btn.btn-primary{type: "submit", name: "commit", data: {disable_with: "Uploading..."} }
|
68
|
+
%i.icon-white.icon-ok
|
69
|
+
= t("admin.form.save")
|
70
|
+
%button.btn{type: "submit", name: "_continue"}
|
71
|
+
%i.icon-remove
|
72
|
+
= t("admin.form.cancel")
|
@@ -9,3 +9,42 @@ en:
|
|
9
9
|
link: "Import"
|
10
10
|
bulk_link: "Import"
|
11
11
|
done: "Imported"
|
12
|
+
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"
|
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
|
+
old_import_hook: >
|
38
|
+
The import hook %{model}.%{method} should take only 1 argument.
|
39
|
+
Data may not imported correctly.
|
40
|
+
See Upgrading section readme in Rails Admin Import.
|
41
|
+
association_not_found: "Association not found. %{error}"
|
42
|
+
help:
|
43
|
+
model_fields: "The fields above may be included in the import file."
|
44
|
+
association_fields: >
|
45
|
+
These fields map to other tables in the database, lookup via attribute selected below.
|
46
|
+
For "many" associations, you may include multiple columns with the same header in the CSV file.
|
47
|
+
update_if_exists: "Update records found with the lookup field below instead of creating new records"
|
48
|
+
file_limit: "Please limit upload file to %{limit} line items."
|
49
|
+
encoding: "Choose file encoding. Leave empty to auto-detect. Ignored for JSON."
|
50
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "rails_admin/config/actions"
|
2
|
+
|
3
|
+
module RailsAdmin
|
4
|
+
module Config
|
5
|
+
module Actions
|
6
|
+
class Import < Base
|
7
|
+
RailsAdmin::Config::Actions.register(self)
|
8
|
+
|
9
|
+
register_instance_option :collection do
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
register_instance_option :http_methods do
|
14
|
+
[:get, :post]
|
15
|
+
end
|
16
|
+
|
17
|
+
register_instance_option :controller do
|
18
|
+
proc do
|
19
|
+
@import_model = RailsAdminImport::ImportModel.new(@abstract_model)
|
20
|
+
|
21
|
+
if request.post?
|
22
|
+
record_importer = RailsAdminImport::Formats.for(
|
23
|
+
params[:import_format] || "csv", @import_model, params)
|
24
|
+
|
25
|
+
if record_importer.valid?
|
26
|
+
importer = RailsAdminImport::Importer.new(
|
27
|
+
@import_model, params)
|
28
|
+
|
29
|
+
@results = importer.import(record_importer.each)
|
30
|
+
else
|
31
|
+
flash[:error] = record_importer.error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
render action: @action.template_name
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
register_instance_option :link_icon do
|
40
|
+
"icon-folder-open"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module RailsAdminImport
|
2
|
+
module Config
|
3
|
+
class LegacyModel
|
4
|
+
attr_reader :model_name
|
5
|
+
def initialize(model_name)
|
6
|
+
@model_name = model_name
|
7
|
+
end
|
8
|
+
|
9
|
+
def label(_value)
|
10
|
+
# Ignored now
|
11
|
+
# RailsAdmin object_label_method will be used
|
12
|
+
end
|
13
|
+
|
14
|
+
def mapping_key(value)
|
15
|
+
config = RailsAdmin.config(model_name)
|
16
|
+
config.mapping_key(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def excluded_fields(values)
|
20
|
+
config = RailsAdmin.config(model_name)
|
21
|
+
|
22
|
+
# Call appropriate Rails Admin field list methods
|
23
|
+
config.import do
|
24
|
+
include_all_fields
|
25
|
+
exclude_fields *values
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def extra_fields(values)
|
30
|
+
config = RailsAdmin.config(model_name)
|
31
|
+
|
32
|
+
# Call appropriate Rails Admin field list methods
|
33
|
+
config.import do
|
34
|
+
include_all_fields
|
35
|
+
values.each do |value|
|
36
|
+
field value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "rails_admin/config/sections/base"
|
2
|
+
|
3
|
+
module RailsAdmin
|
4
|
+
module Config
|
5
|
+
module Sections
|
6
|
+
# Configuration of the navigation view
|
7
|
+
class Import < RailsAdmin::Config::Sections::Base
|
8
|
+
register_instance_option(:mapping_key) do
|
9
|
+
:name
|
10
|
+
end
|
11
|
+
|
12
|
+
register_instance_option(:default_excluded_fields) do
|
13
|
+
[:id, :_id, :created_at, :updated_at]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
section = RailsAdmin::Config::Sections::Import
|
21
|
+
name = :import
|
22
|
+
|
23
|
+
# Manually add to Rails Admin as a model configuration section until
|
24
|
+
# there is a better API to do this
|
25
|
+
RailsAdmin::Config::Model.send(:define_method, name) do |&block|
|
26
|
+
@sections = {} unless @sections
|
27
|
+
@sections[name] = section.new(self) unless @sections[name]
|
28
|
+
@sections[name].instance_eval(&block) if block
|
29
|
+
@sections[name]
|
30
|
+
end
|
@@ -1,52 +1,31 @@
|
|
1
|
-
require
|
2
|
-
# require 'active_support/core_ext/class/attribute_accessors'
|
1
|
+
require "rails_admin_import/config/legacy_model"
|
3
2
|
|
4
3
|
module RailsAdminImport
|
5
4
|
module Config
|
6
5
|
class << self
|
7
|
-
# Stores model configuration objects in a hash identified by model's class
|
8
|
-
# name.
|
9
|
-
#
|
10
|
-
# @see RailsAdminImport::Config.model
|
11
|
-
attr_reader :registry
|
12
6
|
attr_accessor :logging
|
13
7
|
attr_accessor :line_item_limit
|
8
|
+
attr_accessor :rollback_on_error
|
9
|
+
attr_accessor :header_converter
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
#
|
26
|
-
# @see RailsAdminImport::Config.registry
|
27
|
-
def model(entity, &block)
|
28
|
-
key = entity.name.to_sym
|
29
|
-
|
30
|
-
config = @registry[key] ||= RailsAdminImport::Config::Model.new(entity)
|
31
|
-
config.instance_eval(&block) if block
|
32
|
-
config
|
11
|
+
def model(model_name, &block)
|
12
|
+
unless @deprecation_shown
|
13
|
+
warn "RailsAdminImport::Config#model is deprecated. " \
|
14
|
+
"Add a import section for your model inside the rails_admin " \
|
15
|
+
"config block. See the Readme.md for more details"
|
16
|
+
@deprecation_shown = true
|
17
|
+
end
|
18
|
+
legacy_config = RailsAdminImport::Config::LegacyModel.new(model_name)
|
19
|
+
legacy_config.instance_eval(&block) if block
|
20
|
+
legacy_config
|
33
21
|
end
|
34
|
-
|
22
|
+
|
35
23
|
# Reset all configurations to defaults.
|
36
|
-
#
|
37
|
-
# @see RailsAdminImport::Config.registry
|
38
24
|
def reset
|
39
|
-
@registry = {}
|
40
25
|
@logging = false
|
41
26
|
@line_item_limit = 1000
|
42
|
-
|
43
|
-
|
44
|
-
# Reset a provided model's configuration.
|
45
|
-
#
|
46
|
-
# @see RailsAdminImport::Config.registry
|
47
|
-
def reset_model(model)
|
48
|
-
key = model.kind_of?(Class) ? model.name.to_sym : model.to_sym
|
49
|
-
@registry.delete(key)
|
27
|
+
@rollback_on_error = false
|
28
|
+
@header_converter = nil
|
50
29
|
end
|
51
30
|
end
|
52
31
|
|