Skip to content

Commit

Permalink
Disable linked sample type selection if samples exist that are using …
Browse files Browse the repository at this point in the history
…that attribute. #1562
  • Loading branch information
fbacall committed Aug 16, 2023
1 parent 830bffa commit eba0423
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/views/sample_types/_sample_attribute_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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' %>
</div>
</td>
Expand Down
22 changes: 22 additions & 0 deletions test/unit/samples/sample_type_editing_constraints_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eba0423

Please sign in to comment.