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

don't run when the initial valid check fails

parent e8ddf4f1
......@@ -30,10 +30,12 @@ module Excelsior
@report = Report.new
valid?
validate!
end
def run(&block)
return unless valid?
if self.class.use_transaction
model_class.transaction do
insert_rows(&block)
......@@ -45,7 +47,7 @@ module Excelsior
end
end
def valid?
def validate!
@errors = fields.to_a.each_with_object({}) do |f, acc|
acc[:missing_column] ||= []
......@@ -53,6 +55,10 @@ module Excelsior
end
end
def valid?
@errors[:missing_column].empty?
end
private
def model_class
......@@ -60,6 +66,8 @@ module Excelsior
end
def add_model_errors(record, index)
@errors[:model] ||= []
if record.errors.empty?
report_insert
return
......@@ -67,8 +75,6 @@ module Excelsior
report_failure
@errors[:model] ||= []
@errors[:model] << Error.new(index + 1, record.errors.full_messages)
end
......
......@@ -156,6 +156,12 @@ describe Excelsior do
assert import.errors[:missing_column].any?
end
it 'does not run the import' do
import = UserImport.new('test/files/missing-column.xlsx')
import.run
assert_equal 0, import.report.total
end
it 'returns the model validation errors' do
import = UserImport.new('test/files/missing-first-name.xlsx').tap(&:run)
assert import.errors[:model].any?
......
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