From eafeec51a51d803c24d7026755c58f4a62570686 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Wed, 19 Aug 2015 21:40:43 +0200
Subject: [PATCH] FIX: don't show current topic when moving posts to another
 topic

---
 .../discourse/templates/modal/merge-topic.hbs |  2 +-
 .../discourse/views/choose-topic.js.es6       | 21 ++++++++++++-------
 app/controllers/search_controller.rb          | 17 +++++++--------
 3 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/app/assets/javascripts/discourse/templates/modal/merge-topic.hbs b/app/assets/javascripts/discourse/templates/modal/merge-topic.hbs
index e4bf364fc..19160f99c 100644
--- a/app/assets/javascripts/discourse/templates/modal/merge-topic.hbs
+++ b/app/assets/javascripts/discourse/templates/modal/merge-topic.hbs
@@ -2,7 +2,7 @@
   <p>{{{i18n 'topic.merge_topic.instructions' count=selectedPostsCount}}}</p>
 
   <form>
-    {{view "choose-topic" selectedTopicId=selectedTopicId}}
+    {{view "choose-topic" currentTopicId=model.id selectedTopicId=selectedTopicId}}
   </form>
 </div>
 
diff --git a/app/assets/javascripts/discourse/views/choose-topic.js.es6 b/app/assets/javascripts/discourse/views/choose-topic.js.es6
index 7dbd8e424..da4cbac2f 100644
--- a/app/assets/javascripts/discourse/views/choose-topic.js.es6
+++ b/app/assets/javascripts/discourse/views/choose-topic.js.es6
@@ -5,14 +5,16 @@ export default Ember.View.extend({
   templateName: 'choose_topic',
 
   topicTitleChanged: function() {
-    this.set('loading', true);
-    this.set('noResults', true);
-    this.set('selectedTopicId', null);
+    this.setProperties({
+      loading: true,
+      noResults: true,
+      selectedTopicId: null,
+    });
     this.search(this.get('topicTitle'));
   }.observes('topicTitle'),
 
   topicsChanged: function() {
-    var topics = this.get('topics');
+    const topics = this.get('topics');
     if (topics) {
       this.set('noResults', topics.length === 0);
     }
@@ -20,14 +22,17 @@ export default Ember.View.extend({
   }.observes('topics'),
 
   search: debounce(function(title) {
-    var self = this;
+    const self = this,
+          currentTopicId = this.get("currentTopicId");
+
     if (Em.isEmpty(title)) {
       self.setProperties({ topics: null, loading: false });
       return;
     }
-    searchForTerm(title, {typeFilter: 'topic', searchForId: true}).then(function (results) {
+
+    searchForTerm(title, { typeFilter: 'topic', searchForId: true }).then(function (results) {
       if (results && results.posts && results.posts.length > 0) {
-        self.set('topics', results.posts.mapBy('topic'));
+        self.set('topics', results.posts.mapBy('topic').filter(t => t.get("id") !== currentTopicId));
       } else {
         self.setProperties({ topics: null, loading: false });
       }
@@ -36,7 +41,7 @@ export default Ember.View.extend({
 
   actions: {
     chooseTopic: function (topic) {
-      var topicId = Em.get(topic, 'id');
+      const topicId = Em.get(topic, 'id');
       this.set('selectedTopicId', topicId);
 
       Em.run.next(function () {
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb
index 7430d9f41..4446a0af9 100644
--- a/app/controllers/search_controller.rb
+++ b/app/controllers/search_controller.rb
@@ -12,13 +12,12 @@ class SearchController < ApplicationController
     search = Search.new(params[:q], type_filter: 'topic', guardian: guardian, include_blurbs: true, blurb_length: 300)
     result = search.execute
 
-    serializer = serialize_data(result, GroupedSearchResultSerializer, :result => result)
+    serializer = serialize_data(result, GroupedSearchResultSerializer, result: result)
 
     respond_to do |format|
       format.html do
         store_preloaded("search", MultiJson.dump(serializer))
       end
-
       format.json do
         render_json_dump(serializer)
       end
@@ -29,14 +28,14 @@ class SearchController < ApplicationController
   def query
     params.require(:term)
 
-    search_args = {guardian: guardian}
-    search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
-    if params[:include_blurbs].present?
-      search_args[:include_blurbs] = params[:include_blurbs] == "true"
-    end
-    search_args[:search_for_id] = true if params[:search_for_id].present?
+    search_args = { guardian: guardian }
+
+    search_args[:type_filter] = params[:type_filter]                 if params[:type_filter].present?
+    search_args[:include_blurbs] = params[:include_blurbs] == "true" if params[:include_blurbs].present?
+    search_args[:search_for_id] = true                               if params[:search_for_id].present?
 
     search_context = params[:search_context]
+
     if search_context.present?
       raise Discourse::InvalidParameters.new(:search_context) unless SearchController.valid_context_types.include?(search_context[:type])
       raise Discourse::InvalidParameters.new(:search_context) if search_context[:id].blank?
@@ -60,7 +59,7 @@ class SearchController < ApplicationController
 
     search = Search.new(params[:term], search_args.symbolize_keys)
     result = search.execute
-    render_serialized(result, GroupedSearchResultSerializer, :result => result)
+    render_serialized(result, GroupedSearchResultSerializer, result: result)
   end
 
 end