Skip to content

Commit 5a71fbb

Browse files
committed
more specs, improved error hadling
1 parent 50ac9c9 commit 5a71fbb

File tree

10 files changed

+98
-25
lines changed

10 files changed

+98
-25
lines changed

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ en:
22
active_admin:
33
import: "Import"
44
active_admin_import:
5+
file_error: "Error: %{message}"
56
file_format_error: "You can import only valid csv file"
67
file_empty_error: "You can't import empty file"
78
no_file_error: "Please, select file to import"

config/locales/it.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ it:
22
active_admin:
33
import: "Importa"
44
active_admin_import:
5+
file_error: "Errore: %{message}"
56
file_format_error: "Puoi importare solo CSV conformi"
67
no_file_error: "Per favore, seleziona un file da importare"
78
file_empty_error: "Non è possibile importare file vuoto"

lib/active_admin_import/dsl.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ActiveAdminImport
22
module DSL
3-
4-
3+
4+
55
# Declares import functionality
66
#
77
# Options
@@ -24,8 +24,8 @@ module DSL
2424
#
2525

2626
def active_admin_import(options = {}, &block)
27-
options.assert_valid_keys(*VALID_OPTIONS)
28-
27+
options.assert_valid_keys(*VALID_OPTIONS)
28+
2929
default_options = {
3030
back: {action: :import},
3131
csv_options: {},
@@ -52,7 +52,6 @@ def active_admin_import(options = {}, &block)
5252
link_to(I18n.t('active_admin_import.import_model', model: options[:resource_label]), action: :import)
5353
end
5454

55-
5655
@active_admin_import_model = options[:template_object]
5756
render template: options[:template]
5857
end
@@ -61,8 +60,6 @@ def active_admin_import(options = {}, &block)
6160
link_to(I18n.t('active_admin_import.import_model', model: options[:resource_label]), action: :import)
6261
end
6362

64-
65-
6663
collection_action :do_import, method: :post do
6764
authorize!(ActiveAdminImport::Auth::IMPORT, active_admin_config.resource_class)
6865

@@ -76,21 +73,24 @@ def active_admin_import(options = {}, &block)
7673
if block_given?
7774
instance_eval &block
7875
else
79-
76+
8077
model_name = options[:resource_label].downcase
8178
plural_model_name = options[:plural_resource_label].downcase
82-
83-
84-
85-
if result.has_imported?
86-
flash[:notice] = I18n.t('active_admin_import.imported', count: result.imported_qty, model: model_name, plural_model: plural_model_name)
87-
end
88-
if result.has_failed?
89-
flash[:error] = I18n.t('active_admin_import.failed', count: result.failed.count, model: model_name, plural_model: plural_model_name)
79+
80+
81+
if result.empty?
82+
flash[:warning] = I18n.t('active_admin_import.file_empty_error')
83+
else
84+
if result.has_imported?
85+
flash[:notice] = I18n.t('active_admin_import.imported', count: result.imported_qty, model: model_name, plural_model: plural_model_name)
86+
end
87+
if result.has_failed?
88+
flash[:error] = I18n.t('active_admin_import.failed', count: result.failed.count, model: model_name, plural_model: plural_model_name)
89+
end
9090
end
9191
end
9292
rescue ActiveRecord::Import::MissingColumnError, NoMethodError => e
93-
flash[:error] = e.message
93+
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message)
9494
end
9595
redirect_to options[:back]
9696
end

lib/active_admin_import/import_result.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ def has_failed?
2424
@failed.any?
2525
end
2626

27+
def empty?
28+
total == 0
29+
end
30+
2731
end
2832
end

spec/fixtures/files/author.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Name,Last name,Birthday
2+
John,Doe,1986-05-01
File renamed without changes.

spec/fixtures/files/empty.csv

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Name,Last name,Birthday

spec/import_spec.rb

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@
88
visit '/admin/authors/import'
99
end
1010

11-
def upload_file!(name)
12-
attach_file('active_admin_import_model_file', File.expand_path("./spec/fixtures/csv/#{name}.csv"))
11+
def with_zipped_csv(name, &block)
12+
13+
zip_file = File.expand_path("./spec/fixtures/files/#{name}.zip")
14+
15+
begin
16+
Zip::File.open(zip_file, Zip::File::CREATE) do |z|
17+
z.add "#{name}.csv", File.expand_path("./spec/fixtures/files/#{name}.csv")
18+
end
19+
instance_eval &block
20+
ensure
21+
File.delete zip_file rescue nil
22+
end
23+
24+
end
25+
26+
def upload_file!(name, ext='csv')
27+
attach_file('active_admin_import_model_file', File.expand_path("./spec/fixtures/files/#{name}.#{ext}"))
1328
find_button('Import').click
1429
end
1530

@@ -29,23 +44,72 @@ def upload_file!(name)
2944

3045
context "import file" do
3146

47+
[:empty, :only_headers].each do |file|
48+
context "when #{file} file" do
49+
it "should render warning" do
50+
51+
upload_file!(file)
52+
expect(page).to have_content I18n.t('active_admin_import.file_empty_error')
53+
expect(Author.count).to eq(0)
54+
end
55+
end
56+
end
57+
58+
context "when no file" do
59+
it "should render error" do
60+
find_button('Import').click
61+
expect(Author.count).to eq(0)
62+
expect(page).to have_content I18n.t('active_admin_import.no_file_error')
63+
end
64+
end
65+
3266
context "with headers" do
3367

34-
it "imports file" do
68+
it "should import file with many records" do
3569
upload_file!(:authors)
3670
expect(page).to have_content "Successfully imported 2 authors"
3771
expect(Author.count).to eq(2)
3872
end
73+
74+
it "should import file with 1 record" do
75+
upload_file!(:author)
76+
expect(page).to have_content "Successfully imported 1 author"
77+
expect(Author.count).to eq(1)
78+
end
3979
end
4080

4181

4282
context "without headers" do
83+
context "with known csv headers" do
84+
before do
85+
allow_any_instance_of(ActiveAdminImport::Model).to receive(:csv_headers).and_return(['Name', 'Last name', 'Birthday'])
86+
end
87+
88+
it "should import file" do
89+
upload_file!(:authors_no_headers)
90+
expect(page).to have_content "Successfully imported 2 authors"
91+
expect(Author.count).to eq(2)
92+
end
93+
end
4394

44-
it "imports file" do
45-
allow_any_instance_of(ActiveAdminImport::Model).to receive(:csv_headers).and_return(['Name','Last name','Birthday'])
46-
upload_file!(:authors_no_headers)
47-
expect(page).to have_content "Successfully imported 2 authors"
48-
expect(Author.count).to eq(2)
95+
context "with unknown csv headers" do
96+
it "should render error" do
97+
upload_file!(:authors_no_headers)
98+
expect(page).to have_content "Error:"
99+
expect(Author.count).to eq(0)
100+
end
101+
end
102+
103+
end
104+
105+
106+
context "when zipped" do
107+
it "should import file" do
108+
with_zipped_csv(:authors) do
109+
upload_file!(:authors, :zip)
110+
expect(page).to have_content "Successfully imported 2 authors"
111+
expect(Author.count).to eq(2)
112+
end
49113
end
50114
end
51115

0 commit comments

Comments
 (0)