class SpreadsheetImport::ActiveRecordImporter::BulkImporter

Attributes

batch_size[R]

Public Class Methods

new(model, data_processor, options = {}) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/bulk_importer.rb, line 8
def initialize(model, data_processor, options = {})
  super(model, data_processor, options.merge!(skip_callbacks: true))
  @batch_size = options[:batch_size] || 100
  @batch = []
end

Public Instance Methods

create_record(data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/bulk_importer.rb, line 19
def create_record(data)
  if unique_by_attributes.nil? || unique_in_batch?(data)
    @batch << data
    batch_size == @batch.length && execute_batch
  end
end
execute_batch() click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/bulk_importer.rb, line 34
def execute_batch
  model.import(data_processor.mapping.keys,
    @batch.map(&:values), validate: !skip_validations)
  @batch = []
end
import() click to toggle source
Calls superclass method SpreadsheetImport::BaseImporter#import
# File lib/spreadsheet_import/importer/active_record_importer/bulk_importer.rb, line 14
def import
  super
  !@batch.length.zero? && execute_batch
end
unique_in_batch?(data) click to toggle source
# File lib/spreadsheet_import/importer/active_record_importer/bulk_importer.rb, line 26
def unique_in_batch?(data)
  @batch.find do |batch_record|
    unique_by_attributes.all? do |attr_name|
      data[attr_name] == batch_record[attr_name]
    end
  end.nil?
end