DEV: Simplify client and server side code to support removing tags.

Follow up to 834c86678fc9b0900d8ce83365068c41bc34f63f.
This commit is contained in:
Guo Xiang Tan 2019-05-17 16:26:00 +08:00
parent 678a9a61c4
commit 148bfc9be5
5 changed files with 47 additions and 31 deletions

View File

@ -652,15 +652,9 @@ Topic.reopenClass({
delete props.category_id;
}
// Annoyingly, empty arrays are not sent across the wire. This
// allows us to make a distinction between arrays that were not
// sent and arrays that we specifically want to be empty.
Object.keys(props).forEach(function(k) {
const v = props[k];
if (v instanceof Array && v.length === 0) {
props[`${k}_empty_array`] = true;
}
});
if (props.tags && props.tags.length === 0) {
props.tags = [""];
}
return ajax(topic.get("url"), { type: "PUT", data: props }).then(result => {
// The title can be cleaned up server side

View File

@ -324,13 +324,13 @@ class TopicsController < ApplicationController
end
changes = {}
PostRevisor.tracked_topic_fields.each_key do |f|
changes[f] = params[f] if params.has_key?(f)
end
changes.delete(:title) if topic.title == changes[:title]
changes.delete(:category_id) if topic.category_id.to_i == changes[:category_id].to_i
changes.delete(:tags_empty_array) if !topic.tags.exists?
success = true

View File

@ -101,17 +101,6 @@ class PostRevisor
end
end
track_topic_field(:tags_empty_array) do |tc, val|
if val.present? && tc.guardian.can_tag_topics?
prev_tags = tc.topic.tags.map(&:name)
if !DiscourseTagging.tag_topic_by_names(tc.topic, tc.guardian, [])
tc.check_result(false)
next
end
tc.record_change('tags', prev_tags, nil)
end
end
track_topic_field(:featured_link) do |topic_changes, featured_link|
if SiteSetting.topic_featured_link_enabled &&
topic_changes.guardian.can_edit_featured_link?(topic_changes.topic.category_id)

View File

@ -677,14 +677,6 @@ describe PostRevisor do
expect(post.topic.tags.size).to eq(0)
end
it "can remove all tags using tags_empty_array" do
topic.tags = [Fabricate(:tag, name: "stuff")]
result = subject.revise!(user, raw: "lets totally update the body", tags_empty_array: "true")
expect(result).to eq(true)
post.reload
expect(post.topic.tags.size).to eq(0)
end
it "can't add staff-only tags" do
create_staff_tags(['important'])
result = subject.revise!(user, raw: "lets totally update the body", tags: ['important', 'stuff'])

View File

@ -1002,14 +1002,55 @@ RSpec.describe TopicsController do
it "doesn't call the PostRevisor when there is no changes" do
expect do
put "/t/#{topic.slug}/#{topic.id}.json", params: {
category_id: topic.category_id,
tags_empty_array: true
category_id: topic.category_id
}
end.not_to change(PostRevision.all, :count)
expect(response.status).to eq(200)
end
context 'tags' do
fab!(:tag) { Fabricate(:tag) }
before do
SiteSetting.tagging_enabled = true
end
it "can add a tag to topic" do
expect do
put "/t/#{topic.slug}/#{topic.id}.json", params: {
tags: [tag.name]
}
end.to change { topic.reload.first_post.revisions.count }.by(1)
expect(response.status).to eq(200)
expect(topic.tags.pluck(:id)).to contain_exactly(tag.id)
end
it 'does not remove tag if no params is given' do
topic.tags << tag
expect do
put "/t/#{topic.slug}/#{topic.id}.json"
end.to_not change { topic.reload.tags.count }
expect(response.status).to eq(200)
end
it 'can remove a tag' do
topic.tags << tag
expect do
put "/t/#{topic.slug}/#{topic.id}.json", params: {
tags: [""]
}
end.to change { topic.reload.first_post.revisions.count }.by(1)
expect(response.status).to eq(200)
expect(topic.tags).to eq([])
end
end
context 'when topic is private' do
before do
topic.update!(