diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-09-10 17:16:13 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2025-09-10 21:48:58 +0200 |
| commit | 5896956ee97f26a2bd59db1178fc26817db9ee36 (patch) | |
| tree | 49b91c5dbe96d08be5805799d08312a0b4d6eed5 /app/models | |
| parent | acd25165aba6386403d42701c11c5ef133f173a0 (diff) | |
| download | operum-5896956ee97f26a2bd59db1178fc26817db9ee36.tar.gz operum-5896956ee97f26a2bd59db1178fc26817db9ee36.zip | |
search: Discard comments when filtering via 'kind'
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/search.rb | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/app/models/search.rb b/app/models/search.rb index b20aabf..22a282d 100644 --- a/app/models/search.rb +++ b/app/models/search.rb @@ -24,7 +24,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]) + find_by_tags(res:, tags: parsed[:tag], discard_comments: parsed[:kind].present?) end protected @@ -122,7 +122,7 @@ class Search < ApplicationRecord # given `tags` which their references. It expects `res` to be already # initialized by `find_by_text` (yeah, great design, I know), which is also # the structure that will be returned. - def find_by_tags(res:, tags:) + def find_by_tags(res:, tags:, discard_comments:) return res if tags.blank? tags = tags.map { |name| Tag.find_by(name:) } @@ -135,11 +135,15 @@ class Search < ApplicationRecord .order('things.rate DESC, things.title, things.created_at') .map(&:taggable) - query = TagReference.where(tag: tags, taggable_type: 'Comment') - query = query.where(taggable_id: res[:comments].pluck(:id)) if res[:comments].present? - res[:comments] = query.group(:taggable_id) - .having('count(taggable_id) = ?', tags.size) - .map(&:taggable) + if discard_comments + res[:comments] = [] + else + query = TagReference.where(tag: tags, taggable_type: 'Comment') + query = query.where(taggable_id: res[:comments].pluck(:id)) if res[:comments].present? + res[:comments] = query.group(:taggable_id) + .having('count(taggable_id) = ?', tags.size) + .map(&:taggable) + end res end |
