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

approved
#1

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

Follow up to 834c86678fc9b0900d8ce83365068c41bc34f63f.

diff --git a/app/assets/javascripts/discourse/models/topic.js.es6 b/app/assets/javascripts/discourse/models/topic.js.es6
index e769d7e..1f54f1c 100644
--- a/app/assets/javascripts/discourse/models/topic.js.es6
+++ b/app/assets/javascripts/discourse/models/topic.js.es6
@@ -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
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 98e9147..f578291 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -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
 
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index 1f94183..6189a81 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -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)
diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb
index 7f18ac2..1cd9dde 100644
--- a/spec/components/post_revisor_spec.rb
+++ b/spec/components/post_revisor_spec.rb
@@ -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'])
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index e30cca3..2964a91 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -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!(

GitHub sha: 148bfc9b

1 Like
FIX: Missing post revision when editing the first post.
Approved #2