From 6ebddc42d1b195ff3c3205b67d73c5ad550e5362 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 10 Mar 2017 15:58:47 -0500 Subject: [PATCH] FIX: include children categories when searching a category --- lib/search.rb | 3 ++- spec/components/search_spec.rb | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index 4242ab78c6c..7c055e0a408 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -665,7 +665,8 @@ class Search end 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) posts = posts.where("topics.id = #{@search_context.id}") .order("posts.post_number") diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 4be4c155e7a..0cf29c59aa1 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -397,12 +397,17 @@ describe Search do topic = Fabricate(:topic, category: category) 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 ) _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) - expect(search.posts.length).to eq(1) - expect(search.posts.first.id).to eq(post.id) + expect(search.posts.map(&:id).sort).to eq([post.id,sub_post.id].sort) + expect(search.posts.length).to eq(2) end end