Verified Commit f5e28fd2 authored by Alexis Reigel's avatar Alexis Reigel
Browse files

add report (number of inserts, failures, total)

parent 1a070fa8
......@@ -4,13 +4,14 @@ require 'active_record'
require 'excelsior/source'
require 'excelsior/mapping'
require 'excelsior/error'
require 'excelsior/report'
module Excelsior
class Import
include Source
include Mapping
attr_accessor :source, :fields, :errors
attr_accessor :source, :fields, :errors, :report
attr_accessor :rows, :columns
def initialize(file = nil)
......@@ -23,6 +24,8 @@ module Excelsior
@columns = @sheet.rows.shift
@rows = @sheet.rows
@report = Report.new
valid?
end
......@@ -56,7 +59,12 @@ module Excelsior
end
def add_model_errors(record, index)
return if record.errors.empty?
if record.errors.empty?
@report.inserted += 1
return
end
@report.failed += 1
@errors[:model] ||= []
......
module Excelsior
Report = Struct.new(:inserted, :failed) do
def initialize(*args)
super(*args)
self.inserted ||= 0
self.failed ||= 0
end
def total
inserted + failed
end
end
end
......@@ -81,4 +81,11 @@ class ExcelsiorTest < Minitest::Test
assert import.errors[:model].any?
assert_equal import.errors[:model], [Excelsior::Error.new(3, ["First name can't be blank"])]
end
def test_report
import = UserImport.new("test/files/missing-first-name.xlsx").tap(&:run)
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