FEATURE: allow selecting a tag when moving posts to a new topic (#6072)

This commit is contained in:
Maja Komel
2018-07-06 18:21:32 +02:00
committed by Régis Hanol
parent 7cba4cbcc6
commit 18f5f646b1
9 changed files with 33 additions and 8 deletions

View File

@ -8,6 +8,8 @@ export default Ember.Controller.extend(ModalFunctionality, {
topicName: null,
saving: false,
categoryId: null,
tags: null,
canAddTags: Ember.computed.alias("site.can_create_tag"),
topicController: Ember.inject.controller("topic"),
selectedPostsCount: Ember.computed.alias(
@ -29,7 +31,8 @@ export default Ember.Controller.extend(ModalFunctionality, {
"modal.modalClass": "split-modal",
saving: false,
categoryId: null,
topicName: ""
topicName: "",
tags: null
});
},
@ -40,7 +43,8 @@ export default Ember.Controller.extend(ModalFunctionality, {
const options = {
title: this.get("topicName"),
post_ids: this.get("topicController.selectedPostIds"),
category_id: this.get("categoryId")
category_id: this.get("categoryId"),
tags: this.get("tags")
};
movePosts(this.get("model.id"), options)

View File

@ -7,6 +7,10 @@
<label>{{i18n 'categories.category'}}</label>
{{category-chooser value=categoryId class="small"}}
{{#if canAddTags}}
<label>{{i18n 'tagging.tags'}}</label>
{{tag-chooser tags=tags filterable=true categoryId=categoryId}}
{{/if}}
</form>
{{/d-modal-body}}

View File

@ -127,6 +127,10 @@
width: 300px;
}
.category-chooser {
margin-bottom: 9px;
}
form {
margin-top: 20px;
#split-topic-name,

View File

@ -55,6 +55,10 @@
margin-right: 10px;
}
.category-chooser {
margin-bottom: 9px;
}
button {
margin-top: 10px;
display: block;

View File

@ -550,6 +550,7 @@ class TopicsController < ApplicationController
post_ids = params.require(:post_ids)
topic_id = params.require(:topic_id)
params.permit(:category_id)
params.permit(:tags)
topic = Topic.with_deleted.find_by(id: topic_id)
guardian.ensure_can_move_posts!(topic)
@ -792,6 +793,7 @@ class TopicsController < ApplicationController
args[:title] = params[:title] if params[:title].present?
args[:destination_topic_id] = params[:destination_topic_id].to_i if params[:destination_topic_id].present?
args[:category_id] = params[:category_id].to_i if params[:category_id].present?
args[:tags] = params[:tags] if params[:tags].present?
topic.move_posts(current_user, post_ids_including_replies, args)
end

View File

@ -19,19 +19,21 @@ class PostMover
end
end
def to_new_topic(title, category_id = nil)
def to_new_topic(title, category_id = nil, tags = nil)
@move_type = PostMover.move_types[:new_topic]
post = Post.find_by(id: post_ids.first)
raise Discourse::InvalidParameters unless post
Topic.transaction do
move_posts_to Topic.create!(
new_topic = Topic.create!(
user: post.user,
title: title,
category_id: category_id,
created_at: post.created_at
)
DiscourseTagging.tag_topic_by_names(new_topic, Guardian.new(user), tags)
move_posts_to new_topic
end
end

View File

@ -915,7 +915,7 @@ class Topic < ActiveRecord::Base
if opts[:destination_topic_id]
post_mover.to_topic opts[:destination_topic_id]
elsif opts[:title]
post_mover.to_new_topic(opts[:title], opts[:category_id])
post_mover.to_new_topic(opts[:title], opts[:category_id], opts[:tags])
end
end

View File

@ -19,7 +19,7 @@ describe PostMover do
end
context 'move_posts' do
let(:user) { Fabricate(:user) }
let(:user) { Fabricate(:user, admin: true) }
let(:another_user) { Fabricate(:evil_trout) }
let(:category) { Fabricate(:category, user: user) }
let!(:topic) { Fabricate(:topic, user: user) }
@ -39,6 +39,7 @@ describe PostMover do
let(:p6) { Fabricate(:post, topic: topic) }
before do
SiteSetting.tagging_enabled = true
SiteSetting.queue_jobs = false
p1.replies << p3
p2.replies << p4
@ -178,7 +179,7 @@ describe PostMover do
it "works correctly" do
topic.expects(:add_moderator_post).once
new_topic = topic.move_posts(user, [p2.id, p4.id], title: "new testing topic name", category_id: category.id)
new_topic = topic.move_posts(user, [p2.id, p4.id], title: "new testing topic name", category_id: category.id, tags: ["tag1", "tag2"])
expect(TopicUser.find_by(user_id: user.id, topic_id: topic.id).last_read_post_number).to eq(p3.post_number)
@ -187,6 +188,7 @@ describe PostMover do
expect(new_topic.like_count).to eq(1)
expect(new_topic.category).to eq(category)
expect(new_topic.tags.pluck(:name)).to eq(["tag1", "tag2"])
expect(topic.featured_user1_id).to be_blank
expect(new_topic.posts.by_post_number).to match_array([p2, p4])

View File

@ -45,6 +45,7 @@ RSpec.describe TopicsController do
describe '#move_posts' do
before do
SiteSetting.min_topic_title_length = 2
SiteSetting.tagging_enabled = true
end
it 'needs you to be logged in' do
@ -101,10 +102,12 @@ RSpec.describe TopicsController do
post "/t/#{topic.id}/move-posts.json", params: {
title: 'Logan is a good movie',
post_ids: [p2.id],
category_id: 123
category_id: 123,
tags: ["tag1", "tag2"]
}
end.to change { Topic.count }.by(1)
expect(Tag.count).to eq(2)
expect(response.status).to eq(200)
result = ::JSON.parse(response.body)