mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 10:58:31 -05:00
If you search a category by id, also include its children
This commit is contained in:
parent
e393e43ce5
commit
b9df18360d
2 changed files with 9 additions and 6 deletions
|
@ -275,9 +275,9 @@ class Search
|
||||||
end
|
end
|
||||||
|
|
||||||
advanced_filter(/category:(.+)/) do |posts,match|
|
advanced_filter(/category:(.+)/) do |posts,match|
|
||||||
category_id = Category.where('name ilike ? OR id = ?', match, match.to_i).pluck(:id).first
|
category_ids = Category.where('name ilike ? OR id = ? OR parent_category_id = ?', match, match.to_i, match.to_i).pluck(:id)
|
||||||
if category_id
|
if category_ids.present?
|
||||||
posts.where("topics.category_id = ?", category_id)
|
posts.where("topics.category_id IN (?)", category_ids)
|
||||||
else
|
else
|
||||||
posts.where("1 = 0")
|
posts.where("1 = 0")
|
||||||
end
|
end
|
||||||
|
|
|
@ -428,7 +428,7 @@ describe Search do
|
||||||
|
|
||||||
it 'supports wiki' do
|
it 'supports wiki' do
|
||||||
topic = Fabricate(:topic)
|
topic = Fabricate(:topic)
|
||||||
wiki_post = Fabricate(:post, raw: 'this is a test 248', wiki: true, topic: topic)
|
Fabricate(:post, raw: 'this is a test 248', wiki: true, topic: topic)
|
||||||
|
|
||||||
expect(Search.execute('test 248 in:wiki').posts.length).to eq(1)
|
expect(Search.execute('test 248 in:wiki').posts.length).to eq(1)
|
||||||
end
|
end
|
||||||
|
@ -543,14 +543,17 @@ describe Search do
|
||||||
post = Fabricate(:post, raw: 'hi this is a test 123', topic: topic)
|
post = Fabricate(:post, raw: 'hi this is a test 123', topic: topic)
|
||||||
|
|
||||||
expect(Search.execute('this is a test #category-24').posts.length).to eq(1)
|
expect(Search.execute('this is a test #category-24').posts.length).to eq(1)
|
||||||
|
expect(Search.execute("this is a test category:#{category.id}").posts.length).to eq(1)
|
||||||
expect(Search.execute('this is a test #category-25').posts.length).to eq(0)
|
expect(Search.execute('this is a test #category-25').posts.length).to eq(0)
|
||||||
|
|
||||||
# sub category
|
# sub category
|
||||||
sub_category = Fabricate(:category, name: 'sub category', slug: 'sub-category', parent_category_id: category.id)
|
sub_category = Fabricate(:category, name: 'sub category', slug: 'sub-category', parent_category_id: category.id)
|
||||||
second_topic = Fabricate(:topic, created_at: 3.months.ago, category: sub_category)
|
second_topic = Fabricate(:topic, created_at: 3.months.ago, category: sub_category)
|
||||||
second_topic_post = Fabricate(:post, raw: 'hi testing again 123', topic: second_topic)
|
Fabricate(:post, raw: 'hi testing again 123', topic: second_topic)
|
||||||
|
|
||||||
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 category:#{category.id}").posts.length).to eq(2)
|
||||||
|
expect(Search.execute("testing again category:#{sub_category.id}").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
|
# tags
|
||||||
|
@ -562,7 +565,7 @@ describe Search do
|
||||||
it "can find with tag" do
|
it "can find with tag" do
|
||||||
topic1 = Fabricate(:topic, title: 'Could not, would not, on a boat')
|
topic1 = Fabricate(:topic, title: 'Could not, would not, on a boat')
|
||||||
topic1.tags = [Fabricate(:tag, name: 'eggs'), Fabricate(:tag, name: 'ham')]
|
topic1.tags = [Fabricate(:tag, name: 'eggs'), Fabricate(:tag, name: 'ham')]
|
||||||
post1 = Fabricate(:post, topic: topic1)
|
Fabricate(:post, topic: topic1)
|
||||||
post2 = Fabricate(:post, topic: topic1, raw: "It probably doesn't help that they're green...")
|
post2 = Fabricate(:post, topic: topic1, raw: "It probably doesn't help that they're green...")
|
||||||
|
|
||||||
expect(Search.execute('green tags:eggs').posts.map(&:id)).to eq([post2.id])
|
expect(Search.execute('green tags:eggs').posts.map(&:id)).to eq([post2.id])
|
||||||
|
|
Loading…
Reference in a new issue