FEATURE: search fallback to tags when category not found

This commit is contained in:
Arpit Jalan 2016-06-02 18:11:24 +05:30
parent 90a27f118f
commit a166869d67
2 changed files with 12 additions and 2 deletions

View File

@ -297,7 +297,12 @@ class Search
if category_id
posts.where("topics.category_id = ?", category_id)
else
posts.where("1 = 0")
posts.where("topics.id IN (
SELECT DISTINCT(tt.topic_id)
FROM topic_tags tt, tags
WHERE tt.tag_id = tags.id
AND tags.name = ?
)", slug[0])
end
end

View File

@ -536,7 +536,7 @@ describe Search do
end
it 'supports category slug' do
it 'supports category slug and tags' do
# main category
category = Fabricate(:category, name: 'category 24', slug: 'category-24')
topic = Fabricate(:topic, created_at: 3.months.ago, category: category)
@ -552,6 +552,11 @@ describe Search do
expect(Search.execute('testing again #category-24:sub-category').posts.length).to eq(1)
expect(Search.execute('testing again #sub-category').posts.length).to eq(0)
# tags
topic.tags = [Fabricate(:tag, name: 'alpha')]
expect(Search.execute('this is a test #alpha').posts.map(&:id)).to eq([post.id])
expect(Search.execute('this is a test #beta').posts.size).to eq(0)
end
it "can find with tag" do