From 2b51066a3d66e506029719dc843cd646432aef2e Mon Sep 17 00:00:00 2001 From: Kevin De Pelseneer Date: Fri, 6 Sep 2024 16:06:47 +0200 Subject: [PATCH] Fix filtering of input- and output SEEK SAMPLE MULTI fields --- app/controllers/samples_controller.rb | 19 +++++++--- test/functional/samples_controller_test.rb | 42 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/app/controllers/samples_controller.rb b/app/controllers/samples_controller.rb index 4d99fb7fb6..b5ab07682e 100644 --- a/app/controllers/samples_controller.rb +++ b/app/controllers/samples_controller.rb @@ -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 @@ -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? @@ -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] 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 diff --git a/test/functional/samples_controller_test.rb b/test/functional/samples_controller_test.rb index 1bf190b00c..8e8bc52df5 100644 --- a/test/functional/samples_controller_test.rb +++ b/test/functional/samples_controller_test.rb @@ -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