Update the destination category id when a user changes it

This commit is contained in:
Robin Ward 2018-03-23 11:12:22 -04:00
parent 6e4b8901ce
commit 38af67eb73
6 changed files with 60 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import computed from 'ember-addons/ember-computed-decorators'; import computed from 'ember-addons/ember-computed-decorators';
import { ajax } from 'discourse/lib/ajax';
export default Ember.Component.extend({ export default Ember.Component.extend({
tagName: '', tagName: '',
@ -11,8 +12,14 @@ export default Ember.Component.extend({
}, },
actions: { 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 => { bootbox.confirm(I18n.t('shared_drafts.confirm_publish'), result => {
if (result) { if (result) {
this.set('publishing', true); this.set('publishing', true);

View File

@ -6,7 +6,9 @@
<div class='publish-field'> <div class='publish-field'>
<label>{{i18n "shared_drafts.destination_category"}}</label> <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>
<div class='publish-field'> <div class='publish-field'>

View File

@ -98,6 +98,12 @@ export default ComboBoxComponent.extend({
this.appEvents.off("composer:resized"); this.appEvents.off("composer:resized");
}, },
didSelect(computedContentItem) {
if (this.attrs.onChooseCategory) {
this.attrs.onChooseCategory(computedContentItem.originalContent);
}
},
computeContent() { computeContent() {
const categories = Discourse.SiteSettings.fixed_category_positions_on_create ? const categories = Discourse.SiteSettings.fixed_category_positions_on_create ?
Category.list() : Category.list() :

View File

@ -11,6 +11,7 @@ class TopicsController < ApplicationController
:timings, :timings,
:destroy_timings, :destroy_timings,
:update, :update,
:update_shared_draft,
:destroy, :destroy,
:recover, :recover,
:status, :status,
@ -236,6 +237,17 @@ class TopicsController < ApplicationController
render body: nil render body: nil
end end
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 update def update
topic = Topic.find_by(id: params[:topic_id]) topic = Topic.find_by(id: params[:topic_id])
guardian.ensure_can_edit!(topic) guardian.ensure_can_edit!(topic)

View File

@ -599,6 +599,7 @@ Discourse::Application.routes.draw do
put "t/:id/move-to-inbox" => "topics#move_to_inbox" put "t/:id/move-to-inbox" => "topics#move_to_inbox"
put "t/:id/convert-topic/:type" => "topics#convert_topic" put "t/:id/convert-topic/:type" => "topics#convert_topic"
put "t/:id/publish" => "topics#publish" put "t/:id/publish" => "topics#publish"
put "t/:id/shared-draft" => "topics#update_shared_draft"
put "topics/bulk" put "topics/bulk"
put "topics/reset-new" => 'topics#reset_new' put "topics/reset-new" => 'topics#reset_new'
post "topics/timings" post "topics/timings"

View File

@ -464,6 +464,36 @@ RSpec.describe TopicsController do
SiteSetting.shared_drafts_category = shared_drafts_category.id SiteSetting.shared_drafts_category = shared_drafts_category.id
end end
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" do describe "#publish" do
let(:category) { Fabricate(:category) } let(:category) { Fabricate(:category) }
let(:topic) { Fabricate(:topic, category: shared_drafts_category, visible: false) } let(:topic) { Fabricate(:topic, category: shared_drafts_category, visible: false) }