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

Provide unique data for each created model?

dkrusenstrahle opened this issue · comments

I need to use Active_importer from a background service (Resque). The problem I got is that I need to let every new model created get the same account_id (account_id will vary from user to user of course). Is there a way to provide the individual account_id to be used for each import job?

Thanks!

You could listen to the event "row_processing", and inside the block you can call a method that defines an intance variable for your account, but you would need to specify in a cell an attribute to find the account, it could be the id or a Name.

f.i

   on :row_processing do
       load_account
       model.account_id = @account.id
   end

  def load_account
     unless @account
        @account = Account.find_by!(name: row['Account'])
        rescue
           raise "Account '#{row['Account']}' not found"
     end
  end

The Account name in this case must be in the first row to proccess, and it will set the @account, the other rows will be use the same instance.

Or you could define an attr_accessor :account in your importer class, and set it before call the method import for your importer instance, then in the row_processing block you could assign the account_id to your model

@dkrusenstrahle I think what you're asking is a way to pass custom parameters to an importer when invoking it, and then make them available as hash or something to be used inside the different callback events or column definition blocks. If that's what you need, then that's a nice feature suggestion. I'll get to work on it and let you know.

In the mean time, the suggestion made by @lporras just above could work just fine for the time being.

I'm looking forward to any comments you might have about this.

Yes, that is what I need @gnapse. Would be super cool with such a function.

Actually, there is already a way to do this. I did not remember it because it is not documented in the README and I haven't used it. I just implemented it thinking of a scenario like yours. This is how you do it:

MyImporter.import(filename, context: @whatever)

You pass the context option with whatever value you need to pass. It need not be a single value, you can pass an array or even a hash. It'll be available inside the importer blocks by simply referencing context, like this:

class MyImporter < ActiveImporter::Base
  imports Anything

  on :row_processing do
    model.owner_id = context[:owner_id]
  end
end

And then you can invoke the worker like this:

MyImporter.import(filename, context: { owner_id: @user.id })

I'll take some time to document this feature and write some tests. I hope this covers your question, and if it does, please let us know and close this issue.

I renamed the reference to params instead of context, because it just seems more natural. I also added specs and some documentation in the README. This should close this issue.