Skip to content

Commit

Permalink
Fix filtering of input- and output SEEK SAMPLE MULTI fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kdp-cloud committed Sep 6, 2024
1 parent c28ac92 commit 2b51066
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app/controllers/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def query
@result = filter_linked_samples(@result, :linked_samples,
{ attribute_id: params[:input_attribute_id],
attribute_value: params[:input_attribute_value],
template_id: params[:input_template_id] }, input_template_attribute&.title)
template_id: params[:input_template_id] }, input_template_attribute)
end

if params[:output_template_id].present? # linking
Expand All @@ -258,7 +258,7 @@ def query
@result = filter_linked_samples(@result, :linking_samples,
{ attribute_id: params[:output_attribute_id],
attribute_value: params[:output_attribute_value],
template_id: params[:output_template_id] }, output_template_attribute&.title)
template_id: params[:output_template_id] }, output_template_attribute)
end

@result = @result.select { |s| (project_ids & s.project_ids).any? } if project_ids.present?
Expand Down Expand Up @@ -342,12 +342,21 @@ def find_index_assets
# @option options [Integer] :attribute_id the ID of the attribute to filter by
# @param template_attribute_title [String] the title of the template attribute to filter by
# @return [Array<Sample>] the filtered list of samples
def filter_linked_samples(samples, link, options, template_attribute_title)
def filter_linked_samples(samples, link, options, template_attribute)
template_attribute_title = template_attribute&.title
samples.select do |s|
s.send(link).any? do |x|
selected = x.sample_type.template_id == options[:template_id].to_i
selected = x.get_attribute_value(template_attribute_title)&.downcase&.include?(options[:attribute_value]&.downcase) if template_attribute_title.present? && selected
selected || filter_linked_samples([x], link, options, template_attribute_title).present?
if template_attribute.sample_attribute_type.seek_sample_multi?
selected = x.get_attribute_value(template_attribute_title)&.any? { |v| v[:title].downcase.include?(options[:attribute_value]) } if template_attribute.present? && selected
elsif template_attribute.sample_attribute_type.seek_sample?
selected = x.get_attribute_value(template_attribute_title)&[:title].downcase&.include?(options[:attribute_value]) if template_attribute.present? && selected
elsif template_attribute.sample_attribute_type.seek_cv_list?
selected = x.get_attribute_value(template_attribute_title)&.any? { |v| v.downcase.include?(options[:attribute_value]) } if template_attribute.present? && selected
else
selected = x.get_attribute_value(template_attribute_title)&.downcase&.include?(options[:attribute_value]&.downcase) if template_attribute.present? && selected
end
selected || filter_linked_samples([x], link, options, template_attribute).present?
end
end
end
Expand Down
42 changes: 42 additions & 0 deletions test/functional/samples_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,48 @@ class SamplesControllerTest < ActionController::TestCase
assert_response :success
assert result = assigns(:result)
assert_equal 1, result.length

# Simple query on 'Input' attribute (SEEK_SAMPLE_MULTI type)
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template2.id,
template_attribute_id: template2.template_attributes.detect(&:input_attribute?)&.id,
template_attribute_value: 'source'
}

assert_response :success
result = assigns(:result)
assert_equal result.length, 1

# parent query on 'Input' attribute (SEEK_SAMPLE_MULTI type)
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template3.id,
template_attribute_id: template3.template_attributes.second.id,
template_attribute_value: 'Protocol',
input_template_id: template2.id,
input_attribute_id: template2.template_attributes.detect(&:input_attribute?)&.id,
input_attribute_value: "source"
}

assert_response :success
result = assigns(:result)
assert_equal result.length, 1

# Grandchild query on 'Input' attribute (SEEK_SAMPLE_MULTI type)
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template1.id,
template_attribute_id: template1.template_attributes.third.id,
template_attribute_value: "x's",
output_template_id: template3.id,
output_attribute_id: template3.template_attributes.detect(&:input_attribute?)&.id,
output_attribute_value: 'sample'
}

assert_response :success
result = assigns(:result)
assert_equal result.length, 1
end
end

Expand Down

0 comments on commit 2b51066

Please sign in to comment.