pcreux / csv-importer

CSV Import for humans on Ruby / Ruby on Rails
MIT License
590 stars 67 forks source link

Add default_values configuration #113

Open whatcould opened 2 months ago

whatcould commented 2 months ago

This provides a way to set defaults on each model — without adding an extra column to the CSV.

Some potential uses could be batch number, imported_by, etc etc.

whatcould commented 2 months ago

Note that these are added before the find_model step, so you can use these defaults as part of the identifiers to look up existing records.

pcreux commented 2 months ago

Hey @whatcould ! Thank you for your PR.

I was about to say you could use after_build to set defaults, but it doesn't allow you to use these defaults as part of the identifiers to look up existing records. since it's called after find_or_build.

What about updating the flow to call after_build in build_model instead of after find_or_build.

That way, we wouldn't introduce a new way to set default values, and I believe that it would also improve the current behavior: calling after_build on existing models can seem counterintuitive.

Thoughts?