diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/search.rb | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/app/models/search.rb b/app/models/search.rb index 9886f36..b20aabf 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -23,6 +23,7 @@ class Search < ApplicationRecord # And add everything into the `res` hash with the results. res = find_by_text(plain: parsed[:plain]) + res = filter_by_kind(res:, kind: parsed[:kind]) find_by_tags(res:, tags: parsed[:tag]) end @@ -31,16 +32,19 @@ class Search < ApplicationRecord # Returns a hash which contains the tags that has been specified (`tag`) and # the plain text (`plain`) to check on the different fields. def parse_body - res = { tag: [], plain: [] } + res = { tag: [], plain: [], kind: nil } split_body.each do |part| if part.include?(':') parts = part.split(':', 2) parts[1] = clean_clause(part: parts[1]) - next if parts[0] != 'tag' - - res[parts[0].to_sym] << parts[1] + case parts[0] + when 'tag' + res[parts[0].to_sym] << parts[1] + when 'kind' + res[:kind] = Thing.kinds[parts[1]] + end else res[:plain] << part end @@ -100,6 +104,20 @@ class Search < ApplicationRecord res end + # Filter the given `res[:things]` part with the given `kind`. If `kind` is + # nil, then the same `res` object is returned. + def filter_by_kind(res:, kind:) + return res unless kind + + res[:things] = if res[:things].any? + res[:things].and(Thing.where(kind:)) + else + Thing.where(kind:) + end + + res + end + # Returns a hash which groups into taggable types the results by matching the # given `tags` which their references. It expects `res` to be already # initialized by `find_by_text` (yeah, great design, I know), which is also |
