class AbstractImporter::CollectionImporter

Attributes

collection[R]
importer[R]
strategy[R]
summary[R]

Public Class Methods

new(importer, collection) click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 8
def initialize(importer, collection)
  @importer = importer
  @collection = collection
  @strategy = importer.strategy_for(self)
end

Public Instance Methods

each_new_record() { |dup| ... } click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 90
def each_new_record
  source.public_send(name).each do |attrs|
    yield attrs.dup
  end
end
invoke_callback(callback, *args) click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 120
def invoke_callback(callback, *args)
  callback_name = :"#{callback}_callback"
  callback = options.public_send(callback_name)
  return unless callback
  callback = importer.method(callback) if callback.is_a?(Symbol)
  callback.call(*args)
end
map_foreign_key(legacy_id, foreign_key, depends_on) click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 84
def map_foreign_key(legacy_id, foreign_key, depends_on)
  importer.map_foreign_key(legacy_id, name, foreign_key, depends_on)
end
perform!() click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 37
def perform!
  reporter.start_collection(self)
  prepare!

  invoke_callback(:before_all)
  summary.ms = Benchmark.ms do
    each_new_record do |attributes|
      strategy.process_record(attributes)
    end
  end
  strategy.flush
  invoke_callback(:after_all)

  reporter.finish_collection(self, summary)
  summary
end
prepare!() click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 56
def prepare!
  @summary = Summary.new
  @mappings = prepare_mappings!
end
prepare_mappings!() click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 61
def prepare_mappings!
  mappings = []
  model.reflect_on_all_associations.each do |association|

    # We only want the associations where this record
    # has foreign keys that refer to another
    next unless association.macro == :belongs_to

    # We support skipping some mappings entirely. I believe
    # this is largely to cut down on verbosity in the log
    # files and should be refactored to another place in time.
    foreign_key = association.foreign_key.to_sym
    next unless remap_foreign_key?(name, foreign_key)

    if association.options[:polymorphic]
      mappings << AbstractImporter::PolymorphicMapping.new(self, association)
    else
      mappings << AbstractImporter::Mapping.new(self, association)
    end
  end
  mappings
end
redundant_record?(hash) click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 108
def redundant_record?(hash)
  existing_record = invoke_callback(:finder, hash)
  if existing_record
    id_map.register(record: existing_record, legacy_id: hash[:id])
    true
  else
    false
  end
end
remap_foreign_keys!(hash) click to toggle source
# File lib/abstract_importer/collection_importer.rb, line 98
def remap_foreign_keys!(hash)
  @mappings.each_with_index do |mapping, i|
    if mapping.applicable?(hash)
      mapping.apply!(hash)
    else
      reporter.count_notice "#{mapping} will not be mapped because it is not used"
    end
  end
end