11# ActiveAdminImport
2- The most fastest and efficient CSV import for Active Admin
3- with support of validations, bulk inserts and encodings handling
4-
2+ [ The most fastest and efficient CSV import for Active Admin
3+ with support of validations, bulk inserts and encodings handling] ( http://fivell.github.io/active_admin_import )
4+
55
66
77[ ![ Build Status] ( http://img.shields.io/travis/Fivell/active_admin_import.svg )] ( https://travis-ci.org/Fivell/active_admin_import )
@@ -37,18 +37,26 @@ And then execute:
3737
3838# active_admin_import features
3939<ol >
40- <li > Replacements (Ex 2) </li >
41- <li > Encoding handling (Ex 4, 5) </li >
40+ <li > Replacements/Updates support </li >
41+ <li > Encoding handling</li >
4242 <li > CSV options</li >
43- <li > Ability to prepend CSV headers automatically </li >
43+ <li > Ability to descibe/change CSV headers</li >
4444 <li > Bulk import (activerecord-import)</li >
4545 <li > Callbacks</li >
4646 <li > Zip files</li >
47- <li > more...</li >
47+ <li > and more...</li >
4848</ol >
4949
5050
5151
52+ #### Basic usage
53+
54+ ``` ruby
55+ ActiveAdmin .register Post
56+ active_admin_import options
57+ end
58+ ```
59+
5260
5361#### Options
5462Tool | Description
@@ -73,185 +81,9 @@ Tool | Description
7381
7482
7583
76- #### Default options values
77-
78- ``` ruby
79- back: {action: :import },
80- csv_options: {},
81- template: " admin/import" ,
82- fetch_extra_options_from_params: [],
83- resource_class: config.resource_class,
84- resource_label: config.resource_label,
85- plural_resource_label: config.plural_resource_label,
86- ```
87-
88- #### Example1
89-
90- ``` ruby
91- ActiveAdmin .register Post do
92- active_admin_import validate: false ,
93- csv_options: {col_sep: " ;" },
94- before_import: -> (importer){ Post .delete_all },
95- batch_size: 1000
96-
97-
98- end
99- ```
100-
101-
102- #### Example2 Importing to mediate table with insert select operation after import completion
103-
104- <p > This config allows to replace data in 1 sql query with callback </p >
84+ #### Wiki
10585
106- ``` ruby
107- ActiveAdmin .register Post do
108- active_admin_import validate: false ,
109- csv_options: {col_sep: " ;" },
110- resource_class: ImportedPost , # we import data into another resource
111- before_import: -> (importer){ ImportedPost .delete_all },
112- after_import: -> (importer){
113- Post .transaction do
114- Post .delete_all
115- Post .connection.execute(" INSERT INTO posts (SELECT * FROM imported_posts)" )
116- end
117- },
118- back: -> { config.namespace.resource_for(Post ).route_collection_path } # redirect to post index
119- end
120- ```
121-
122-
123- #### Example3 Importing file without headers, but we always know file format, so we can predefine it
124-
125- ``` ruby
126- ActiveAdmin .register Post do
127- active_admin_import validate: true ,
128- template_object: ActiveAdminImport ::Model .new (
129- hint: " file will be imported with such header format: 'body','title','author'" ,
130- csv_headers: [" body" ," title" ," author" ]
131- )
132- end
133- ```
134-
135- #### Example4 Importing ISO-8859-1 encoded file and disallow archives
136-
137-
138- ``` ruby
139- ActiveAdmin .register Post do
140- active_admin_import validate: true ,
141- template_object: ActiveAdminImport ::Model .new (
142- hint: " file encoded in ISO-8859-1" ,
143- force_encoding: " ISO-8859-1" ,
144- allow_archive: false
145- )
146- end
147- ```
148-
149- #### Example5 Importing file with unknown encoding and autodetect it
150-
151-
152- ``` ruby
153- ActiveAdmin .register Post do
154- active_admin_import validate: true ,
155- template_object: ActiveAdminImport ::Model .new (
156- force_encoding: :auto
157- )
158- end
159- ```
160-
161- #### Example6 Callbacks for each bulk insert iteration
162-
163-
164- ``` ruby
165- ActiveAdmin .register Post do
166- active_admin_import validate: true ,
167- before_batch_import: -> (importer) {
168- import.file # current file used
169- import.resource # ActiveRecord class to import to
170- import.options # options
171- import.result # result before bulk iteration
172- import.headers # CSV headers
173- import.csv_lines # lines to import
174- import.model # template_object instance
175- },
176- after_batch_import: -> (importer) {
177- # the same
178- }
179- end
180- ```
181-
182- #### Example7 update by id emulation
183-
184- ``` ruby
185-
186- ActiveAdmin .register Post do
187- active_admin_import({
188- before_batch_import: -> (importer) {
189- Post .where(id: importer.values_at(' id' )).delete_all
190- }
191- })
192- end
193-
194- ```
195-
196-
197-
198- #### Example8 change csv values before import (find each 'Author name' column and replace it with authors_id before insert )
199-
200- ``` ruby
201- ActiveAdmin .register Post do
202- active_admin_import validate: true ,
203- headers_rewrites: { :'Author name' => :author_id },
204- before_batch_import: -> (importer) {
205- authors_names = importer.values_at(:author_id )
206- # replacing author name with author id
207- authors = Author .where(name: authors_names).pluck(:name , :id )
208- options = Hash [* authors.flatten] # #{"Jane" => 2, "John" => 1}
209- importer.batch_replace(:author_id , options)
210- }
211- end
212- ```
213-
214-
215- #### Example9 dynamic CSV options, template overriding
216-
217- - put overridden template to ``` app/views/import.html.erb ```
218-
219- ``` erb
220-
221- <p>
222- <%= raw(@active_admin_import_model.hint) %>
223- </p>
224- <%= semantic_form_for @active_admin_import_model, url: {action: :do_import}, html: {multipart: true} do |f| %>
225- <%= f.inputs do %>
226- <%= f.input :file, as: :file %>
227- <% end %>
228- <%= f.inputs "CSV options", for: [:csv_options, OpenStruct.new(@active_admin_import_model.csv_options)] do |csv| %>
229- <% csv.with_options input_html: {style: 'width:40px;'} do |opts| %>
230- <%= opts.input :col_sep %>
231- <%= opts.input :row_sep %>
232- <%= opts.input :quote_char %>
233- <% end %>
234- <% end %>
235-
236- <%= f.actions do %>
237- <%= f.action :submit, label: t("active_admin_import.import_btn"), button_html: {disable_with: t("active_admin_import.import_btn_disabled")} %>
238- <% end %>
239- <% end %>
240-
241- ```
242-
243- - call method with following parameters
244-
245- ``` ruby
246- ActiveAdmin .register Post do
247- active_admin_import validate: false ,
248- template: ' import' ,
249- template_object: ActiveAdminImport ::Model .new (
250- hint: " specify CSV options"
251- csv_options: {col_sep: " ;" , row_sep: nil , quote_char: nil }
252- )
253- end
254- ```
86+ [ Check various examples] ( https://github.com/Fivell/active_admin_import/wiki )
25587
25688## Dependencies
25789
@@ -264,6 +96,14 @@ Tool | Description
26496[ activerecord-import ] : https://github.com/jmhodges/rchardet
26597
26698
99+ ## Contributing
100+
101+ 1 . Fork it
102+ 2 . Create your feature branch (` git checkout -b my-new-feature ` )
103+ 3 . Commit your changes (` git commit -am 'Add some feature' ` )
104+ 4 . Push to the branch (` git push origin my-new-feature ` )
105+ 5 . Create new Pull Request
106+
267107
268108
269109
0 commit comments