Skip to content

Commit

Permalink
FEATURE: bulk remove tags (#10831)
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitjalan committed Oct 6, 2020
1 parent 340d979 commit f4c7c7b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ addBulkButton("showAppendTagTopics", "append_tags", {
class: "btn-default",
enabledSetting: "tagging_enabled",
});
addBulkButton("removeTags", "remove_tags", {
icon: "tag",
class: "btn-danger",
});
addBulkButton("deleteTopics", "delete", {
icon: "trash-alt",
class: "btn-danger",
Expand Down Expand Up @@ -201,6 +205,10 @@ export default Controller.extend(ModalFunctionality, {
resetRead() {
this.performAndRefresh({ type: "reset_read" });
},

removeTags() {
this.performAndRefresh({ type: "remove_tags" });
},
},
});

Expand Down
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,7 @@ en:
choose_new_tags: "Choose new tags for these topics:"
choose_append_tags: "Choose new tags to append for these topics:"
changed_tags: "The tags of those topics were changed."
remove_tags: "Remove Tags"

none:
unread: "You have no unread topics."
Expand Down
12 changes: 11 additions & 1 deletion lib/topics_bulk_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def initialize(user, topic_ids, operation, options = {})
def self.operations
@operations ||= %w(change_category close archive change_notification_level
reset_read dismiss_posts delete unlist archive_messages
move_messages_to_inbox change_tags append_tags relist)
move_messages_to_inbox change_tags append_tags remove_tags
relist)
end

def self.register_operation(name, &block)
Expand Down Expand Up @@ -176,6 +177,15 @@ def append_tags
end
end

def remove_tags
topics.each do |t|
if guardian.can_edit?(t)
TopicTag.where(topic_id: t.id).delete_all
@changed_ids << t.id
end
end
end

def guardian
@guardian ||= Guardian.new(@user)
end
Expand Down
34 changes: 34 additions & 0 deletions spec/components/topics_bulk_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,38 @@
end
end
end

describe "remove_tags" do
fab!(:tag1) { Fabricate(:tag) }
fab!(:tag2) { Fabricate(:tag) }

before do
SiteSetting.tagging_enabled = true
SiteSetting.min_trust_level_to_tag_topics = 0
topic.tags = [tag1, tag2]
end

it "can remove all tags" do
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'remove_tags')
topic_ids = tba.perform!
expect(topic_ids).to eq([topic.id])
topic.reload
expect(topic.tags.size).to eq(0)
end

context "when user can't edit topic" do
before do
Guardian.any_instance.expects(:can_edit?).returns(false)
end

it "doesn't remove the tags" do
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'remove_tags')
topic_ids = tba.perform!
expect(topic_ids).to eq([])
topic.reload
expect(topic.tags.map(&:name)).to contain_exactly(tag1.name, tag2.name)
end
end
end

end

1 comment on commit f4c7c7b

@discoursebot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/bulk-actions-remove-tags-in-addition-to-change-tags/52145/8

Please sign in to comment.