module Imports
Creates join records between an import and the records created for it as part of the import process, e.g. Patient, Parent, VaccinationRecord, etc, records.
This module is intended to be used as a small service object:
Imports::JoinRecords.call(import, records)
It determines:
-
import_typefromimport.class.name(e.g.ClassImport,CohortImport) -
records_typeeither fromrecords_type:(if provided) or by inferring fromrecords(all records must be the same class).
The join table model is resolved in one of two ways: 1) If a join model constant exists (e.g. +“ClassImportsPatient”+), it is used. 2) Otherwise, an anonymous ApplicationRecord subclass is created with:
- +table_name+ set to +"<import_type.tableize>_<records_type.tableize>"+, for example +"class_imports_patients"+ (depending on inflections) - a stable +model_name+ suitable for ActiveModel integration
When calling {#call}, rows are bulk-inserted using import on the join model, ignoring duplicates:
-
columns: +“<records_type.underscore>_id”+ and +“<import_type.underscore>_id”+
-
values: each record id paired with the import’s
id
Duplicate join rows are ignored via +on_duplicate_key_ignore: true+.
@attr_reader import [ApplicationRecord] the import instance being joined @attr_reader import_type [String] class name of the import (e.g. “ClassImport”) @attr_reader records [Array<ApplicationRecord>] records to be joined to the import @attr_reader records_type [String] class name of the records (e.g. “Patient”)