Skip to content

A platform for community discussion. Free, open, simple.

FIX: Remap tag hashtags when a tag is renamed

Régis Hanol94ab7bd10462
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 3fe1e5dfc53..1e2495cdeef 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -72,6 +72,7 @@ class Tag < ActiveRecord::Base
before_save :cook_description
+ after_update :enqueue_tag_hashtag_remap, if: :saved_change_to_name?
after_save :index_search
after_save :update_synonym_associations
@@ -349,6 +350,13 @@ class Tag < ActiveRecord::Base
def name_validator
errors.add(:name, :invalid) if name.present? && RESERVED_TAGS.include?(name.strip.downcase)
end
+
+ def enqueue_tag_hashtag_remap
+ old_ref = name_before_last_save
+ return if old_ref.blank? || old_ref.casecmp?(name)
+
+ HashtagRemapper.enqueue([{ type: TagHashtagDataSource.type, id:, old_ref: }])
+ end
end
# == Schema Information
diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb
index a01a67bfe4d..7118e672414 100644
--- a/spec/models/tag_spec.rb
+++ b/spec/models/tag_spec.rb
@@ -787,4 +787,30 @@ RSpec.describe Tag do
end
end
end
+
+ describe "tag hashtag remapping" do
+ it "enqueues a remap job when the name changes" do
+ tag = Fabricate(:tag, name: "support")
+
+ expect_enqueued_with(
+ job: :remap_hashtag,
+ args: {
+ remaps: [{ type: "tag", id: tag.id, old_ref: "support" }],
+ },
+ ) { tag.update!(name: "help") }
+ end
+
+ it "does not enqueue a remap job when only the casing changes" do
+ SiteSetting.force_lowercase_tags = false
+ tag = Fabricate(:tag, name: "Support")
+
+ expect_not_enqueued_with(job: :remap_hashtag) { tag.update!(name: "support") }
+ end
+
+ it "does not enqueue a remap job for unrelated changes" do
+ tag = Fabricate(:tag, name: "support")
+
+ expect_not_enqueued_with(job: :remap_hashtag) { tag.update!(description: "a description") }
+ end
+ end
end