Skip to content

Commit f6ac13a

Browse files
committed
added one more spec, example, new proc syntax
1 parent 0e2e8aa commit f6ac13a

File tree

5 files changed

+95
-40
lines changed

5 files changed

+95
-40
lines changed

README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ And then execute:
9494
ActiveAdmin.register Post do
9595
active_admin_import validate: false,
9696
csv_options: {col_sep: ";" },
97-
before_import: proc{ Post.delete_all},
97+
before_import: ->(importer){ Post.delete_all },
9898
batch_size: 1000
9999

100100

@@ -111,14 +111,14 @@ And then execute:
111111
active_admin_import validate: false,
112112
csv_options: {col_sep: ";" },
113113
resource_class: ImportedPost , # we import data into another resource
114-
before_import: proc{ ImportedPost.delete_all },
115-
after_import: proc{
114+
before_import: ->(importer){ ImportedPost.delete_all },
115+
after_import: ->(importer){
116116
Post.transaction do
117117
Post.delete_all
118118
Post.connection.execute("INSERT INTO posts (SELECT * FROM imported_posts)")
119119
end
120120
},
121-
back: proc { config.namespace.resource_for(Post).route_collection_path } # redirect to post index
121+
back: -> { config.namespace.resource_for(Post).route_collection_path } # redirect to post index
122122
end
123123
```
124124

@@ -167,7 +167,7 @@ And then execute:
167167
```ruby
168168
ActiveAdmin.register Post do
169169
active_admin_import validate: true,
170-
before_batch_import: proc { |import|
170+
before_batch_import: ->(importer) {
171171
import.file #current file used
172172
import.resource #ActiveRecord class to import to
173173
import.options # options
@@ -176,13 +176,28 @@ And then execute:
176176
import.csv_lines #lines to import
177177
import.model #template_object instance
178178
},
179-
after_batch_import: proc{ |import|
179+
after_batch_import: ->(importer) {
180180
#the same
181181
}
182182
end
183183
```
184184

185-
#### Example7 change csv values before import (find each 'Author name' column and replace it with authors_id before insert )
185+
#### Example7 update by id emulation
186+
187+
```ruby
188+
ActiveAdmin.register Post do
189+
active_admin_import({
190+
before_batch_import: ->(importer) {
191+
Post.where(id: importer.values_at('id')).delete_all
192+
}
193+
})
194+
end
195+
196+
```
197+
198+
199+
200+
#### Example8 change csv values before import (find each 'Author name' column and replace it with authors_id before insert )
186201

187202
```ruby
188203
ActiveAdmin.register Post do
@@ -199,9 +214,9 @@ And then execute:
199214
```
200215

201216

202-
#### Example8 dynamic CSV options, template overriding
217+
#### Example9 dynamic CSV options, template overriding
203218

204-
- put overrided template to ```app/views/import.html.erb```
219+
- put overridden template to ```app/views/import.html.erb```
205220

206221
```erb
207222

lib/active_admin_import/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module DSL
2424
#
2525

2626

27-
DEFAULT_RESULT_PROC = proc do |result, options|
27+
DEFAULT_RESULT_PROC = ->(result, options) do
2828

2929
model_name = options[:resource_label].downcase
3030
plural_model_name = options[:plural_resource_label].downcase

lib/active_admin_import/model.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class Model
1010
include ActiveModel::Validations::Callbacks
1111

1212
validates :file, presence: {
13-
message: proc { I18n.t("active_admin_import.no_file_error") }
14-
}, unless: proc { |me| me.new_record? }
13+
message: ->(*_){ I18n.t("active_admin_import.no_file_error") }
14+
}, unless: ->(me){ me.new_record? }
1515

16-
validate :correct_content_type, if: proc { |me| me.file.present? }
17-
validate :file_contents_present, if: proc { |me| me.file.present? }
16+
validate :correct_content_type, if: ->(me) { me.file.present? }
17+
validate :file_contents_present, if: ->(me) { me.file.present? }
1818

19-
before_validation :uncompress_file, if: proc { |me| me.archive? && me.allow_archive? }
20-
before_validation :encode_file, if: proc { |me| me.force_encoding? && me.file.present? }
19+
before_validation :unzip_file, if: ->(me) { me.archive? && me.allow_archive? }
20+
before_validation :encode_file, if: ->(me) { me.force_encoding? && me.file.present? }
2121

2222
attr_reader :attributes
2323

@@ -40,7 +40,13 @@ def read_attribute_for_validation(key)
4040
end
4141

4242
def default_attributes
43-
{hint: '', file: nil, csv_headers: [], allow_archive: true, force_encoding: 'UTF-8'}
43+
{
44+
allow_archive: true,
45+
csv_headers: [],
46+
file: nil,
47+
force_encoding: "UTF-8",
48+
hint: ""
49+
}
4450
end
4551

4652
def allow_archive?
@@ -82,7 +88,7 @@ def encode_file
8288
end
8389
end
8490

85-
def uncompress_file
91+
def unzip_file
8692
Zip::File.open(file_path) do |zip_file|
8793
self.file = Tempfile.new('active-admin-import-unzipped')
8894
data = zip_file.entries.select { |f| f.file? }.first.get_input_stream.read
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Id,Name,Last name,Birthday
2+
1,John,Doe,1986-05-01
3+
2,Jane,Roe,1988-11-16

spec/import_spec.rb

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,24 @@ def upload_file!(name, ext='csv')
4646

4747
context "posts index" do
4848
before do
49-
Author.create!(name: 'John', last_name: 'Doe')
50-
Author.create!(name: 'Jane', last_name: 'Roe')
51-
add_post_resource({
52-
validate: true,
53-
headers_rewrites: {
54-
:'Author Name' => :author_id
55-
},
56-
before_batch_import: ->(importer) do
57-
authors_names = importer.values_at(:author_id)
58-
# replacing author name with author id
59-
authors = Author.where(name: authors_names).pluck(:name, :id)
60-
#{"Jane" => 2, "John" => 1}
61-
options = Hash[*authors.flatten]
62-
importer.batch_replace(:author_id, options)
63-
end
64-
})
65-
visit '/admin/posts/import'
66-
upload_file!(:posts)
49+
Author.create!(name: "John", last_name: "Doe")
50+
Author.create!(name: "Jane", last_name: "Roe")
51+
add_post_resource({
52+
validate: true,
53+
headers_rewrites: {
54+
:'Author Name' => :author_id
55+
},
56+
before_batch_import: ->(importer) do
57+
authors_names = importer.values_at(:author_id)
58+
# replacing author name with author id
59+
authors = Author.where(name: authors_names).pluck(:name, :id)
60+
#{"Jane" => 2, "John" => 1}
61+
options = Hash[*authors.flatten]
62+
importer.batch_replace(:author_id, options)
63+
end
64+
})
65+
visit "/admin/posts/import"
66+
upload_file!(:posts)
6767
end
6868

6969
it "should resolve author_id by author name" do
@@ -114,6 +114,37 @@ def upload_file!(name, ext='csv')
114114

115115
end
116116

117+
context "authors already exist" do
118+
before do
119+
Author.create!(id: 1, name: "Jane", last_name: "Roe")
120+
Author.create!(id: 2, name: "John", last_name: "Doe")
121+
end
122+
123+
context "having authors with the same Id" do
124+
before do
125+
add_author_resource({
126+
before_batch_import: ->(importer) do
127+
Author.where(id: importer.values_at("id")).delete_all
128+
end
129+
})
130+
visit "/admin/authors/import"
131+
upload_file!(:authors_with_ids)
132+
end
133+
134+
it "should replace authors" do
135+
expect(page).to have_content "Successfully imported 2 authors"
136+
expect(Author.count).to eq(2)
137+
end
138+
139+
it "should replace authors by id" do
140+
expect(Author.find(1).name).to eq("John")
141+
expect(Author.find(2).name).to eq("Jane")
142+
end
143+
144+
end
145+
146+
end
147+
117148
context "with valid options" do
118149

119150
let(:options) { {} }
@@ -322,10 +353,10 @@ def upload_file!(name, ext='csv')
322353
context "with callback procs options" do
323354
let(:options) do
324355
{
325-
before_import: proc { |_| },
326-
after_import: proc { |_| },
327-
before_batch_import: proc { |_| },
328-
after_batch_import: proc { |_| }
356+
before_import: ->(_) { true },
357+
after_import: ->(_) { true },
358+
before_batch_import: ->(_) { true },
359+
after_batch_import: ->(_) { true }
329360
}
330361
end
331362

0 commit comments

Comments
 (0)