Sha256: 49ff7f6f258f0696d24d86c3b66d52d716eda73bbf3da2e5582c620ab6d05899

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'csv'

class Import
  attr_reader :klass, :file

  def initialize(klass, file)
    @klass = klass
    @file = file

    validate_import

    # TODO: initialize import job instead of doing import here
    perform!
  end

  def perform!
    # TODO: this creates a new object per CSV row. It may be worth optimizing further to use
    # something like this approach to do only a single insert.
    # https://gist.github.com/jackrg/76ade1724bd816292e4e
    CSV.foreach(file.tempfile,
                headers: true,
                encoding:'iso-8859-1:utf-8',
                skip_blanks: true,
                skip_lines: /^(?:,\s*)+$/,
                header_converters: -> (f) { f.strip },
                converters: -> (f) { f ? f.strip : nil } ) do |row|
      attrs = row.to_h.compact
      klass.find_or_create_by!(attrs)
    end
  end

  private

    def validate_import
      unless file.content_type == 'text/csv'
        raise "Import must be a .csv file."
      end

      unless klass.present?
        raise "A valid model must be supplied to the import."
      end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
forest_cms-0.98.1 app/models/import.rb