Verified Commit 982c884a authored by Alexis Reigel's avatar Alexis Reigel
Browse files

report for run with custom block

parent f5e28fd2
......@@ -33,7 +33,13 @@ module Excelsior
@rows.map.with_index do |row, i|
attributes = map_row_values(row, @columns)
if block_given?
yield attributes
begin
result = yield attributes
report_insert
result
rescue
report_failure
end
else
record = model_class.create(attributes)
add_model_errors(record, i)
......@@ -60,15 +66,23 @@ module Excelsior
def add_model_errors(record, index)
if record.errors.empty?
@report.inserted += 1
report_insert
return
end
@report.failed += 1
report_failure
@errors[:model] ||= []
@errors[:model] << Error.new(index + 1, record.errors.full_messages)
end
def report_insert
@report.inserted += 1
end
def report_failure
@report.failed += 1
end
end
end
......@@ -88,4 +88,15 @@ class ExcelsiorTest < Minitest::Test
assert_equal 1, import.report.failed
assert_equal 3, import.report.total
end
def test_report_with_block
import = UserImport.new("test/files/missing-first-name.xlsx")
import.run do |v|
raise "failure!" if v[:first_name].nil?
v
end
assert_equal 2, import.report.inserted
assert_equal 1, import.report.failed
assert_equal 3, import.report.total
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment