A platform for community discussion. Free, open, simple.
FIX: Watched word tag action broken after TagChooser refactor (#38245)
Natalie Tay375779df7bb6
diff --git a/app/controllers/admin/watched_words_controller.rb b/app/controllers/admin/watched_words_controller.rbindex 797829adeb8..66c90464236 100644--- a/app/controllers/admin/watched_words_controller.rb+++ b/app/controllers/admin/watched_words_controller.rb@@ -17,6 +17,10 @@ class Admin::WatchedWordsController < Admin::StaffControlleraction = WatchedWord.actions[opts[:action_key].to_sym]words = opts.delete(:words)+ if action == WatchedWord.actions[:tag] && watched_words_params[:replacement_tags].present?+ opts = opts.merge(replacement: resolve_replacement_tags)+ end+watched_word_group = WatchedWordGroup.new(action: action)watched_word_group.create_or_update_members(words, opts)@@ -125,6 +129,32 @@ class Admin::WatchedWordsController < Admin::StaffControllerdef watched_words_params@watched_words_params ||=- params.permit(:id, :replacement, :action_key, :case_sensitive, :html, words: [])+ params.permit(+ :id,+ :replacement,+ :action_key,+ :case_sensitive,+ :html,+ words: [],+ replacement_tags: %i[id name],+ )+ end++ def resolve_replacement_tags+ tags_param = watched_words_params[:replacement_tags]+ tags_param = tags_param.values if tags_param.is_a?(ActionController::Parameters)+ return if tags_param.blank?++ tag_ids = tags_param.filter_map { |t| t[:id]&.to_i }+ new_tag_names = tags_param.select { |t| t[:id].blank? }.filter_map { |t| t[:name] }++ tag_names = Tag.where(id: tag_ids).pluck(:name)++ if new_tag_names.present?+ new_tags = DiscourseTagging.find_or_create_tags!(new_tag_names, guardian)+ tag_names.concat(new_tags.map(&:name))+ end++ tag_names.join(",")endenddiff --git a/frontend/discourse/admin/components/watched-word-form.gjs b/frontend/discourse/admin/components/watched-word-form.gjsindex 9142f815ca7..8085b8e8439 100644--- a/frontend/discourse/admin/components/watched-word-form.gjs+++ b/frontend/discourse/admin/components/watched-word-form.gjs@@ -76,7 +76,9 @@ export default class WatchedWordForm extends Component {changeSelectedTags(tags) {this.setProperties({selectedTags: tags,- replacement: tags.join(","),+ replacementTags: tags.map((t) =>+ typeof t.id === "number" ? { id: t.id, name: t.name } : { name: t.name }+ ),});}@@ -95,10 +97,8 @@ export default class WatchedWordForm extends Component {const watchedWord = WatchedWord.create({words: this.words,- replacement:- this.canReplace || this.canTag || this.canLink- ? this.replacement- : null,+ replacement: this.canReplace || this.canLink ? this.replacement : null,+ replacementTags: this.canTag ? this.replacementTags : null,action: this.actionKey,isCaseSensitive: this.isCaseSensitive,isHtml: this.isHtml,@@ -110,6 +110,7 @@ export default class WatchedWordForm extends Component {this.setProperties({words: [],replacement: "",+ replacementTags: [],selectedTags: [],showMessage: true,message: i18n("admin.watched_words.form.success"),diff --git a/frontend/discourse/admin/models/watched-word.js b/frontend/discourse/admin/models/watched-word.jsindex 4862423aeb5..85089786c9c 100644--- a/frontend/discourse/admin/models/watched-word.js+++ b/frontend/discourse/admin/models/watched-word.js@@ -27,19 +27,22 @@ export default class WatchedWord extends EmberObject {}save() {+ const data = {+ words: this.words,+ replacement: this.replacement,+ action_key: this.action,+ case_sensitive: this.isCaseSensitive,+ html: this.isHtml,+ replacement_tags: this.replacementTags,+ };+return ajax("/admin/customize/watched_words" +(this.id ? "/" + this.id : "") +".json",{type: this.id ? "PUT" : "POST",- data: {- words: this.words,- replacement: this.replacement,- action_key: this.action,- case_sensitive: this.isCaseSensitive,- html: this.isHtml,- },+ data,dataType: "json",});diff --git a/spec/requests/admin/watched_words_controller_spec.rb b/spec/requests/admin/watched_words_controller_spec.rbindex 56a768af9ce..7f82068a9c5 100644--- a/spec/requests/admin/watched_words_controller_spec.rb+++ b/spec/requests/admin/watched_words_controller_spec.rb@@ -146,6 +146,62 @@ RSpec.describe Admin::WatchedWordsController doexpect(WatchedWord.take.case_sensitive?).to eq(true)expect(WatchedWord.take.word).to eq("PNG")end++ it "creates a tag watched word with existing tags by id" do+ tag = Fabricate(:tag, name: "greeting")++ post "/admin/customize/watched_words.json",+ params: {+ action_key: "tag",+ words: ["hello"],+ replacement_tags: [{ id: tag.id, name: tag.name }],+ }++ expect(response.status).to eq(200)+ expect(WatchedWord.last.replacement).to eq("greeting")+ end++ it "creates a tag watched word and creates new tags" do+ post "/admin/customize/watched_words.json",+ params: {+ action_key: "tag",+ words: ["hello"],+ replacement_tags: [{ name: "brandnewtag" }],+ }++ expect(response.status).to eq(200)+ expect(Tag.find_by(name: "brandnewtag")).to be_present+ expect(WatchedWord.last.replacement).to eq("brandnewtag")+ end++ it "creates a tag watched word with a mix of existing and new tags" do+ tag = Fabricate(:tag, name: "existing")++ post "/admin/customize/watched_words.json",+ params: {+ action_key: "tag",+ words: ["hello"],+ replacement_tags: [{ id: tag.id, name: tag.name }, { name: "newone" }],+ }++ expect(response.status).to eq(200)+ expect(Tag.find_by(name: "newone")).to be_present+ expect(WatchedWord.last.replacement).to eq("existing,newone")+ end++ it "creates a tag watched word via the legacy replacement param" do+ Fabricate(:tag, name: "greeting")++ post "/admin/customize/watched_words.json",+ params: {+ action_key: "tag",+ words: ["hello"],+ replacement: "greeting",+ }++ expect(response.status).to eq(200)+ expect(WatchedWord.last.replacement).to eq("greeting")+ endendenddiff --git a/spec/system/admin_watched_words_spec.rb b/spec/system/admin_watched_words_spec.rbindex 48da7633ade..dcdf27c2252 100644--- a/spec/system/admin_watched_words_spec.rb+++ b/spec/system/admin_watched_words_spec.rb@@ -32,4 +32,18 @@ describe "Admin Watched Words", type: :system doww_page.visit(action: "replace")expect(ww_page).to have_text(I18n.t("admin_js.admin.watched_words.form.html_description"))end++ it "creates a watched word with the tag action type" do+ Fabricate(:tag, name: "greeting")++ ww_page.visit(action: "tag")+ ww_page.add_word_with_tag("hello", "greeting")++ expect(ww_page).to have_word++ watched_word = WatchedWord.find_by(word: "hello")+ expect(watched_word).to be_present+ expect(watched_word.action).to eq(WatchedWord.actions[:tag])+ expect(watched_word.replacement).to eq("greeting")+ endenddiff --git a/spec/system/page_objects/pages/admin_watched_words.rb b/spec/system/page_objects/pages/admin_watched_words.rbindex c30c0ab735f..af710318cb1 100644--- a/spec/system/page_objects/pages/admin_watched_words.rb+++ b/spec/system/page_objects/pages/admin_watched_words.rb@@ -21,6 +21,21 @@ module PageObjectshas_css?(".watched-words-detail .show-words-checkbox")end+ def add_word_with_tag(word, tag_name)+ words_selector = PageObjects::Components::SelectKit.new("#watched-words")+ words_selector.expand+ words_selector.search(word)+ words_selector.select_row_by_name(word)+ words_selector.collapse++ tag_chooser = PageObjects::Components::SelectKit.new(".tag-chooser")+ tag_chooser.expand+ tag_chooser.search(tag_name)+ tag_chooser.select_row_by_name(tag_name)++ page.find(".watched-words-detail .btn-primary").click+ end+def has_error?(error)has_css?(".dialog-container .dialog-body", text: error)end