From eba042364611ecf62030d3ad11543ac9af53b0c0 Mon Sep 17 00:00:00 2001 From: Finn Bacall Date: Wed, 16 Aug 2023 13:58:08 +0100 Subject: [PATCH] Disable linked sample type selection if samples exist that are using that attribute. #1562 --- .../_sample_attribute_form.html.erb | 1 + .../sample_type_editing_constraints_test.rb | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/views/sample_types/_sample_attribute_form.html.erb b/app/views/sample_types/_sample_attribute_form.html.erb index 26d76db423..0f9cbb1e19 100644 --- a/app/views/sample_types/_sample_attribute_form.html.erb +++ b/app/views/sample_types/_sample_attribute_form.html.erb @@ -84,6 +84,7 @@ grouped_options_for_select(options, link_to_self ? 'self' : linked_sample_type_id), include_blank: true, + disabled: !allow_type_change, class: 'form-control linked-sample-type-selection' %> diff --git a/test/unit/samples/sample_type_editing_constraints_test.rb b/test/unit/samples/sample_type_editing_constraints_test.rb index 14aecb4b0e..915807ae43 100644 --- a/test/unit/samples/sample_type_editing_constraints_test.rb +++ b/test/unit/samples/sample_type_editing_constraints_test.rb @@ -55,6 +55,28 @@ class SampleTypeEditingConstraintsTest < ActiveSupport::TestCase refute_nil attr assert c.allow_type_change?(attr) assert c.allow_type_change?(nil) + + type = FactoryBot.create(:linked_optional_sample_type) + c = Seek::Samples::SampleTypeEditingConstraints.new(type) + assert c.allow_type_change?(:patient) + attr = c.sample_type.sample_attributes.detect { |t| t.accessor_name == 'patient' } + refute_nil attr + assert c.allow_type_change?(attr) + assert c.allow_type_change?(nil) + + person = FactoryBot.create(:person) + User.with_current_user(person.user) do + type.samples.create!(data: { title: 'Lib-3', patient: nil }, sample_type: type, project_ids: person.project_ids) + + assert Seek::Samples::SampleTypeEditingConstraints.new(type).allow_type_change?(:patient), + 'Should still allow type change because patient was blank' + + patient_sample = FactoryBot.create(:patient_sample, sample_type: attr.linked_sample_type) + type.samples.create!(data: { title: 'Lib-4', patient: patient_sample.id }, sample_type: type, project_ids: person.project_ids) + + refute Seek::Samples::SampleTypeEditingConstraints.new(type).allow_type_change?(:patient), + 'Should not allow type change because a sample exists with a patient' + end end test 'allow_required?' do