Skip to content

Commit

Permalink
Allow adding optional attribute to existing sample type
Browse files Browse the repository at this point in the history
  • Loading branch information
kdp-cloud committed Oct 21, 2024
1 parent d44fafc commit 3cb7b13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/seek/samples/sample_type_editing_constraints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def samples?
# if attr is nil, indicates a new attribute. required is not allowed if there are already samples
def allow_required?(attr)
if attr.is_a?(SampleAttribute)
return true if attr.new_record?
return true if attr.new_record? && @sample_type.new_record?
return false if inherited?(attr)

attr = attr.accessor_name
Expand Down
20 changes: 16 additions & 4 deletions test/functional/sample_types_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -775,16 +775,28 @@ class SampleTypesControllerTest < ActionController::TestCase
get :edit, params: { id: sample_type.id }
assert_response :success
assert_select 'a#add-attribute', count: 1
assert_difference('SampleAttribute.count') do
put :update, params: { id: sample_type.id, sample_type: {

# Should be able to add an optional new attribute to a sample type with samples
assert_difference('SampleAttribute.count', 1) do
patch :update, params: { id: sample_type.id, sample_type: {
sample_attributes_attributes: {
'1': { title: 'new attribute', sample_attribute_type_id: @string_type.id }
'1': { title: 'new optional attribute', sample_attribute_type_id: @string_type.id, required: '0' }
}
} }
end
assert_redirected_to sample_type_path(sample_type)
sample_type.reload
assert_equal 'new attribute', sample_type.sample_attributes.last.title
assert_equal 'new optional attribute', sample_type.sample_attributes.last.title

# Should not be able to add a mandatory new attribute to a sample type with samples
assert_no_difference('SampleAttribute.count') do
patch :update, params: { id: sample_type.id, sample_type: {
sample_attributes_attributes: {
'2': { title: 'new mandatory attribute', sample_attribute_type_id: @string_type.id, required: '1' }
}
} }
end

end

test 'change attribute name' do
Expand Down

0 comments on commit 3cb7b13

Please sign in to comment.