-
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.
feat: add in metadata key value searching to samples table
- Loading branch information
Showing
9 changed files
with
117 additions
and
9 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,71 @@ | ||
module Irida | ||
Check warning on line 1 in lib/irida/search_syntax/ransack_transformer.rb GitHub Actions / runner / rubocop
|
||
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 | ||
|
||
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]) | ||
key = 'metadata_jcont' | ||
search_value = {} | ||
search_value = JSON.parse(result[key]) if result.key?(key) | ||
if search_value.key?(node[:name]) | ||
errors.push(::SearchSyntax::DuplicateParamError.new( | ||
name: node[:name], | ||
start: node[:start], | ||
finish: node[:finish] | ||
)) | ||
else | ||
search_value[node[:name]] = 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 |