mirror of
https://github.com/discourse/discourse.git
synced 2025-03-22 19:35:31 +08:00
DEV: Simplify client and server side code to support removing tags.
Follow up to 834c86678fc9b0900d8ce83365068c41bc34f63f.
This commit is contained in:
parent
678a9a61c4
commit
148bfc9be5
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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'])
|
||||
|
@ -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!(
|
||||
|
Loading…
x
Reference in New Issue
Block a user