Skip to content

Commit

Permalink
Merge 1f674cc into 722c641
Browse files Browse the repository at this point in the history
  • Loading branch information
Fivell committed Mar 17, 2019
2 parents 722c641 + 1f674cc commit 10521e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/active_admin_import/import_result.rb
Expand Up @@ -33,7 +33,10 @@ def failed_message(options = {})
limit = options[:limit] || failed.count
failed.first(limit).map do |record|
errors = record.errors
(errors.full_messages.zip errors.keys.map { |k| record.send k }).map { |ms| ms.join(' - ') }.join(', ')
failed_values = errors.keys.map do |key|
key == :base ? nil : record.public_send(key)
end
errors.full_messages.zip(failed_values).map { |ms| ms.compact.join(' - ') }.join(', ')
end.join(' ; ')
end
end
Expand Down
21 changes: 11 additions & 10 deletions spec/import_result_spec.rb
Expand Up @@ -5,17 +5,18 @@
context 'failed_message' do
let(:import_result) { ActiveAdminImport::ImportResult.new }

before do
Author.create(name: 'John', last_name: 'Doe')
Author.create(name: 'Jane', last_name: 'Roe')

let(:failed_instances) do
[
Author.new(last_name: 'Doe').tap {|r| r.errors.add(:last_name, :taken) },
Author.new(name: "", last_name: 'Doe').tap {|r| r.errors.add(:name, :blank); r.errors.add(:last_name, :taken) },
Author.new.tap {|r| r.errors.add(:base, 'custom') }
]
end

before do
@result = double \
failed_instances: [
# {:last_name=>["has already been taken"]}
Author.create(name: 'Jim', last_name: 'Doe'),
# {:name=>["can't be blank"], :last_name=>["has already been taken"]}
Author.create(name: nil, last_name: 'Doe')
]
failed_instances: failed_instances
end

it 'should work without any failed instances' do
Expand All @@ -26,7 +27,7 @@
import_result.add(@result, 4)
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"
"Last name has already been taken - Doe ; Name can't be blank - , Last name has already been taken - Doe ; custom"
)
end

Expand Down

0 comments on commit 10521e3

Please sign in to comment.