aboutsummaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-09-10 17:14:08 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-09-10 17:14:08 +0200
commit2574d61593df31130de8ab8081c7f64038df8db4 (patch)
treed630102234a7fc59ebfb904939d2d87ec833a0f2 /app/models
parent9b9f8ac28f34d85f52c30392f5bc07470d159005 (diff)
downloadoperum-2574d61593df31130de8ab8081c7f64038df8db4.tar.gz
operum-2574d61593df31130de8ab8081c7f64038df8db4.zip
search: Add a 'kind' parameter
This way you can also filter by the kind of resource you want to be given on the searches page. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'app/models')
-rw-r--r--app/models/search.rb26
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