From a8eb561cf136baae4ba1cc7c70fa4282129eb48b Mon Sep 17 00:00:00 2001 From: Theodor Vararu Date: Tue, 16 Jul 2024 15:43:28 +0200 Subject: [PATCH] Associate Locations created via import Add the model associations and populate the field when importing. --- app/models/immunisation_import.rb | 9 +++++++-- app/models/location.rb | 1 + spec/models/immunisation_import_spec.rb | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/immunisation_import.rb b/app/models/immunisation_import.rb index 78371e71f..9508cb1fc 100644 --- a/app/models/immunisation_import.rb +++ b/app/models/immunisation_import.rb @@ -27,6 +27,9 @@ class ImmunisationImport < ApplicationRecord has_many :vaccination_records, dependent: :restrict_with_exception, foreign_key: :imported_from_id + has_many :locations, + dependent: :restrict_with_exception, + foreign_key: :imported_from_id EXPECTED_HEADERS = %w[ ANATOMICAL_SITE @@ -73,8 +76,10 @@ def process!(patient_session:) .map(&:to_location) .uniq(&:urn) .reject(&:invalid?) - .map(&:attributes) - .tap { Location.create! _1 } + .each do |location| + location.imported_from = self + location.save! + end rows .map(&:to_vaccination_record) diff --git a/app/models/location.rb b/app/models/location.rb index 6e4006a37..95aa8a15c 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -32,6 +32,7 @@ class Location < ApplicationRecord has_many :sessions has_many :patients has_many :consent_forms, through: :sessions + belongs_to :imported_from, class_name: "ImmunisationImport", optional: true validates :name, presence: true validates :urn, presence: true, uniqueness: true diff --git a/spec/models/immunisation_import_spec.rb b/spec/models/immunisation_import_spec.rb index 4c65ee4b6..7201d7cec 100644 --- a/spec/models/immunisation_import_spec.rb +++ b/spec/models/immunisation_import_spec.rb @@ -86,7 +86,7 @@ expect { immunisation_import.process!(patient_session:) }.to change( immunisation_import.vaccination_records, :count - ).by(11).and change(Location, :count).by(4) + ).by(11).and change(immunisation_import.locations, :count).by(4) end end end