From a008e4625c18b9e6fa3b8b85fc69c866e77bdf52 Mon Sep 17 00:00:00 2001 From: Jake Benilov Date: Mon, 22 Jul 2024 17:07:32 +0100 Subject: [PATCH] Vaccs recording: make the back links work as expected This requires pre-populating the VaccinationRecord with "delivery_site_other" value out of the session, and "todays_batch", otherwise the form steps aren't derived correctly. --- app/controllers/vaccinations/edit_controller.rb | 9 +++++++++ app/controllers/vaccinations_controller.rb | 4 ++++ app/helpers/vaccinations_helper.rb | 10 ++++++++++ app/models/vaccination_record.rb | 9 +++------ app/views/vaccinations/edit/batch.html.erb | 2 +- app/views/vaccinations/edit/confirm.html.erb | 2 +- app/views/vaccinations/edit/delivery-site.html.erb | 2 +- app/views/vaccinations/edit/reason.html.erb | 2 +- 8 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/controllers/vaccinations/edit_controller.rb b/app/controllers/vaccinations/edit_controller.rb index 661d05b9b..162f22fab 100644 --- a/app/controllers/vaccinations/edit_controller.rb +++ b/app/controllers/vaccinations/edit_controller.rb @@ -43,6 +43,8 @@ def handle_confirm send_vaccination_mail(@draft_vaccination_record) @patient_session.do_vaccination! + session.delete(:delivery_site_other) + heading = if @draft_vaccination_record.administered? t("vaccinations.flash.given") @@ -87,6 +89,13 @@ def set_steps def set_draft_vaccination_record @draft_vaccination_record = @patient_session.draft_vaccination_record + if (session[:delivery_site_other] = "true") + @draft_vaccination_record.delivery_site_other = true + end + if todays_batch_id.present? + @draft_vaccination_record.todays_batch = + policy_scope(Batch).find(todays_batch_id) + end end def set_batches diff --git a/app/controllers/vaccinations_controller.rb b/app/controllers/vaccinations_controller.rb index 862b7de13..235e15099 100644 --- a/app/controllers/vaccinations_controller.rb +++ b/app/controllers/vaccinations_controller.rb @@ -54,6 +54,10 @@ def index def create if @draft_vaccination_record.update create_params.merge(user: current_user) + session[:delivery_site_other] = "true" if delivery_site_param_other? + @draft_vaccination_record.todays_batch = + todays_batch_id if todays_batch_id.present? + redirect_to session_patient_vaccinations_edit_path( @session, patient_id: @patient.id, diff --git a/app/helpers/vaccinations_helper.rb b/app/helpers/vaccinations_helper.rb index 34b404967..170bcca06 100644 --- a/app/helpers/vaccinations_helper.rb +++ b/app/helpers/vaccinations_helper.rb @@ -38,4 +38,14 @@ def in_tab_vaccinated?(_action, outcome) def in_tab_not_vaccinated?(_action, outcome) outcome.in? %i[do_not_vaccinate not_vaccinated] end + + # rubocop:disable Rails/HelperInstanceVariable + def vaccinations_back_link_path + if current_step?(@draft_vaccination_record.form_steps.first.to_s) + session_patient_path(@session, id: @patient.id) + else + previous_wizard_path + end + end + # rubocop:enable Rails/HelperInstanceVariable end diff --git a/app/models/vaccination_record.rb b/app/models/vaccination_record.rb index 7dff44588..4cd57e2a9 100644 --- a/app/models/vaccination_record.rb +++ b/app/models/vaccination_record.rb @@ -130,10 +130,7 @@ class VaccinationRecord < ApplicationRecord end on_wizard_step :reason, exact: true do - validates :reason, - inclusion: { - in: VaccinationRecord.reasons.keys - } + validates :reason, inclusion: { in: VaccinationRecord.reasons.keys } end on_wizard_step :batch, exact: true do @@ -154,8 +151,8 @@ def recorded? def form_steps [ - ("delivery-site" if administered? && delivery_site.nil?), - (:batch if administered? && batch_id.nil?), + ("delivery-site" if administered? && delivery_site_other), + (:batch if administered? && todays_batch.nil?), (:reason if not_administered?), :confirm ].compact diff --git a/app/views/vaccinations/edit/batch.html.erb b/app/views/vaccinations/edit/batch.html.erb index a6a93e072..9a3a2c057 100644 --- a/app/views/vaccinations/edit/batch.html.erb +++ b/app/views/vaccinations/edit/batch.html.erb @@ -1,6 +1,6 @@ <% content_for :before_main do %> <%= render AppBacklinkComponent.new( - href: previous_wizard_path, + href: vaccinations_back_link_path, name: "vaccination page", ) %> <% end %> diff --git a/app/views/vaccinations/edit/confirm.html.erb b/app/views/vaccinations/edit/confirm.html.erb index e0fb12415..e6b45d3ad 100644 --- a/app/views/vaccinations/edit/confirm.html.erb +++ b/app/views/vaccinations/edit/confirm.html.erb @@ -1,7 +1,7 @@ <% content_for :before_main do %> <%= render AppBacklinkComponent.new( - href: previous_wizard_path, + href: vaccinations_back_link_path, name: "vaccination page", ) %> <% end %> diff --git a/app/views/vaccinations/edit/delivery-site.html.erb b/app/views/vaccinations/edit/delivery-site.html.erb index 4b2bda8e2..53aee82da 100644 --- a/app/views/vaccinations/edit/delivery-site.html.erb +++ b/app/views/vaccinations/edit/delivery-site.html.erb @@ -1,6 +1,6 @@ <% content_for :before_main do %> <%= render AppBacklinkComponent.new( - href: session_patient_vaccinations_path(@session, @patient), + href: vaccinations_back_link_path, name: "vaccination page", ) %> <% end %> diff --git a/app/views/vaccinations/edit/reason.html.erb b/app/views/vaccinations/edit/reason.html.erb index a4b2feb07..0ee9eba64 100644 --- a/app/views/vaccinations/edit/reason.html.erb +++ b/app/views/vaccinations/edit/reason.html.erb @@ -1,6 +1,6 @@ <% content_for :before_main do %> <%= render AppBacklinkComponent.new( - href: previous_wizard_path, + href: vaccinations_back_link_path, name: "vaccination page", ) %> <% end %>