A platform for community discussion. Free, open, simple.
FIX: allow new tags to be created from the Tag Group editor (#37594)
Alan Guo Xiang Tan8750ae2e186d
diff --git a/app/controllers/tag_groups_controller.rb b/app/controllers/tag_groups_controller.rbindex 497e2683d32..2b6253836a3 100644--- a/app/controllers/tag_groups_controller.rb+++ b/app/controllers/tag_groups_controller.rb@@ -135,7 +135,18 @@ class TagGroupsController < ApplicationController)if result[:tags].present?- result[:tag_ids] = result[:tags].map { |t| t["id"] }+ result[:tag_ids] = result[:tags].filter_map { |t| t["id"] }++ new_tag_names = result[:tags].filter_map { |t| t["name"] if t["id"].blank? }++ if new_tag_names.present?+ result[:tag_ids].concat(+ DiscourseTagging.find_or_create_tags!(+ new_tag_names,+ Guardian.new(Discourse.system_user),+ ).map(&:id),+ )+ endelsif result[:tag_names].present?Discourse.deprecate("the tag_names param is deprecated, use tags instead",@@ -148,7 +159,19 @@ class TagGroupsController < ApplicationControllerresult.delete(:tags)if result[:parent_tag].present?- result[:parent_tag_id] = result[:parent_tag].first&.dig("id")+ parent = result[:parent_tag].first++ if parent&.dig("id").present?+ result[:parent_tag_id] = parent["id"]+ elsif parent&.dig("name").present?+ tag =+ DiscourseTagging.find_or_create_tags!(+ [parent["name"]],+ Guardian.new(Discourse.system_user),+ ).first++ result[:parent_tag_id] = tag.id if tag+ endelsif result[:parent_tag_name].present?Discourse.deprecate("the parent_tag_name param is deprecated, use parent_tag instead",diff --git a/frontend/discourse/app/components/tag-groups-form.gjs b/frontend/discourse/app/components/tag-groups-form.gjsindex 610e28fc2eb..0b5799fdcba 100644--- a/frontend/discourse/app/components/tag-groups-form.gjs+++ b/frontend/discourse/app/components/tag-groups-form.gjs@@ -78,6 +78,14 @@ export default class TagGroupsForm extends Component {"permissions");+ if (attrs.tags) {+ attrs.tags = attrs.tags.map(this.#serializeTag);+ }++ if (attrs.parent_tag) {+ attrs.parent_tag = attrs.parent_tag.map(this.#serializeTag);+ }+if (isEmpty(attrs.name)) {this.dialog.alert("tagging.groups.cannot_save.empty_name");return false;@@ -106,6 +114,12 @@ export default class TagGroupsForm extends Component {this.model.save(attrs).then(() => this.onSave?.());}+ #serializeTag(t) {+ return typeof t.id === "number"+ ? { id: t.id, name: t.name }+ : { name: t.name };+ }+@actiondestroyTagGroup() {return this.dialog.yesNoConfirm({diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex 1da2bde0058..1e1a81570ef 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -809,6 +809,16 @@ module DiscourseTaggingDiscoursePluginRegistry.apply_modifier(:tags_for_saving, saving_tags, tag_names, guardian, opts)end+ def self.find_or_create_tags!(tag_names, guardian)+ tag_names = tags_for_saving(tag_names, guardian) || []+ return [] if tag_names.empty?++ existing_tag_names = Tag.where_name(tag_names).pluck(:name)+ new_tag_names = tag_names - existing_tag_names+ new_tag_names.each { |name| Tag.create!(name: name) }+ Tag.where_name(tag_names).all+ end+def self.add_or_create_tags_by_name(taggable, tag_names_arg, opts = {})tag_names =DiscourseTagging.tags_for_saving(tag_names_arg, Guardian.new(Discourse.system_user), opts) ||diff --git a/spec/requests/tag_groups_controller_spec.rb b/spec/requests/tag_groups_controller_spec.rbindex 02c7efa06ac..7a49495c162 100644--- a/spec/requests/tag_groups_controller_spec.rb+++ b/spec/requests/tag_groups_controller_spec.rb@@ -207,6 +207,43 @@ RSpec.describe TagGroupsController do)expect(TagGroup.last.parent_tag).to eq(parent_tag)end++ it "creates a tag group with a mix of existing and brand-new tags" do+ post "/tag_groups.json",+ params: {+ tag_group: {+ name: "group_with_new_tags",+ tags: [{ id: tag1.id, name: tag1.name }, { name: "brand-new-tag" }],+ },+ }++ expect(response.status).to eq(200)++ created_group = TagGroup.find(response.parsed_body["tag_group"]["id"])+ new_tag = Tag.find_by(name: "brand-new-tag")++ expect(new_tag).to be_present+ expect(created_group.tags).to contain_exactly(tag1, new_tag)+ end++ it "creates a tag group with a brand-new parent tag" do+ post "/tag_groups.json",+ params: {+ tag_group: {+ name: "group_with_new_parent",+ tags: [{ id: tag1.id, name: tag1.name }],+ parent_tag: [{ name: "new-parent-tag" }],+ },+ }++ expect(response.status).to eq(200)++ created_group = TagGroup.find(response.parsed_body["tag_group"]["id"])+ new_parent = Tag.find_by(name: "new-parent-tag")++ expect(new_parent).to be_present+ expect(created_group.parent_tag).to eq(new_parent)+ endenddescribe "#delete" do@@ -287,5 +324,37 @@ RSpec.describe TagGroupsController do)expect(tag_group.reload.parent_tag).to eq(parent_tag)end++ it "adds a brand-new tag to an existing tag group" do+ put "/tag_groups/#{tag_group.id}.json",+ params: {+ tag_group: {+ tags: [{ id: tag1.id, name: tag1.name }, { name: "another-new-tag" }],+ },+ }++ expect(response.status).to eq(200)++ new_tag = Tag.find_by(name: "another-new-tag")++ expect(new_tag).to be_present+ expect(tag_group.reload.tags).to contain_exactly(tag1, new_tag)+ end++ it "updates a tag group with a brand-new parent tag" do+ put "/tag_groups/#{tag_group.id}.json",+ params: {+ tag_group: {+ parent_tag: [{ name: "new-update-parent" }],+ },+ }++ expect(response.status).to eq(200)++ new_parent = Tag.find_by(name: "new-update-parent")++ expect(new_parent).to be_present+ expect(tag_group.reload.parent_tag).to eq(new_parent)+ endendenddiff --git a/spec/system/tag_group_spec.rb b/spec/system/tag_group_spec.rbindex 3fcd626fb28..152d8994c8b 100644--- a/spec/system/tag_group_spec.rb+++ b/spec/system/tag_group_spec.rb@@ -37,7 +37,7 @@ describe "Tag Groups", type: :system dodescribe "creating a new tag group" dofab!(:existing_tag_group) { Fabricate(:tag_group, name: "Existing Group") }- it "can create a tag group and see it after refresh" do+ it "can create a tag group with existing and brand-new tags" dotag_groups_page.visitexpect(tag_groups_page).to have_tag_group_in_sidebar("Existing Group")@@ -48,8 +48,10 @@ describe "Tag Groups", type: :system dotag_groups_page.tags_chooser.expandtag_groups_page.tags_chooser.search("cats")tag_groups_page.tags_chooser.select_row_by_name("cats")- tag_groups_page.tags_chooser.search("bats")- tag_groups_page.tags_chooser.select_row_by_name("bats")+ tag_groups_page.tags_chooser.search("new-tag-one")+ tag_groups_page.tags_chooser.select_row_by_value("new-tag-one")+ tag_groups_page.tags_chooser.search("new-tag-two")+ tag_groups_page.tags_chooser.select_row_by_value("new-tag-two")tag_groups_page.tags_chooser.collapsetag_groups_page.parent_tag_chooser.expand@@ -68,7 +70,8 @@ describe "Tag Groups", type: :system dotag_groups_page.click_tag_group("New Test Group")expect(tag_groups_page).to have_tag_in_group("cats")- expect(tag_groups_page).to have_tag_in_group("bats")+ expect(tag_groups_page).to have_tag_in_group("new-tag-one")+ expect(tag_groups_page).to have_tag_in_group("new-tag-two")expect(page).to have_css(".parent-tag-section .tag-chooser", text: "parent-tag")expect(tag_groups_page).to have_visible_permission_checkedend@@ -78,7 +81,7 @@ describe "Tag Groups", type: :system dofab!(:tag3) { Fabricate(:tag, name: "rats") }fab!(:tag_group) { Fabricate(:tag_group, name: "Animals", tags: [tag1, tag2]) }- it "can edit a tag group's tags and permissions" do+ it "can swap existing tags and add brand-new tags" dotag_groups_page.visittag_groups_page.click_tag_group("Animals")@@ -89,6 +92,10 @@ describe "Tag Groups", type: :system dotag_groups_page.tags_chooser.unselect_by_name("bats")tag_groups_page.tags_chooser.search("rats")tag_groups_page.tags_chooser.select_row_by_name("rats")+ tag_groups_page.tags_chooser.search("new-animal-one")+ tag_groups_page.tags_chooser.select_row_by_value("new-animal-one")+ tag_groups_page.tags_chooser.search("new-animal-two")+ tag_groups_page.tags_chooser.select_row_by_value("new-animal-two")tag_groups_page.tags_chooser.collapsetag_groups_page.select_visible_permission@@ -100,6 +107,8 @@ describe "Tag Groups", type: :system doexpect(tag_groups_page).to have_tag_in_group("cats")expect(tag_groups_page).to have_tag_in_group("rats")expect(tag_groups_page).to have_no_tag_in_group("bats")+ expect(tag_groups_page).to have_tag_in_group("new-animal-one")+ expect(tag_groups_page).to have_tag_in_group("new-animal-two")expect(tag_groups_page).to have_visible_permission_checkedendend