continuum / active_importer

Define importers that load tabular data from spreadsheets or CSV files into any ActiveRecord-like ORM.

Home Page:http://continuum.github.io/active_importer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Are there any validations?

dkrusenstrahle opened this issue · comments

Hello,

First off, thank you for this cool gem!

Before I decide to use it I need to know if there is a way of using validation? If for example the Excel file does not contain a title (which is required) what happens then? Can I display errors?

Thanks!

Hi @dkrusenstrahle, thanks for your interest in our gem.

Currently, the importer checks for the presence of each declared column in the header row. If not all column names are found in a row the import process fails, firing the :import_failed event. You can catch this event in your own importers like shown below:

class MyImporter < ActiveImporter::Base
  imports MyDataModel

  column 'Title'
  # more columns here ...

  on :import_failed do |e|
     # Your code here.
  end
end

Actually, that block of code will be invoked if any problem occurs that prevent to even start processing the data in the spreadsheet. This includes the case you mention, but also includes, for instance, the case where the given file cannot be read, either because it does not exist, or because it is not in a readable format (any of the supported spreadsheet formats).

The exception object that is passed to the block can be used to determine which case you're dealing with, either by the class of the exception object, or the message. In the case you specifically want to handle, we're currently raising a RuntimeException with a descriptive message, as you can see for yourself here.

There's certainly room for improvement in this area, but this projects is still very young, so any suggestions are welcome and appreciated. We could for instance launch a dedicated exception type for active importer specific errors, and also give info of which columns were found missing.

Ah I understand! I have played with this a bit now and it seems that if I got two records (one which is complete and one which lacks a required attribute) :import_failed is not fired but :import_finished is. import_failed seems to only fire if one of the headers are wrong/missing not the records themselfs.

Also it would be cool if I was able to get the index/id of the invalid row when using :row_error. Is that possible?

Ah, sorry, I though you meant the case when the spreadsheet is missing a column, not when a single row has the corresponding cell empty. In that case the validations of your own data model should fire if that field is required, and the corresponding validation exception is caught and sent to the :row_error event, which you can receive by declaring a block similar to the one I explained before. It's also worth noticing that a row failure does not halt the import process, so rows coming after the one that failed are still processed.

As for your second question, you can indeed access the row index inside any of these event blocks. Here's an example:

  on :row_error do |e|
    puts "Could not import row at index #{row_index}: #{e.message}"
  end

  on :row_success do
    puts "Successfully imported row at index #{row_index}"
  end