FEATURE: search fallback to tags when category not found
This commit is contained in:
parent
90a27f118f
commit
a166869d67
|
@ -297,7 +297,12 @@ class Search
|
||||||
if category_id
|
if category_id
|
||||||
posts.where("topics.category_id = ?", category_id)
|
posts.where("topics.category_id = ?", category_id)
|
||||||
else
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -536,7 +536,7 @@ describe Search do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'supports category slug' do
|
it 'supports category slug and tags' do
|
||||||
# main category
|
# main category
|
||||||
category = Fabricate(:category, name: 'category 24', slug: 'category-24')
|
category = Fabricate(:category, name: 'category 24', slug: 'category-24')
|
||||||
topic = Fabricate(:topic, created_at: 3.months.ago, category: category)
|
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 #category-24:sub-category').posts.length).to eq(1)
|
||||||
expect(Search.execute('testing again #sub-category').posts.length).to eq(0)
|
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
|
end
|
||||||
|
|
||||||
it "can find with tag" do
|
it "can find with tag" do
|
||||||
|
|
Loading…
Reference in New Issue