aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.rubocop.yml2
-rw-r--r--app/models/search.rb18
-rw-r--r--test/fixtures/comments.yml1
3 files changed, 13 insertions, 8 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index 764bab0..cfd8e63 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -44,7 +44,7 @@ Layout/LineLength:
# The default is just too small.
Metrics/AbcSize:
- Max: 30
+ Max: 40
# We will skip it for tests.
Metrics/ClassLength:
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
diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml
index f61437d..78e08db 100644
--- a/test/fixtures/comments.yml
+++ b/test/fixtures/comments.yml
@@ -1,2 +1,3 @@
comment1:
+ content: some
thing_id: <%= ActiveRecord::FixtureSet.identify(:thing1) %>