-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Samples Table: Search by metadata field and value presence (#807)
* feat: add in metadata key value searching to samples table * chore: cleanup code and add some comments * chore: add in tests for filtering samples table by metadata and fixed accessibility issue with Samples::TableComponent * chore: fix tests * chore: update metadata searching to be case insensitive * chore: fix issue with select all not updating timestamp param on page morphs * chore: fix bad rebase * chore: attempt to fix flaky test * chore: actually fix flaky test
- Loading branch information
Showing
11 changed files
with
213 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'ransack_transformer' | ||
|
||
module Irida | ||
module SearchSyntax | ||
# IRIDA Next specific Ransack search syntax parser that supports metadata_fields | ||
class Ransack | ||
def initialize(text:, params: [], metadata_fields: [], sort: nil) | ||
@transformer = RansackTransformer.new(text:, params:, metadata_fields:, sort:) | ||
@parser = ::SearchSyntax::Parser.new | ||
end | ||
|
||
def parse_with_errors(text) | ||
@transformer.transform_with_errors(@parser.parse(text || '').value) | ||
end | ||
|
||
def parse(text) | ||
parse_with_errors(text)[0] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module Irida | ||
module SearchSyntax | ||
# IRIDA Next specific RansackTransformer that supoorts metadata_fields | ||
class RansackTransformer < ::SearchSyntax::RansackTransformer | ||
def initialize(text:, params:, metadata_fields:, sort: nil) | ||
super(text:, params:, sort:) | ||
@metadata_fields = metadata_fields | ||
end | ||
|
||
# Note this method is a copy of SearchSyntax::RanstackTronsformer but with additional processing for metadata | ||
def transform_with_errors(ast) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity | ||
errors = [] | ||
result = {} | ||
|
||
if @allowed_params.length.positive? || @metadata_fields.length.positive? | ||
ast = ast.filter do |node| # rubocop:disable Metrics/BlockLength | ||
if node[:type] != :param | ||
true | ||
elsif node[:name] == @sort | ||
result[:s] = transform_sort_param(node[:value]) | ||
false | ||
elsif @allowed_params.include?(node[:name]) | ||
key = name_with_predicate(node) | ||
if result.key?(key) | ||
errors.push(::SearchSyntax::DuplicateParamError.new( | ||
name: node[:name], | ||
start: node[:start], | ||
finish: node[:finish] | ||
)) | ||
else | ||
result[key] = node[:value] | ||
end | ||
false | ||
elsif @metadata_fields.include?(node[:name].downcase) | ||
# all metadata searching uses metadata_jcont ransacker | ||
key = 'metadata_jcont' | ||
search_value = {} | ||
search_value = JSON.parse(result[key]) if result.key?(key) | ||
# if we already processed a metadata field, ignore subsequent instances of field and push an error | ||
if search_value.key?(node[:name].downcase) | ||
errors.push(::SearchSyntax::DuplicateParamError.new( | ||
name: node[:name], | ||
start: node[:start], | ||
finish: node[:finish] | ||
)) | ||
# else add it to the search_value hash and then transform back to a JSON string | ||
else | ||
search_value[node[:name].downcase] = node[:value] | ||
result[key] = JSON.generate(search_value) | ||
end | ||
false | ||
else | ||
errors.push(::SearchSyntax::UnknownParamError.new( | ||
name: node[:name], | ||
start: node[:start], | ||
finish: node[:finish], | ||
did_you_mean: @spell_checker.correct(node[:name]) | ||
)) | ||
true | ||
end | ||
end | ||
end | ||
|
||
previous = -1 | ||
result[@text] = ast.map do |node| | ||
separator = previous == node[:start] || previous == -1 ? '' : ' ' | ||
previous = node[:finish] | ||
separator + node[:raw] | ||
end.join | ||
|
||
[result, errors] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters