From 5896956ee97f26a2bd59db1178fc26817db9ee36 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 10 Sep 2025 17:16:13 +0200 Subject: search: Discard comments when filtering via 'kind' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- app/models/search.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'app/models/search.rb') 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 -- cgit v1.2.3