active_admin_import 3.0.0.pre → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +43 -0
- data/.travis.yml +14 -6
- data/Gemfile +8 -8
- data/README.md +25 -33
- data/Rakefile +3 -2
- data/active_admin_import.gemspec +14 -17
- data/config/locales/fr.yml +23 -0
- data/config/locales/ja.yml +24 -0
- data/config/locales/ko.yml +23 -0
- data/config/locales/pt-BR.yml +23 -0
- data/config/locales/ru.yml +27 -0
- data/config/locales/uk.yml +27 -0
- data/lib/active_admin_import.rb +1 -2
- data/lib/active_admin_import/authorization.rb +1 -0
- data/lib/active_admin_import/dsl.rb +57 -41
- data/lib/active_admin_import/engine.rb +2 -3
- data/lib/active_admin_import/import_result.rb +9 -8
- data/lib/active_admin_import/importer.rb +18 -18
- data/lib/active_admin_import/model.rb +47 -36
- data/lib/active_admin_import/options.rb +34 -33
- data/lib/active_admin_import/version.rb +2 -1
- data/spec/fixtures/files/authors_with_tabs.tsv +3 -0
- data/spec/import_result_spec.rb +15 -9
- data/spec/import_spec.rb +148 -137
- data/spec/model_spec.rb +4 -3
- data/spec/spec_helper.rb +13 -25
- data/spec/support/active_model_lint.rb +5 -4
- data/spec/support/admin.rb +5 -9
- data/spec/support/rails_template.rb +10 -10
- data/tasks/test.rake +5 -4
- metadata +25 -24
- data/.hound.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a7b928539df8bb150300b72b8b2a0971a31a2a9
|
4
|
+
data.tar.gz: 8144a8f48472cce96498a3fd5ce1a5b4c1b530ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e04cb45397a7b3540d2c5302d99e154172cc08055dfb0a4f9d1311f48f56133c8928b5b85fe3d50a4d14f0c280f844e4e1b05c7f600b670c0e7dba7813ea0883
|
7
|
+
data.tar.gz: 175dc4931d433f53aedba0ec57d7a004c6e0c18f268ed1f8af711d70350e2e20e8e3e9cafa4c3c97a1df349036d3f54ecdb9b4fda964fd046376a8406e366a25
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- 'spec/fixtures/**/*'
|
5
|
+
|
6
|
+
Style/IndentationWidth:
|
7
|
+
Width: 2
|
8
|
+
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/Encoding:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Style/MultilineOperationIndentation:
|
16
|
+
EnforcedStyle: indented
|
17
|
+
|
18
|
+
Style/FirstParameterIndentation:
|
19
|
+
EnforcedStyle: special_for_inner_method_call_in_parentheses
|
20
|
+
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Max: 25
|
23
|
+
|
24
|
+
Metrics/LineLength:
|
25
|
+
Max: 120
|
26
|
+
|
27
|
+
Metrics/MethodLength:
|
28
|
+
Max: 40
|
29
|
+
|
30
|
+
Metrics/ClassLength:
|
31
|
+
Max: 250
|
32
|
+
|
33
|
+
Metrics/ModuleLength:
|
34
|
+
Max: 250
|
35
|
+
|
36
|
+
Metrics/CyclomaticComplexity:
|
37
|
+
Max: 8
|
38
|
+
|
39
|
+
Metrics/PerceivedComplexity:
|
40
|
+
Max: 8
|
41
|
+
|
42
|
+
Rails:
|
43
|
+
Enabled: true
|
data/.travis.yml
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
script: bundle exec rspec spec
|
2
|
+
env:
|
3
|
+
matrix:
|
4
|
+
- RAILS=4.2.6
|
5
|
+
- RAILS=5.0.0
|
2
6
|
rvm:
|
3
|
-
- 1.9
|
4
|
-
- 2.
|
5
|
-
- 2.1
|
6
|
-
|
7
|
+
- 2.1.9
|
8
|
+
- 2.2.5
|
9
|
+
- 2.3.1
|
10
|
+
matrix:
|
11
|
+
exclude:
|
12
|
+
- rvm: 2.1.9
|
13
|
+
env: RAILS=5.0.0 # Rails 5.0 requires Ruby 2.2.2 or newer
|
7
14
|
before_install:
|
8
|
-
- gem update --system
|
9
|
-
- gem
|
15
|
+
- gem update --system # use the very latest Rubygems
|
16
|
+
- rvm @global do gem uninstall bundler -a -x
|
17
|
+
- rvm @global do gem install bundler -v 1.14.6 # latest version known to work
|
data/Gemfile
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
source 'https://rubygems.org'
|
2
3
|
|
3
4
|
# Specify your gem's dependencies in active_admin_importable.gemspec
|
4
5
|
gemspec
|
6
|
+
|
7
|
+
|
5
8
|
group :test do
|
6
|
-
|
9
|
+
default_rails_version = '4.2.8'
|
10
|
+
rails_version = ENV['RAILS'] || default_rails_version
|
11
|
+
gem 'rails', rails_version
|
7
12
|
gem 'rspec-rails'
|
8
|
-
gem 'activeadmin', github: 'activeadmin' , ref: '54bede0558a99ab759f98f9b24e1b0144063a81e'
|
9
|
-
|
10
13
|
gem 'coveralls', require: false # Test coverage website. Go to https://coveralls.io
|
11
|
-
|
12
|
-
gem 'devise'
|
13
|
-
gem 'sass-rails'
|
14
14
|
gem 'sqlite3'
|
15
15
|
gem 'launchy'
|
16
16
|
gem 'database_cleaner'
|
17
17
|
gem 'capybara'
|
18
|
-
gem 'selenium-webdriver'
|
19
18
|
gem 'poltergeist'
|
20
|
-
|
19
|
+
gem 'jquery-ui-rails', '~> 5.0'
|
20
|
+
end
|
data/README.md
CHANGED
@@ -1,18 +1,16 @@
|
|
1
|
-
# ActiveAdminImport
|
1
|
+
# ActiveAdminImport
|
2
2
|
[The most fastest and efficient CSV import for Active Admin
|
3
3
|
with support of validations, bulk inserts and encodings handling](http://activeadmin-plugins.github.io/active_admin_import/)
|
4
|
-
|
5
4
|
|
6
5
|
|
7
|
-
|
6
|
+
|
7
|
+
[![Build Status](https://img.shields.io/travis/activeadmin-plugins/active_admin_import.svg)](https://travis-ci.org/activeadmin-plugins/active_admin_import)
|
8
8
|
[![Dependency Status](http://img.shields.io/gemnasium/activeadmin-plugins/active_admin_import.svg)](https://gemnasium.com/activeadmin-plugins/active_admin_import)
|
9
9
|
[![Coverage Status](https://coveralls.io/repos/activeadmin-plugins/active_admin_import/badge.svg)](https://coveralls.io/r/activeadmin-plugins/active_admin_import)
|
10
|
-
|
11
|
-
[![Code Climate](http://img.shields.io/codeclimate/github/activeadmin-plugins/active_admin_import.svg)](https://codeclimate.com/github/activeadmin-plugins/active_admin_import)
|
10
|
+
[![Code Climate](https://codeclimate.com/github/activeadmin-plugins/active_admin_import/badges/gpa.svg)](https://codeclimate.com/github/activeadmin-plugins/active_admin_import)
|
12
11
|
[![Gem Version](http://img.shields.io/gem/v/active_admin_import.svg)](https://rubygems.org/gems/active_admin_import)
|
13
12
|
[![License](http://img.shields.io/:license-mit-blue.svg)](http://Fivell.mit-license.org)
|
14
13
|
|
15
|
-
master can be used with AA 1.0.0 and Rails >= 4.1
|
16
14
|
|
17
15
|
|
18
16
|
#Installation
|
@@ -20,7 +18,7 @@ master can be used with AA 1.0.0 and Rails >= 4.1
|
|
20
18
|
Add this line to your application's Gemfile:
|
21
19
|
|
22
20
|
```ruby
|
23
|
-
gem "active_admin_import" , '
|
21
|
+
gem "active_admin_import" , '3.0.0'
|
24
22
|
|
25
23
|
```
|
26
24
|
or
|
@@ -47,7 +45,7 @@ And then execute:
|
|
47
45
|
<li> and more...</li>
|
48
46
|
</ol>
|
49
47
|
|
50
|
-
|
48
|
+
|
51
49
|
|
52
50
|
#### Basic usage
|
53
51
|
|
@@ -61,24 +59,26 @@ end
|
|
61
59
|
#### Options
|
62
60
|
Tool | Description
|
63
61
|
--------------------- | -----------
|
64
|
-
:back
|
65
|
-
:csv_options
|
66
|
-
:validate
|
67
|
-
:batch_size
|
62
|
+
:back |resource action to redirect after processing
|
63
|
+
:csv_options |hash with column separator, row separator, etc
|
64
|
+
:validate |bool means perform validations or not
|
65
|
+
:batch_size |integer value of max record count inserted by 1 query/transaction
|
68
66
|
:batch_transaction |bool (false by default), if transaction is used when batch importing and works when :validate is set to true
|
69
|
-
:before_import
|
70
|
-
:after_import
|
71
|
-
:before_batch_import
|
72
|
-
:after_batch_import
|
73
|
-
:on_duplicate_key_update|an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
|
74
|
-
:timestamps
|
75
|
-
:ignore
|
76
|
-
:template
|
77
|
-
:template_object
|
78
|
-
:resource_class
|
79
|
-
:resource_label
|
80
|
-
:plural_resource_label
|
81
|
-
:
|
67
|
+
:before_import |proc for before import action, hook called with importer object
|
68
|
+
:after_import |proc for after import action, hook called with importer object
|
69
|
+
:before_batch_import |proc for before each batch action, called with importer object
|
70
|
+
:after_batch_import |proc for after each batch action, called with importer object
|
71
|
+
:on_duplicate_key_update|an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE or Postgres 9.5+ ON CONFLICT DO UPDATE ability.
|
72
|
+
:timestamps |bool, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
|
73
|
+
:ignore |bool, tells activerecord-import to use MySQL's INSERT IGNORE ability
|
74
|
+
:template |custom template rendering
|
75
|
+
:template_object |object passing to view
|
76
|
+
:resource_class |resource class name
|
77
|
+
:resource_label |resource label value
|
78
|
+
:plural_resource_label |pluralized resource label value (default config.plural_resource_label)
|
79
|
+
:error_limit |Limit the number of errors reported (default `5`, set to `nil` for all)
|
80
|
+
:headers_rewrites |hash with key (csv header) - value (db column name) rows mapping
|
81
|
+
:if |Controls whether the 'Import' button is displayed. It supports a proc to be evaluated into a boolean value within the activeadmin render context.
|
82
82
|
|
83
83
|
|
84
84
|
|
@@ -104,11 +104,3 @@ Tool | Description
|
|
104
104
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
105
105
|
4. Push to the branch (`git push origin my-new-feature`)
|
106
106
|
5. Create new Pull Request
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
data/Rakefile
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'bundler'
|
2
3
|
require 'rake'
|
3
4
|
Bundler.setup
|
4
5
|
Bundler::GemHelper.install_tasks
|
5
6
|
|
6
7
|
# Import all our rake tasks
|
7
|
-
FileList['tasks/**/*.rake'].each { |task| import task }
|
8
|
+
FileList['tasks/**/*.rake'].each { |task| import task }
|
data/active_admin_import.gemspec
CHANGED
@@ -1,25 +1,22 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
# frozen_string_literal: true
|
2
3
|
require File.expand_path('../lib/active_admin_import/version', __FILE__)
|
3
4
|
|
4
5
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = [
|
6
|
-
gem.email = [
|
7
|
-
gem.description =
|
8
|
-
gem.summary =
|
9
|
-
gem.homepage =
|
6
|
+
gem.authors = ['Igor Fedoronchuk']
|
7
|
+
gem.email = ['fedoronchuk@gmail.com']
|
8
|
+
gem.description = 'The most efficient way to import for Active Admin'
|
9
|
+
gem.summary = 'ActiveAdmin import based on activerecord-import gem.'
|
10
|
+
gem.homepage = 'http://github.com/Fivell/active_admin_import'
|
10
11
|
gem.license = 'MIT'
|
11
|
-
gem.files = `git ls-files`.split(
|
12
|
+
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
|
12
13
|
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
13
14
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
-
gem.name =
|
15
|
-
gem.require_paths = [
|
16
|
-
gem.version = ActiveAdminImport::VERSION
|
17
|
-
|
18
|
-
|
19
|
-
gem.add_runtime_dependency '
|
20
|
-
gem.
|
21
|
-
|
22
|
-
gem.add_runtime_dependency 'rubyzip', '~> 1.0', '>= 1.0.0'
|
23
|
-
gem.add_dependency "rails", ">= 4.0"
|
24
|
-
|
15
|
+
gem.name = 'active_admin_import'
|
16
|
+
gem.require_paths = ['lib']
|
17
|
+
gem.version = ActiveAdminImport::VERSION
|
18
|
+
gem.add_runtime_dependency 'activerecord-import', '~> 0.17.0'
|
19
|
+
gem.add_runtime_dependency 'rchardet', '~> 1.6'
|
20
|
+
gem.add_runtime_dependency 'rubyzip', '~> 1.2'
|
21
|
+
gem.add_dependency 'activeadmin', '~> 1.0'
|
25
22
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
fr:
|
2
|
+
active_admin:
|
3
|
+
import: "Importer"
|
4
|
+
active_admin_import:
|
5
|
+
file: 'Fichier'
|
6
|
+
file_error: "Erreur: %{message}"
|
7
|
+
file_format_error: "Vous ne pouvez importer qu'un fichier csv valide"
|
8
|
+
file_empty_error: "Vous ne pouvez pas importer de ficher vide"
|
9
|
+
no_file_error: "Veuillez sélectionner un fichier"
|
10
|
+
details: "Veuillez sélectionner un fichier à importer"
|
11
|
+
imported:
|
12
|
+
one: "Importé 1 %{model} avec succès"
|
13
|
+
other: "Importé %{count} %{plural_model} avec succès"
|
14
|
+
failed:
|
15
|
+
one: "Impossible d'importer 1 %{model}: %{message}"
|
16
|
+
other: "Impossible d'importer %{count} %{plural_model}: %{message}"
|
17
|
+
import_model: "Importer %{plural_model}"
|
18
|
+
import_btn: "Importer"
|
19
|
+
import_btn_disabled: "Veuillez patienter..."
|
20
|
+
csv_options: "Options CSV"
|
21
|
+
col_sep: 'Sép de col'
|
22
|
+
row_sep: 'Sep de ligne'
|
23
|
+
quote_char: 'Guillemets'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
ja:
|
2
|
+
active_admin:
|
3
|
+
import: "インポート"
|
4
|
+
active_admin_import:
|
5
|
+
file: "ファイル"
|
6
|
+
file_error: "エラー: %{message}"
|
7
|
+
file_format_error: "有効なCSVファイルでないとインポートできません"
|
8
|
+
file_empty_error: "空ファイルはインポートできません"
|
9
|
+
no_file_error: "インポートするファイルを選択してください"
|
10
|
+
details: "インポートするファイルを選択してください"
|
11
|
+
imported:
|
12
|
+
one: "1つの%{model}のインポートに成功しました"
|
13
|
+
other: "%{count}つの%{plural_model}のインポートに成功しました"
|
14
|
+
failed:
|
15
|
+
one: "1つの%{model}のインポートに失敗しました: %{message}"
|
16
|
+
other: "%{count}つの%{plural_model}のインポートに失敗しました: %{message}"
|
17
|
+
import_model: "%{plural_model}をインポートする"
|
18
|
+
import_btn: "インポート"
|
19
|
+
import_btn_disabled: "お待ちください..."
|
20
|
+
csv_options: "CSVオプション"
|
21
|
+
col_sep: "カラム区切り"
|
22
|
+
row_sep: "行区切り"
|
23
|
+
quote_char: "クォート文字"
|
24
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
ko:
|
2
|
+
active_admin:
|
3
|
+
import: "가져오기"
|
4
|
+
active_admin_import:
|
5
|
+
file: '파일'
|
6
|
+
file_error: "오류: %{message}"
|
7
|
+
file_format_error: "CSV 파일만 가져오기 할 수 있습니다"
|
8
|
+
file_empty_error: "파일을 추가해주세요"
|
9
|
+
no_file_error: "파일을 추가해주세요"
|
10
|
+
details: "가져오기할 파일을 추가해주세요"
|
11
|
+
imported:
|
12
|
+
one: "1개의 %{model}을 가져왔습니다"
|
13
|
+
other: "%{count}개의 %{plural_model}을 가져왔습니다"
|
14
|
+
failed:
|
15
|
+
one: "1개의 %{model} 가져오기 실패 : %{message}"
|
16
|
+
other: "%{count}개의 %{plural_model} 가져오기 실패: %{message}"
|
17
|
+
import_model: "%{plural_model} 가져오기"
|
18
|
+
import_btn: "가져오기"
|
19
|
+
import_btn_disabled: "가져오는 중입니다..."
|
20
|
+
csv_options: "CSV 옵션"
|
21
|
+
col_sep: '열 sep'
|
22
|
+
row_sep: '행 sep'
|
23
|
+
quote_char: 'Quote char'
|
@@ -0,0 +1,23 @@
|
|
1
|
+
pt-BR:
|
2
|
+
active_admin:
|
3
|
+
import: "Importar"
|
4
|
+
active_admin_import:
|
5
|
+
file: 'Arquivo'
|
6
|
+
file_error: "Error: %{message}"
|
7
|
+
file_format_error: "Você só pode importar arquivos csv válidos"
|
8
|
+
file_empty_error: "Você não pode importar arquivos vázios"
|
9
|
+
no_file_error: "Por favor, selecione o arquivo para importar"
|
10
|
+
details: "Por favor, selecione o arquivo para importar"
|
11
|
+
imported:
|
12
|
+
one: "Importado com sucesso 1 %{model}"
|
13
|
+
other: "Importado com sucesso %{count} %{plural_model}"
|
14
|
+
failed:
|
15
|
+
one: "Não foi possível importar 1 %{model}: %{message}"
|
16
|
+
other: "Não foi possível importar %{count} %{plural_model}: %{message}"
|
17
|
+
import_model: "Importar %{plural_model}"
|
18
|
+
import_btn: "Importar"
|
19
|
+
import_btn_disabled: "Aguarde..."
|
20
|
+
csv_options: "opções CSV"
|
21
|
+
col_sep: 'Col sep'
|
22
|
+
row_sep: 'Row sep'
|
23
|
+
quote_char: 'Quote char'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
ru:
|
2
|
+
active_admin:
|
3
|
+
import: "Импорт"
|
4
|
+
active_admin_import:
|
5
|
+
file: 'Файл'
|
6
|
+
file_error: "Ошибка: %{message}"
|
7
|
+
file_format_error: "Файл должен быть корректно сформатированным CSV-файлом"
|
8
|
+
file_empty_error: "Файл пуст"
|
9
|
+
no_file_error: "Выберите файл для импорта"
|
10
|
+
details: "Выберите файл для импорта"
|
11
|
+
imported:
|
12
|
+
one: "Успешно импортировано: 1 %{model}"
|
13
|
+
few: "Успешно импортировано: %{count} %{plural_model}"
|
14
|
+
many: "Успешно импортировано: %{count} %{plural_model}"
|
15
|
+
other: "Успешно импортировано: %{count} %{plural_model}"
|
16
|
+
failed:
|
17
|
+
one: "Не удалось импортировать: 1 %{model}: %{message}"
|
18
|
+
few: "Не удалось импортировать: %{count} %{plural_model}: %{message}"
|
19
|
+
many: "Не удалось импортировать: %{count} %{plural_model}: %{message}"
|
20
|
+
other: "Не удалось импортировать: %{count} %{plural_model}: %{message}"
|
21
|
+
import_model: "Импорт %{plural_model}"
|
22
|
+
import_btn: "Импортировать"
|
23
|
+
import_btn_disabled: "Подождите..."
|
24
|
+
csv_options: "Параметры CSV"
|
25
|
+
col_sep: 'Разделитель колонок'
|
26
|
+
row_sep: 'Разделитель строк'
|
27
|
+
quote_char: 'Обособляющий символ'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
uk:
|
2
|
+
active_admin:
|
3
|
+
import: "Імпорт"
|
4
|
+
active_admin_import:
|
5
|
+
file: "Файл"
|
6
|
+
file_error: "Помилка: %{message}"
|
7
|
+
file_format_error: "Файл повинен бути коректно форматованим CSV-файлом"
|
8
|
+
file_empty_error: "Файл порожній"
|
9
|
+
no_file_error: "Виберіть файл для імпорту"
|
10
|
+
details: "Виберіть файл для імпорту"
|
11
|
+
imported:
|
12
|
+
one: "Успішно імпортовано: 1 %{model}"
|
13
|
+
few: "Успішно імпортовано: %{count} %{plural_model}"
|
14
|
+
many: "Успішно імпортовано: %{count} %{plural_model}"
|
15
|
+
other: "Успішно імпортовано: %{count} %{plural_model}"
|
16
|
+
failed:
|
17
|
+
one: "Не вдалося імпортувати: 1 %{model}: %{message}"
|
18
|
+
few: "Не вдалося імпортувати: %{count} %{plural_model}: %{message}"
|
19
|
+
many: "Не вдалося імпортувати: %{count} %{plural_model}: %{message}"
|
20
|
+
other: "Не вдалося імпортувати: %{count} %{plural_model}: %{message}"
|
21
|
+
import_model: "Імпорт %{plural_model}"
|
22
|
+
import_btn: "Імпортувати"
|
23
|
+
import_btn_disabled: "Зачекайте..."
|
24
|
+
csv_options: "Параметри CSV"
|
25
|
+
col_sep: "Роздільник колонок"
|
26
|
+
row_sep: "Роздільник рядків"
|
27
|
+
quote_char: "Відокремлювальний символ"
|
data/lib/active_admin_import.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'activerecord-import'
|
2
3
|
require 'active_admin'
|
3
4
|
require 'active_admin_import/version'
|
@@ -9,5 +10,3 @@ require 'active_admin_import/importer'
|
|
9
10
|
require 'active_admin_import/model'
|
10
11
|
require 'active_admin_import/authorization'
|
11
12
|
::ActiveAdmin::DSL.send(:include, ActiveAdminImport::DSL)
|
12
|
-
|
13
|
-
|
@@ -1,48 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module ActiveAdminImport
|
3
|
+
# Declares import functionality
|
4
|
+
#
|
5
|
+
# Options
|
6
|
+
# +back+:: resource action to redirect after processing
|
7
|
+
# +csv_options+:: hash to override default CSV options
|
8
|
+
# +batch_size+:: integer value of max record count inserted by 1 query/transaction
|
9
|
+
# +batch_transaction+:: bool (false by default), if transaction is used when batch importing
|
10
|
+
# and works when :validate is set to true
|
11
|
+
# +before_import+:: proc for before import action, hook called with importer object
|
12
|
+
# +after_import+:: proc for after import action, hook called with importer object
|
13
|
+
# +before_batch_import+:: proc for before each batch action, called with importer object
|
14
|
+
# +after_batch_import+:: proc for after each batch action, called with importer object
|
15
|
+
# +validate+:: true|false, means perform validations or not
|
16
|
+
# +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import
|
17
|
+
# to use MySQL's ON DUPLICATE KEY UPDATE ability.
|
18
|
+
# +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false)
|
19
|
+
# even if record timestamps is disabled in ActiveRecord::Base
|
20
|
+
# +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
|
21
|
+
# +template+:: custom template rendering
|
22
|
+
# +template_object+:: object passing to view
|
23
|
+
# +resource_class+:: resource class name, override to import to another table (default config.resource_class)
|
24
|
+
# +resource_label+:: resource label value (default config.resource_label)
|
25
|
+
# +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
|
26
|
+
#
|
2
27
|
module DSL
|
3
|
-
|
4
|
-
|
5
|
-
# Declares import functionality
|
6
|
-
#
|
7
|
-
# Options
|
8
|
-
# +back+:: resource action to redirect after processing
|
9
|
-
# +csv_options+:: hash to override default CSV options
|
10
|
-
# +batch_size+:: integer value of max record count inserted by 1 query/transaction
|
11
|
-
# +batch_transaction+:: bool (false by default), if transaction is used when batch importing and works when :validate is set to true
|
12
|
-
# +before_import+:: proc for before import action, hook called with importer object
|
13
|
-
# +after_import+:: proc for after import action, hook called with importer object
|
14
|
-
# +before_batch_import+:: proc for before each batch action, called with importer object
|
15
|
-
# +after_batch_import+:: proc for after each batch action, called with importer object
|
16
|
-
# +validate+:: true|false, means perform validations or not
|
17
|
-
# +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
|
18
|
-
# +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
|
19
|
-
# +ignore+:: true|false, tells activerecord-import to use MySQL's INSERT IGNORE ability
|
20
|
-
# +template+:: custom template rendering
|
21
|
-
# +template_object+:: object passing to view
|
22
|
-
# +resource_class+:: resource class name, override to import to another table (default config.resource_class)
|
23
|
-
# +resource_label+:: resource label value (default config.resource_label)
|
24
|
-
# +plural_resource_label+:: pluralized resource label value (default config.plural_resource_label)
|
25
|
-
#
|
26
|
-
|
27
|
-
|
28
|
-
DEFAULT_RESULT_PROC = ->(result, options) do
|
29
|
-
|
28
|
+
DEFAULT_RESULT_PROC = lambda do |result, options|
|
30
29
|
model_name = options[:resource_label].downcase
|
31
30
|
plural_model_name = options[:plural_resource_label].downcase
|
32
31
|
if result.empty?
|
33
32
|
flash[:warning] = I18n.t('active_admin_import.file_empty_error')
|
34
33
|
else
|
35
|
-
if result.
|
36
|
-
flash[:error] = I18n.t(
|
34
|
+
if result.failed?
|
35
|
+
flash[:error] = I18n.t(
|
36
|
+
'active_admin_import.failed',
|
37
|
+
count: result.failed.count,
|
38
|
+
model: model_name,
|
39
|
+
plural_model: plural_model_name,
|
40
|
+
message: result.failed_message(limit: options[:error_limit]))
|
37
41
|
return if options[:batch_transaction]
|
38
42
|
end
|
39
|
-
if result.
|
40
|
-
flash[:notice] = I18n.t(
|
43
|
+
if result.imported?
|
44
|
+
flash[:notice] = I18n.t(
|
45
|
+
'active_admin_import.imported',
|
46
|
+
count: result.imported_qty,
|
47
|
+
model: model_name,
|
48
|
+
plural_model: plural_model_name)
|
41
49
|
end
|
42
50
|
end
|
43
51
|
end
|
44
|
-
|
45
|
-
|
52
|
+
# rubocop:disable Metrics/AbcSize
|
46
53
|
def active_admin_import(options = {}, &block)
|
47
54
|
options.assert_valid_keys(*Options::VALID_OPTIONS)
|
48
55
|
|
@@ -55,35 +62,44 @@ module ActiveAdminImport
|
|
55
62
|
render template: options[:template]
|
56
63
|
end
|
57
64
|
|
58
|
-
action_item :import, only: :index do
|
65
|
+
action_item :import, only: :index, if: options[:if] do
|
59
66
|
if authorized?(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
|
60
|
-
link_to(
|
67
|
+
link_to(
|
68
|
+
I18n.t('active_admin_import.import_model', plural_model: options[:plural_resource_label]),
|
69
|
+
action: :import
|
70
|
+
)
|
61
71
|
end
|
62
72
|
end
|
63
73
|
|
64
74
|
collection_action :do_import, method: :post do
|
65
75
|
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
|
66
|
-
|
76
|
+
_params = params.respond_to?(:to_unsafe_h) ? params.to_unsafe_h : params
|
77
|
+
params = ActiveSupport::HashWithIndifferentAccess.new _params
|
67
78
|
@active_admin_import_model = options[:template_object]
|
68
79
|
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
|
69
|
-
#go back to form
|
80
|
+
# go back to form
|
70
81
|
return render template: options[:template] unless @active_admin_import_model.valid?
|
71
|
-
@importer = Importer.new(
|
82
|
+
@importer = Importer.new(
|
83
|
+
options[:resource_class],
|
84
|
+
@active_admin_import_model,
|
85
|
+
options
|
86
|
+
)
|
72
87
|
begin
|
73
88
|
result = @importer.import
|
74
89
|
|
75
90
|
if block_given?
|
76
|
-
instance_eval
|
91
|
+
instance_eval(&block)
|
77
92
|
else
|
78
93
|
instance_exec result, options, &DEFAULT_RESULT_PROC
|
79
94
|
end
|
80
|
-
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid => e
|
95
|
+
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid, CSV::MalformedCSVError => e
|
81
96
|
Rails.logger.error(I18n.t('active_admin_import.file_error', message: e.message))
|
97
|
+
Rails.logger.error(e.backtrace.join("\n"))
|
82
98
|
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message[0..200])
|
83
99
|
end
|
84
100
|
redirect_to options[:back]
|
85
101
|
end
|
86
|
-
|
102
|
+
# rubocop:enable Metrics/AbcSize
|
87
103
|
end
|
88
104
|
end
|
89
105
|
end
|