Skip to content

Commit

Permalink
Refactor code for validating models on import
Browse files Browse the repository at this point in the history
  • Loading branch information
jkowens committed Apr 4, 2016
1 parent 7787a40 commit ea02391
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,19 @@ def import_helper( *args )
end

return_obj = if is_validating
import_with_validations( column_names, array_of_attributes, options )
if models
import_with_validations( column_names, array_of_attributes, options ) do |failed|
models.each_with_index do |model, i|
model = model.dup if options[:recursive]
next if model.valid?(options[:validate_with_context])
model.send(:raise_record_invalid) if options[:raise_error]
array_of_attributes[i] = nil
failed << model
end
end
else
import_with_validations( column_names, array_of_attributes, options )
end
else
(num_inserts, ids) = import_without_validations_or_callbacks( column_names, array_of_attributes, options )
ActiveRecord::Import::Result.new([], num_inserts, ids)
Expand Down Expand Up @@ -424,21 +436,26 @@ def import_from_table( options ) # :nodoc:
def import_with_validations( column_names, array_of_attributes, options = {} )
failed_instances = []

# create instances for each of our column/value sets
arr = validations_array_for_column_names_and_attributes( column_names, array_of_attributes )
if block_given?
yield failed_instances
else
# create instances for each of our column/value sets
arr = validations_array_for_column_names_and_attributes( column_names, array_of_attributes )

# keep track of the instance and the position it is currently at. if this fails
# validation we'll use the index to remove it from the array_of_attributes
arr.each_with_index do |hsh, i|
instance = new do |model|
hsh.each_pair { |k, v| model[k] = v }
end

# keep track of the instance and the position it is currently at. if this fails
# validation we'll use the index to remove it from the array_of_attributes
arr.each_with_index do |hsh, i|
instance = new do |model|
hsh.each_pair { |k, v| model[k] = v }
next if instance.valid?(options[:validate_with_context])
raise(ActiveRecord::RecordInvalid, instance) if options[:raise_error]
array_of_attributes[i] = nil
failed_instances << instance
end

next if instance.valid?(options[:validate_with_context])
raise(ActiveRecord::RecordInvalid, instance) if options[:raise_error]
array_of_attributes[i] = nil
failed_instances << instance
end

array_of_attributes.compact!

num_inserts, ids = if array_of_attributes.empty? || options[:all_or_none] && failed_instances.any?
Expand Down

0 comments on commit ea02391

Please sign in to comment.