A platform for community discussion. Free, open, simple.
Update the destination category id when a user changes it
Robin Ward38af67eb738a
diff --git a/app/assets/javascripts/discourse/components/shared-draft-controls.js.es6 b/app/assets/javascripts/discourse/components/shared-draft-controls.js.es6index a2b11f460b2..a9f35cb8ab2 100644--- a/app/assets/javascripts/discourse/components/shared-draft-controls.js.es6+++ b/app/assets/javascripts/discourse/components/shared-draft-controls.js.es6@@ -1,4 +1,5 @@import computed from 'ember-addons/ember-computed-decorators';+import { ajax } from 'discourse/lib/ajax';export default Ember.Component.extend({tagName: '',@@ -11,8 +12,14 @@ export default Ember.Component.extend({},actions: {- publish() {+ updateDestinationCategory(category) {+ ajax(`/t/${this.get('topic.id')}/shared-draft`, {+ method: 'PUT',+ data: { category_id: category.get('id') }+ });+ },+ publish() {bootbox.confirm(I18n.t('shared_drafts.confirm_publish'), result => {if (result) {this.set('publishing', true);diff --git a/app/assets/javascripts/discourse/templates/components/shared-draft-controls.hbs b/app/assets/javascripts/discourse/templates/components/shared-draft-controls.hbsindex 056d9d5af54..feeed4be439 100644--- a/app/assets/javascripts/discourse/templates/components/shared-draft-controls.hbs+++ b/app/assets/javascripts/discourse/templates/components/shared-draft-controls.hbs@@ -6,7 +6,9 @@<div class='publish-field'><label>{{i18n "shared_drafts.destination_category"}}</label>- {{category-chooser value=topic.destination_category_id}}+ {{category-chooser+ value=topic.destination_category_id+ onChooseCategory=(action "updateDestinationCategory")}}</div><div class='publish-field'>diff --git a/app/assets/javascripts/select-kit/components/category-chooser.js.es6 b/app/assets/javascripts/select-kit/components/category-chooser.js.es6index 069ba1a3299..b1e4fedc73a 100644--- a/app/assets/javascripts/select-kit/components/category-chooser.js.es6+++ b/app/assets/javascripts/select-kit/components/category-chooser.js.es6@@ -98,6 +98,12 @@ export default ComboBoxComponent.extend({this.appEvents.off("composer:resized");},+ didSelect(computedContentItem) {+ if (this.attrs.onChooseCategory) {+ this.attrs.onChooseCategory(computedContentItem.originalContent);+ }+ },+computeContent() {const categories = Discourse.SiteSettings.fixed_category_positions_on_create ?Category.list() :diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 457dcaf7220..af3f12c5483 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -11,6 +11,7 @@ class TopicsController < ApplicationController:timings,:destroy_timings,:update,+ :update_shared_draft,:destroy,:recover,:status,@@ -236,6 +237,17 @@ class TopicsController < ApplicationControllerrender body: nilend+ def update_shared_draft+ topic = Topic.find_by(id: params[:id])+ guardian.ensure_can_edit!(topic)+ guardian.ensure_can_create_shared_draft!+ raise Discourse::NotFound unless topic.shared_draft.present?++ SharedDraft.where(topic_id: topic.id).update_all(category_id: params[:category_id].to_i)++ render json: success_json+ end+def updatetopic = Topic.find_by(id: params[:topic_id])guardian.ensure_can_edit!(topic)diff --git a/config/routes.rb b/config/routes.rbindex 587e93ab7d9..a5249b598db 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -599,6 +599,7 @@ Discourse::Application.routes.draw doput "t/:id/move-to-inbox" => "topics#move_to_inbox"put "t/:id/convert-topic/:type" => "topics#convert_topic"put "t/:id/publish" => "topics#publish"+ put "t/:id/shared-draft" => "topics#update_shared_draft"put "topics/bulk"put "topics/reset-new" => 'topics#reset_new'post "topics/timings"diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 646a926e3af..b70c38aeed8 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -464,6 +464,36 @@ RSpec.describe TopicsController doSiteSetting.shared_drafts_category = shared_drafts_category.idend+ describe "#update_shared_draft" do+ let(:category) { Fabricate(:category) }+ let(:other_cat) { Fabricate(:category) }+ let(:topic) { Fabricate(:topic, category: shared_drafts_category, visible: false) }+ let!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) }+ let(:moderator) { Fabricate(:moderator) }++ context "anonymous" do+ it "doesn't allow staff to update the shared draft" do+ put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id }+ expect(response.code.to_i).to eq(403)+ topic.reload+ expect(topic.shared_draft.category_id).to eq(category.id)+ end+ end++ context "as a moderator" do+ before do+ sign_in(moderator)+ end++ it "allows staff to update the category id" do+ put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id }+ expect(response).to be_success+ topic.reload+ expect(topic.shared_draft.category_id).to eq(other_cat.id)+ end+ end+ end+describe "#publish" dolet(:category) { Fabricate(:category) }let(:topic) { Fabricate(:topic, category: shared_drafts_category, visible: false) }