active_admin_import 2.1.0.rc3 → 2.1.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +37 -24
- data/active_admin_import.gemspec +1 -1
- data/lib/active_admin_import.rb +1 -0
- data/lib/active_admin_import/dsl.rb +22 -18
- data/lib/active_admin_import/locales/en.yml +1 -0
- data/lib/active_admin_import/locales/it.yml +1 -0
- data/lib/active_admin_import/model.rb +11 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YWMyOTEwMzdiMDIyMjE5ZDMwNmIwZGFjMmVlOGYwYTFiNTllMzgyYw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
M2NkMjcwMjZiYjhhNmIwNzRhMWUyN2QxYjJmNGM3ZDc0MGIxZjAxYg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjZhYTM0NjE2ZmM3ODkwMjU2MzBjNTliZmRiM2IzN2Y3YWNkOTRhZjg4YTJh
|
10
|
+
YTI5NjJkM2NmYzYwMzU1YjYzMzhmNGMxOWQxYWUyZjU3N2E5YTYzZWM2ZGVj
|
11
|
+
MWM3OGZmNTgxODBhODA1MjFmYzEwN2RkZjJmZjlhMjUyNmYwNWQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NjRlNDE4NzhmMGUwNGI5NjNiNzY4M2VmNzUxMTI1YzNmNzMyNGRmNDE4ZTVi
|
14
|
+
NWRkNzEyZGZkMTUwNzMwOGIzYWU5NzNkZGQxNzBiNzRhMWNlYjk2ZGE5OTJm
|
15
|
+
OWUwZmQ4N2NlZTMzNjE4YmIyNDg5YmQ0NTE0M2RmOWUyM2QwYmU=
|
data/README.md
CHANGED
@@ -3,14 +3,16 @@ The most fastest and efficient CSV import for Active Admin (based on activerecor
|
|
3
3
|
with support of validations and bulk inserts
|
4
4
|
|
5
5
|
|
6
|
-
Current master works with AA 0-6-stable branch
|
6
|
+
Important! Current master works and tested with with AA 0-6-stable branch
|
7
7
|
|
8
8
|
|
9
|
-
|
9
|
+
#Installation
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile:
|
12
12
|
|
13
|
-
|
13
|
+
```ruby
|
14
|
+
gem "active_admin_import" , '2.1.0.rc4'
|
15
|
+
```
|
14
16
|
|
15
17
|
And then execute:
|
16
18
|
|
@@ -18,7 +20,7 @@ And then execute:
|
|
18
20
|
|
19
21
|
|
20
22
|
|
21
|
-
|
23
|
+
Why yet another import for ActiveAdmin ? Now with activerecord-import ....
|
22
24
|
|
23
25
|
"Because plain-vanilla, out-of-the-box ActiveRecord doesn’t provide support for inserting large amounts of data efficiently"
|
24
26
|
|
@@ -28,7 +30,7 @@ cool features of activerecord-import
|
|
28
30
|
activerecord-import can perform on duplicate key updates (requires mysql)
|
29
31
|
|
30
32
|
|
31
|
-
|
33
|
+
So active_admin_import features
|
32
34
|
|
33
35
|
Encoding handling
|
34
36
|
Support importing with ZIP file
|
@@ -38,9 +40,10 @@ cool features of activerecord-import
|
|
38
40
|
Bulk import (activerecord-import)
|
39
41
|
Ability to customize template
|
40
42
|
Callbacks support
|
43
|
+
Zip files import
|
41
44
|
and more ....
|
42
45
|
|
43
|
-
|
46
|
+
Options
|
44
47
|
|
45
48
|
# +back+:: resource action to redirect after processing
|
46
49
|
# +csv_options+:: hash with column separator, row separator, etc
|
@@ -63,8 +66,9 @@ cool features of activerecord-import
|
|
63
66
|
|
64
67
|
|
65
68
|
|
66
|
-
|
67
|
-
|
69
|
+
Default options values
|
70
|
+
|
71
|
+
```ruby
|
68
72
|
back: {action: :import},
|
69
73
|
csv_options: {},
|
70
74
|
template: "admin/import",
|
@@ -72,10 +76,11 @@ cool features of activerecord-import
|
|
72
76
|
resource_class: config.resource_class,
|
73
77
|
resource_label: config.resource_label,
|
74
78
|
plural_resource_label: config.plural_resource_label,
|
75
|
-
|
79
|
+
```
|
76
80
|
|
77
|
-
|
78
|
-
|
81
|
+
Example1
|
82
|
+
|
83
|
+
```ruby
|
79
84
|
ActiveAdmin.register Post do
|
80
85
|
active_admin_import :validate => false,
|
81
86
|
:csv_options => {:col_sep => ";" },
|
@@ -84,13 +89,14 @@ cool features of activerecord-import
|
|
84
89
|
|
85
90
|
|
86
91
|
end
|
92
|
+
```
|
87
93
|
|
88
94
|
|
89
|
-
|
90
|
-
#Example2 Importing to mediate table with insert select operation after import completion
|
95
|
+
Example2 Importing to mediate table with insert select operation after import completion
|
91
96
|
|
92
97
|
This config allows to replace data without downtime
|
93
98
|
|
99
|
+
```ruby
|
94
100
|
ActiveAdmin.register Post do
|
95
101
|
active_admin_import :validate => false,
|
96
102
|
:csv_options => {:col_sep => ";" },
|
@@ -104,11 +110,12 @@ This config allows to replace data without downtime
|
|
104
110
|
},
|
105
111
|
:back => proc { config.namespace.resource_for(Post).route_collection_path } # redirect to post index
|
106
112
|
end
|
113
|
+
```
|
107
114
|
|
108
115
|
|
116
|
+
Example3 Importing file without headers, but we always know file format, so we can predefine it
|
109
117
|
|
110
|
-
|
111
|
-
|
118
|
+
```ruby
|
112
119
|
ActiveAdmin.register Post do
|
113
120
|
active_admin_import :validate => true,
|
114
121
|
:template_object => ActiveAdminImport::Model.new(
|
@@ -116,10 +123,12 @@ This config allows to replace data without downtime
|
|
116
123
|
:csv_headers => ["body","title","author"]
|
117
124
|
)
|
118
125
|
end
|
119
|
-
|
126
|
+
```
|
120
127
|
|
121
|
-
|
128
|
+
Example4 Importing without forcing to UTF-8 and disallow archives
|
129
|
+
|
122
130
|
|
131
|
+
```ruby
|
123
132
|
ActiveAdmin.register Post do
|
124
133
|
active_admin_import :validate => true,
|
125
134
|
:template_object => ActiveAdminImport::Model.new(
|
@@ -128,11 +137,13 @@ This config allows to replace data without downtime
|
|
128
137
|
:allow_archive => false
|
129
138
|
)
|
130
139
|
end
|
140
|
+
```
|
131
141
|
|
132
142
|
|
143
|
+
Example5 Callbacks for each bulk insert iteration
|
133
144
|
|
134
|
-
#Example5 Callbacks for each bulk insert iteration
|
135
145
|
|
146
|
+
```ruby
|
136
147
|
ActiveAdmin.register Post do
|
137
148
|
active_admin_import :validate => true,
|
138
149
|
:before_batch_import => proc { |import|
|
@@ -145,18 +156,19 @@ This config allows to replace data without downtime
|
|
145
156
|
import.model #template_object instance
|
146
157
|
},
|
147
158
|
:before_batch_import => proc{ |import|
|
148
|
-
|
159
|
+
#the same
|
149
160
|
}
|
150
161
|
end
|
162
|
+
```
|
151
163
|
|
152
|
-
|
153
|
-
#Example6 dynamic CSV options, template overriding
|
164
|
+
Example6 dynamic CSV options, template overriding
|
154
165
|
|
155
166
|
1) put overrided template to app/views/import.html.erb
|
156
167
|
|
168
|
+
```erb
|
157
169
|
|
158
170
|
<p>
|
159
|
-
|
171
|
+
<%= raw(@active_admin_import_model.hint) %>
|
160
172
|
</p>
|
161
173
|
<%= semantic_form_for @active_admin_import_model, url: {action: :do_import}, html: {multipart: true} do |f| %>
|
162
174
|
<%= f.inputs do %>
|
@@ -175,10 +187,11 @@ This config allows to replace data without downtime
|
|
175
187
|
<% end %>
|
176
188
|
<% end %>
|
177
189
|
|
178
|
-
|
190
|
+
```
|
179
191
|
|
180
192
|
2) call method with following parameters
|
181
193
|
|
194
|
+
```ruby
|
182
195
|
ActiveAdmin.register Post do
|
183
196
|
active_admin_import :validate => false,
|
184
197
|
:template => 'import' ,
|
@@ -187,7 +200,7 @@ This config allows to replace data without downtime
|
|
187
200
|
:csv_options => {:col_sep => ";", :row_sep => nil, :quote_char => nil}
|
188
201
|
)
|
189
202
|
end
|
190
|
-
|
203
|
+
```
|
191
204
|
|
192
205
|
#Links
|
193
206
|
https://github.com/gregbell/active_admin
|
data/active_admin_import.gemspec
CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
14
|
gem.name = "active_admin_import"
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version = ActiveAdminImport::VERSION << '.
|
16
|
+
gem.version = ActiveAdminImport::VERSION << '.rc4'
|
17
17
|
|
18
18
|
gem.add_runtime_dependency 'activerecord-import', '~> 0.4', '>= 0.4.1'
|
19
19
|
gem.add_runtime_dependency 'activeadmin', '~> 0.6', '>= 0.6.0'
|
data/lib/active_admin_import.rb
CHANGED
@@ -27,14 +27,13 @@ module ActiveAdminImport
|
|
27
27
|
back: {action: :import},
|
28
28
|
csv_options: {},
|
29
29
|
template: "admin/import",
|
30
|
-
fetch_extra_options_from_params: [],
|
31
30
|
resource_class: config.resource_class,
|
32
|
-
resource_label:
|
31
|
+
resource_label: config.resource_label,
|
33
32
|
plural_resource_label: config.plural_resource_label,
|
34
33
|
headers_rewrites: {}
|
35
34
|
}
|
36
35
|
options = default_options.deep_merge(options)
|
37
|
-
options[:template_object] =
|
36
|
+
options[:template_object] = ActiveAdminImport::Model.new if options[:template_object].blank?
|
38
37
|
params_key = ActiveModel::Naming.param_key(options[:template_object])
|
39
38
|
|
40
39
|
collection_action :import, method: :get do
|
@@ -48,29 +47,34 @@ module ActiveAdminImport
|
|
48
47
|
|
49
48
|
collection_action :do_import, method: :post do
|
50
49
|
|
51
|
-
@active_admin_import_model =
|
50
|
+
@active_admin_import_model = options[:template_object]
|
52
51
|
@active_admin_import_model.assign_attributes(params[params_key].try(:deep_symbolize_keys) || {})
|
53
52
|
#go back to form
|
54
53
|
return render template: options[:template] unless @active_admin_import_model.valid?
|
55
|
-
|
54
|
+
|
56
55
|
importer = Importer.new(options[:resource_class],
|
57
56
|
@active_admin_import_model,
|
58
57
|
options
|
59
58
|
)
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
59
|
+
begin
|
60
|
+
result = importer.import
|
61
|
+
model_name = options[:resource_label].downcase
|
62
|
+
plural_model_name = options[:resource_label].downcase
|
63
|
+
flash[:notice] = I18n.t('active_admin_import.imported',
|
64
|
+
count: result[:imported].to_i,
|
65
|
+
model: model_name,
|
66
|
+
plural_model: plural_model_name
|
67
|
+
) if result[:imported].to_i > 0
|
68
|
+
|
69
|
+
flash[:error] = I18n.t('active_admin_import.failed',
|
70
|
+
count: result[:failed].count,
|
71
|
+
model: model_name,
|
72
|
+
plural_model: plural_model_name
|
73
|
+
) if result[:failed].count > 0
|
74
|
+
rescue ActiveRecord::Import::MissingColumnError => e
|
75
|
+
flash[:error] = e.message
|
76
|
+
end
|
68
77
|
|
69
|
-
flash[:error] = I18n.t('active_admin_import.failed',
|
70
|
-
count: result[:failed].count,
|
71
|
-
model: model_name,
|
72
|
-
plural_model: plural_model_name
|
73
|
-
) if result[:failed].count > 0
|
74
78
|
|
75
79
|
redirect_to options[:back]
|
76
80
|
end
|
@@ -4,6 +4,7 @@ it:
|
|
4
4
|
active_admin_import:
|
5
5
|
file_format_error: "Puoi importare solo CSV conformi"
|
6
6
|
no_file_error: "Per favore, seleziona un file da importare"
|
7
|
+
file_empty_error: "Non è possibile importare file vuoto"
|
7
8
|
imported:
|
8
9
|
one: "Importato 1 %{model} con successo"
|
9
10
|
other: "Importati %{count} %{plural_model} con successo"
|
@@ -9,6 +9,8 @@ module ActiveAdminImport
|
|
9
9
|
unless: proc { |me| me.new_record? }
|
10
10
|
|
11
11
|
validate :correct_content_type
|
12
|
+
validate :file_contents_present
|
13
|
+
|
12
14
|
|
13
15
|
before_validation :uncompress_file, if: proc { |me| me.archive? && me.allow_archive? }
|
14
16
|
before_validation :encode_file, if: proc { |me| me.force_encoding? && me.file.present? }
|
@@ -112,8 +114,16 @@ module ActiveAdminImport
|
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
117
|
+
def file_contents_present
|
118
|
+
errors.add(:file, I18n.t('active_admin_import.file_empty_error')) if File.zero?(file_path)
|
119
|
+
end
|
120
|
+
|
115
121
|
def file_type
|
116
|
-
file.
|
122
|
+
if file.is_a? ActionDispatch::Http::UploadedFile
|
123
|
+
file.content_type.chomp
|
124
|
+
else
|
125
|
+
''
|
126
|
+
end
|
117
127
|
end
|
118
128
|
end
|
119
129
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_admin_import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.0.
|
4
|
+
version: 2.1.0.rc4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Fedoronchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord-import
|