From 706ea28ef96f3cdb74dc6fdd96bf9812e65a5d99 Mon Sep 17 00:00:00 2001
From: Arpit Jalan <arpit@techapj.com>
Date: Tue, 3 May 2016 14:37:57 +0530
Subject: [PATCH] FIX: prepend 'continue discussion' link to topic template

---
 .../discourse/controllers/topic.js.es6            |  2 +-
 .../javascripts/discourse/models/composer.js.es6  |  9 +++++++++
 test/javascripts/models/composer-test.js.es6      | 15 +++++++++++++++
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6
index 3607e1d9f..71be8528f 100644
--- a/app/assets/javascripts/discourse/controllers/topic.js.es6
+++ b/app/assets/javascripts/discourse/controllers/topic.js.es6
@@ -509,7 +509,7 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
       }).then(q => {
         const postUrl = `${location.protocol}//${location.host}${post.get('url')}`;
         const postLink = `[${Handlebars.escapeExpression(self.get('model.title'))}](${postUrl})`;
-        composerController.get('model').appendText(`${I18n.t("post.continue_discussion", { postLink })}\n\n${q}`);
+        composerController.get('model').prependText(`${I18n.t("post.continue_discussion", { postLink })}\n\n${q}`, {new_line: true});
       });
     },
 
diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6
index af4c9c41a..b7d5a318b 100644
--- a/app/assets/javascripts/discourse/models/composer.js.es6
+++ b/app/assets/javascripts/discourse/models/composer.js.es6
@@ -360,6 +360,15 @@ const Composer = RestModel.extend({
     return before.length + text.length;
   },
 
+  prependText(text, opts) {
+    const reply = (this.get('reply') || '');
+
+    if (opts && opts.new_line && reply.length > 0) {
+      text = text.trim() + "\n\n";
+    }
+    this.set('reply', text + reply);
+  },
+
   applyTopicTemplate(oldCategoryId, categoryId) {
     if (this.get('action') !== CREATE_TOPIC) { return; }
     let reply = this.get('reply');
diff --git a/test/javascripts/models/composer-test.js.es6 b/test/javascripts/models/composer-test.js.es6
index c156c9230..d541aaeb0 100644
--- a/test/javascripts/models/composer-test.js.es6
+++ b/test/javascripts/models/composer-test.js.es6
@@ -95,6 +95,21 @@ test("appendText", function() {
   equal(composer.get("reply"), "c\n\nab");
 });
 
+test("prependText", function() {
+  const composer = createComposer();
+
+  blank(composer.get('reply'), "the reply is blank by default");
+
+  composer.prependText("hello");
+  equal(composer.get('reply'), "hello", "it prepends text to nothing");
+
+  composer.prependText("world ");
+  equal(composer.get('reply'), "world hello", "it prepends text to existing text");
+  
+  composer.prependText("before new line", {new_line: true});
+  equal(composer.get('reply'), "before new line\n\nworld hello", "it prepends text with new line to existing text");
+});
+
 test("Title length for regular topics", function() {
   Discourse.SiteSettings.min_topic_title_length = 5;
   Discourse.SiteSettings.max_topic_title_length = 10;