From 1313c0f0944a2a17c45f4e1929b5e5bb8b4e83a6 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 24 May 2013 16:17:09 -0400 Subject: [PATCH] FIX: When using a search context, *prefer* the context's results, don't restrict to only them. --- lib/search.rb | 22 +++++++++++++--------- spec/components/search_spec.rb | 12 ++++++++---- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index ac3d61f818..e3864d9446 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -138,30 +138,34 @@ class Search .where("topics.deleted_at" => nil) .where("topics.visible") .where("topics.archetype <> ?", Archetype.private_message) - .order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC") - .order("TS_RANK_CD(post_search_data.search_data, #{ts_query}) DESC") - .order("topics.bumped_at DESC") - .limit(limit) - # Search context post results + + # If we have a search context, prioritize those posts first if @search_context.present? if @search_context.is_a?(User) - # If the context is a user, restrict posts to that user - posts = posts.where(user_id: @search_context.id) + # If the context is a user, prioritize that user's posts + posts = posts.order("CASE WHEN posts.user_id = #{@search_context.id} THEN 0 ELSE 1 END") elsif @search_context.is_a?(Category) # If the context is a category, restrict posts to that category - posts = posts.where('topics.category_id' => @search_context.id) + posts = posts.order("CASE WHEN topics.category_id = #{@search_context.id} THEN 0 ELSE 1 END") end end + posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC") + .order("TS_RANK_CD(post_search_data.search_data, #{ts_query}) DESC") + .order("topics.bumped_at DESC") + + + + if secure_category_ids.present? posts = posts.where("(categories.id IS NULL) OR (NOT categories.secure) OR (categories.id IN (?))", secure_category_ids) else posts = posts.where("(categories.id IS NULL) OR (NOT categories.secure)") end - posts + posts.limit(limit) end def query_locale diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 4ae9e42cca..b3eeb134f2 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -239,11 +239,14 @@ describe Search do context 'user as a search context' do let(:search_user) { Search.new('hello', search_context: post.user).execute } - let(:search_coding_horror) { Search.new('hello', search_context: Fabricate(:coding_horror)).execute } + let(:coding_horror) { Fabricate(:coding_horror) } + let(:search_coding_horror) { Search.new('hello', search_context: coding_horror).execute } Given!(:post) { Fabricate(:post) } + Given!(:coding_horror_post) { Fabricate(:post, user: coding_horror )} + Then { first_of_type(search_user, 'topic')['id'] == post.topic_id } - And { first_of_type(search_coding_horror, 'topic').should be_blank } + And { first_of_type(search_user, 'topic')['id'] == coding_horror_post.topic_id } end context 'category as a search context' do @@ -251,10 +254,11 @@ describe Search do let(:search_cat) { Search.new('hello', search_context: category).execute } let(:search_other_cat) { Search.new('hello', search_context: Fabricate(:category) ).execute } let(:topic) { Fabricate(:topic, category: category) } + let(:topic_no_cat) { Fabricate(:topic) } Given!(:post) { Fabricate(:post, topic: topic, user: topic.user ) } - Then { first_of_type(search_cat, 'topic')['id'] == post.topic_id } - Then { first_of_type(search_other_cat, 'topic').should be_blank } + Then { first_of_type(search_cat, 'topic')['id'] == topic.id } + Then { first_of_type(search_cat, 'topic')['id'] == topic_no_cat.id } end