Skip to content

Commit 01bed24

Browse files
committed
Merge branch 'ifyouseewendy-feature/add_failed_message'
2 parents ef39bcc + 78ccd70 commit 01bed24

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

config/locales/en.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ en:
1212
one: "Successfully imported 1 %{model}"
1313
other: "Successfully imported %{count} %{plural_model}"
1414
failed:
15-
one: "Failed to import 1 %{model}"
16-
other: "Failed to import %{count} %{plural_model}"
15+
one: "Failed to import 1 %{model}: %{message}"
16+
other: "Failed to import %{count} %{plural_model}: %{message}"
1717
import_model: "Import %{plural_model}"
1818
import_btn: "Import"
1919
import_btn_disabled: "Wait..."

lib/active_admin_import/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module DSL
3737
return if options[:batch_transaction]
3838
end
3939
if result.has_imported?
40-
flash[:notice] = I18n.t('active_admin_import.imported', count: result.imported_qty, model: model_name, plural_model: plural_model_name)
40+
flash[:error] = I18n.t('active_admin_import.failed', count: result.failed.count, model: model_name, plural_model: plural_model_name, message: result.failed_message(limit: 5))
4141
end
4242
end
4343
end

lib/active_admin_import/import_result.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ def initialize
88
end
99

1010
def add(result, qty)
11-
@failed += result.failed_instances
12-
@total+=qty
11+
@failed += result.failed_instances
12+
@total += qty
1313
end
14-
14+
1515
def imported_qty
1616
total - failed.count
17-
end
18-
17+
end
18+
1919
def has_imported?
2020
imported_qty > 0
2121
end
22-
22+
2323
def has_failed?
2424
@failed.any?
2525
end
@@ -28,5 +28,12 @@ def empty?
2828
total == 0
2929
end
3030

31+
def failed_message(options = {})
32+
limit = options.fetch(:limit, failed.count)
33+
failed.first(limit).map{|record|
34+
errors = record.errors
35+
(errors.full_messages.zip errors.keys.map{|k| record.send k}).map{|ms| ms.join(' - ')}.join(', ')
36+
}.join(" ; ")
37+
end
3138
end
32-
end
39+
end

spec/import_result_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
describe ActiveAdminImport::ImportResult do
4+
context "failed_message" do
5+
let(:import_result) { ActiveAdminImport::ImportResult.new }
6+
7+
before do
8+
Author.create(name: 'John', last_name: 'Doe')
9+
Author.create(name: 'Jane', last_name: 'Roe')
10+
11+
@result = double \
12+
failed_instances: [
13+
Author.create(name: 'Jim', last_name: "Doe"), # {:last_name=>["has already been taken"]}
14+
Author.create(name: nil, last_name: 'Doe') # {:name=>["can't be blank"], :last_name=>["has already been taken"]}
15+
]
16+
end
17+
18+
it "should work without any failed instances" do
19+
expect(import_result.failed_message).to eq("")
20+
end
21+
22+
it "should work" do
23+
import_result.add(@result, 4)
24+
expect(import_result.failed_message).to eq("Last name has already been taken - Doe ; Name can't be blank - , Last name has already been taken - Doe")
25+
end
26+
27+
it "should work on limit param" do
28+
import_result.add(@result, 4)
29+
expect(import_result.failed_message(limit: 1)).to eq("Last name has already been taken - Doe")
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)