module CarefulImport::Initializer

Public Class Methods

import_helper(*args) click to toggle source

Under the hood import_helper is what is called by import and import! and it is a public method so you could directly call Configuration.import_helper(records, {}) and it would basically do what import does. So instead of overriding each of the methods we are overriding the one that they all use.

# File lib/careful_import/initializer.rb, line 22
def import_helper(*args)
  ::CarefulImport::Importer.new(self).import(*args)
end
new() click to toggle source

This wraps the activerecord-import gem's import_helper method so that we can validate the args passed to it to ensure no id keys are being used. This will ensure that we are using the generated 53-bit id instead of nextval.

# File lib/careful_import/initializer.rb, line 8
def initialize
  ActiveRecord::Base.class_eval do
    class << self
      unless ActiveRecord::Base.respond_to?(:__private_import_helper__)
        # `activerecord-import` opens up ActiveRecord::Base and adds the import methods to it directly
        # so we have to add the alias_method to it directly as well.
        # We don't want the original `import_helper` to be called without having its args validated
        # so we rename the original method which allows us to call it after validating.
        alias_method :__private_import_helper__, :import_helper
      end

      # Under the hood import_helper is what is called by import and import! and it is a public method
      # so you could directly call Configuration.import_helper(records, {}) and it would basically do what
      # import does. So instead of overriding each of the methods we are overriding the one that they all use.
      def import_helper(*args)
        ::CarefulImport::Importer.new(self).import(*args)
      end
    end
  end
end