diff options
Diffstat (limited to 'app')
| -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 |
