Pull request Compare This branch is 4 commits ahead of atlaswebdev:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Importer Plugin

Importer imports data from a CSV and creates record in the application database.

Basic importing

Organisation.import(:from => '/path/to/file.csv')

This will extract data straight from the CSV

Importing from a stream

Importer will also take a raw StringIO from a form POST:

Organisation.import(:from => params[:organisations])

CSV Columns

If no columns are specified, Importer will use the first row as column names. To set columns, you can use the columns option:

Organisation.import(:from => '/path/to/file.csv', :columns => ['name','email'])

Column Name Mapping

If you have a CSV with column names in the first row that do not correspond with your database columns, you can map them with a hash:

Organisation.import(:from => '/path/to/file.csv', :map => {'Org' => 'name', 'EMail' => 'email'})

This is particularly useful if your CSV is generated automatically from another system that you have no control over.


Take the row following for example:

"Curve21","South-East England","",""

‘South-East England’ is this organisation’s region. If Organisation has_many :regions then we need a way to look up the region in our database using the name specified in this row. Importer achieves this using Proc objects:

Organisation.import(:from => '/path/to/file.csv', :map => {'region' => 'region_id'}, 
                            :associations => {'region_id' => lambda {|a| Region.find_by_name(a) } }

Currently, this is only designed to support only belongs_to and has_one relationships.

Error handling

Error handling uses logger at the moment and warns when an associated record was not found