FIX: include children categories when searching a category

This commit is contained in:
Sam 2017-03-10 15:58:47 -05:00
parent 16593ae8bf
commit 6ebddc42d1
2 changed files with 9 additions and 3 deletions

View File

@ -665,7 +665,8 @@ class Search
end end
elsif @search_context.is_a?(Category) elsif @search_context.is_a?(Category)
posts = posts.where("topics.category_id = #{@search_context.id}") category_ids = [@search_context.id] + Category.where(parent_category_id: @search_context.id).pluck(:id)
posts = posts.where("topics.category_id in (?)", category_ids)
elsif @search_context.is_a?(Topic) elsif @search_context.is_a?(Topic)
posts = posts.where("topics.id = #{@search_context.id}") posts = posts.where("topics.id = #{@search_context.id}")
.order("posts.post_number") .order("posts.post_number")

View File

@ -397,12 +397,17 @@ describe Search do
topic = Fabricate(:topic, category: category) topic = Fabricate(:topic, category: category)
topic_no_cat = Fabricate(:topic) topic_no_cat = Fabricate(:topic)
# includes subcategory in search
subcategory = Fabricate(:category, parent_category_id: category.id)
sub_topic = Fabricate(:topic, category: subcategory)
post = Fabricate(:post, topic: topic, user: topic.user ) post = Fabricate(:post, topic: topic, user: topic.user )
_another_post = Fabricate(:post, topic: topic_no_cat, user: topic.user ) _another_post = Fabricate(:post, topic: topic_no_cat, user: topic.user )
sub_post = Fabricate(:post, raw: 'I am saying hello from a subcategory', topic: sub_topic, user: topic.user )
search = Search.execute('hello', search_context: category) search = Search.execute('hello', search_context: category)
expect(search.posts.length).to eq(1) expect(search.posts.map(&:id).sort).to eq([post.id,sub_post.id].sort)
expect(search.posts.first.id).to eq(post.id) expect(search.posts.length).to eq(2)
end end
end end