From e20104919af0f54ec07d9567f8fc5800170c6553 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 8 Mar 2017 15:21:41 +0000 Subject: [PATCH 1/3] UX: Show configurable length excerpt when quoting the solution --- .../initializers/extend-for-solved-button.js.es6 | 7 +++++-- config/locales/server.en.yml | 1 + config/settings.yml | 4 ++++ plugin.rb | 11 ++++++----- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 b/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 index 300482a..2e1fd44 100644 --- a/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 +++ b/assets/javascripts/discourse/initializers/extend-for-solved-button.js.es6 @@ -47,7 +47,8 @@ function acceptPost(post) { topic.set('accepted_answer', { username: post.get('username'), - post_number: post.get('post_number') + post_number: post.get('post_number'), + excerpt: post.get('cooked'), }); ajax("/solution/accept", { @@ -160,7 +161,9 @@ function initializeWithApi(api) {
${topic.get('acceptedAnswerHtml')}
<\/div>
-
+
+ ${topic.get('accepted_answer').excerpt} +
` var cooked = new PostCooked({cooked:rawhtml}); diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 3ea8b27..d8b809b 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -4,6 +4,7 @@ en: allow_solved_on_all_topics: "Allow users to select solutions on all topics (by default you control this by editing categories)" accept_all_solutions_trust_level: "Minimum trust level required to accept solutions on any topic (even when not OP)" empty_box_on_unsolved: "Display an empty box next to unsolved topics" + solved_quote_length: "Number of characters to quote when displaying the solution under the first post" reports: accepted_solutions: title: "Accepted solutions" diff --git a/config/settings.yml b/config/settings.yml index fb42003..5f5f51e 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -11,3 +11,7 @@ plugins: empty_box_on_unsolved: default: false client: true + solved_quote_length: + default: 100 + client: false + diff --git a/plugin.rb b/plugin.rb index b8c4915..615280b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -224,17 +224,18 @@ SQL { post_number: info[0], username: info[1], + excerpt: info[2] } end end def accepted_answer_post_info # TODO: we may already have it in the stream ... so bypass query here - - Post.where(id: accepted_answer_post_id, topic_id: object.topic.id) - .joins(:user) - .pluck('post_number, username') - .first + post = Post.where(id: accepted_answer_post_id, topic_id: object.topic.id).joins(:user).first + excerpt = post.excerpt + + return [post.post_number, post.username, post.excerpt(SiteSetting.solved_quote_length)] + end def accepted_answer_post_id From 8b03c459f8e26fed86a5cd53ad3985829bb96ba4 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 8 Mar 2017 15:42:41 +0000 Subject: [PATCH 2/3] Only select required columns, and don't call excerpt twice --- plugin.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plugin.rb b/plugin.rb index 615280b..d923257 100644 --- a/plugin.rb +++ b/plugin.rb @@ -231,10 +231,14 @@ SQL def accepted_answer_post_info # TODO: we may already have it in the stream ... so bypass query here - post = Post.where(id: accepted_answer_post_id, topic_id: object.topic.id).joins(:user).first - excerpt = post.excerpt + postInfo = Post.where(id: accepted_answer_post_id, topic_id: object.topic.id) + .joins(:user) + .pluck('post_number', 'username', 'cooked') + .first - return [post.post_number, post.username, post.excerpt(SiteSetting.solved_quote_length)] + postInfo[2] = PrettyText.excerpt(postInfo[2], SiteSetting.solved_quote_length) + + return postInfo end From f66da5be153ec75edb3f0770032204b00f8ec7cc Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 8 Mar 2017 15:47:18 +0000 Subject: [PATCH 3/3] Check postInfo exists --- plugin.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin.rb b/plugin.rb index d923257..6f1c8a1 100644 --- a/plugin.rb +++ b/plugin.rb @@ -235,11 +235,11 @@ SQL .joins(:user) .pluck('post_number', 'username', 'cooked') .first - - postInfo[2] = PrettyText.excerpt(postInfo[2], SiteSetting.solved_quote_length) - - return postInfo - + + if postInfo + postInfo[2] = PrettyText.excerpt(postInfo[2], SiteSetting.solved_quote_length) + return postInfo + end end def accepted_answer_post_id