From 0d238db43c1c60c415a52c8e331565fd9e150f35 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 14 Apr 2014 15:20:41 -0400 Subject: [PATCH] Exclude category definition topics from similar search --- app/models/topic.rb | 22 +++++++++++++++------- spec/models/topic_spec.rb | 8 ++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index b6dc18deabd..930743c3efc 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -319,13 +319,21 @@ class Topic < ActiveRecord::Base return [] unless raw.present? # For now, we only match on title. We'll probably add body later on, hence the API hook - Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) AS similarity", title: title])) - .visible - .where(closed: false, archived: false) - .secured(Guardian.new(user)) - .listable_topics - .limit(SiteSetting.max_similar_results) - .order('similarity desc') + similar = Topic.select(sanitize_sql_array(["topics.*, similarity(topics.title, :title) AS similarity", title: title])) + .visible + .where(closed: false, archived: false) + .secured(Guardian.new(user)) + .listable_topics + .limit(SiteSetting.max_similar_results) + .order('similarity desc') + + # Exclude category definitions from similar topic suggestions + exclude_topic_ids = Category.pluck(:topic_id).compact! + if exclude_topic_ids.present? + similar = similar.where("topics.id NOT IN (?)", exclude_topic_ids) + end + + similar end def update_status(status, enabled, user) diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 74bf0deb108..d2426eac8ea 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -202,6 +202,14 @@ describe Topic do Topic.similar_to(nil, nil).should be_blank end + context "with a category definition" do + let!(:category) { Fabricate(:category) } + + it "excludes the category definition topic from similar_to" do + Topic.similar_to('category definition for', "no body").should be_blank + end + end + context 'with a similar topic' do let!(:topic) { Fabricate(:topic, title: "Evil trout is the dude who posted this topic") }