diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index a8219550c..3fc765f78 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -2,6 +2,12 @@ module Admin class TagsController < Admin::ApplicationController layout "admin" + ALLOWED_PARAMS = %i[ + id supported rules_markdown short_summary pretty_name bg_color_hex + text_color_hex user_id alias_for badge_id requires_approval + category social_preview_template wiki_body_markdown submission_template + ].freeze + before_action :set_default_options, only: %i[index] before_action :badges_for_options, only: %i[new create edit update] after_action only: [:update] do @@ -38,6 +44,7 @@ module Admin def update @tag = Tag.find(params[:id]) if @tag.update(tag_params) + ::Tags::AliasRetagWorker.perform_async(@tag.id) if tag_alias_updated? flash[:success] = "#{@tag.name} tag successfully updated!" else flash[:error] = "The tag update failed: #{@tag.errors_as_sentence}" @@ -57,12 +64,11 @@ module Admin end def tag_params - allowed_params = %i[ - id supported rules_markdown short_summary pretty_name bg_color_hex - text_color_hex user_id alias_for badge_id requires_approval - category social_preview_template wiki_body_markdown submission_template - ] - params.require(:tag).permit(allowed_params) + params.require(:tag).permit(ALLOWED_PARAMS) + end + + def tag_alias_updated? + tag_params[:alias_for].present? end end end diff --git a/app/workers/tags/alias_retag_worker.rb b/app/workers/tags/alias_retag_worker.rb new file mode 100644 index 000000000..ad47898a6 --- /dev/null +++ b/app/workers/tags/alias_retag_worker.rb @@ -0,0 +1,18 @@ +module Tags + class AliasRetagWorker + include Sidekiq::Worker + + sidekiq_options queue: :low_priority, retry: 5 + + def perform(tag_id) + tag = Tag.find_by(id: tag_id) + return unless tag + + tag.taggings.find_each do |tagging| + taggable = tagging.taggable + new_tag_list = ActsAsTaggableOn::TagParser.new(taggable.tag_list).parse + taggable.update(tag_list: new_tag_list) + end + end + end +end diff --git a/spec/requests/admin/tags_spec.rb b/spec/requests/admin/tags_spec.rb index 3539f1ef7..2d050f57a 100644 --- a/spec/requests/admin/tags_spec.rb +++ b/spec/requests/admin/tags_spec.rb @@ -60,5 +60,19 @@ RSpec.describe "/admin/tags", type: :request do expect(tag.bg_color_hex).to eq(params[:bg_color_hex]) expect(tag.text_color_hex).to eq(params[:text_color_hex]) end + + it "updates posts when making a tag an alias for another tag" do + create(:tag, name: "rails") + article1 = create(:article, tags: "ruby, ror") + article2 = create(:article, tags: "ror, webdev") + alias_tag = Tag.find_by(name: "ror") + + sidekiq_perform_enqueued_jobs do + put admin_tag_path(alias_tag), params: { tag: { alias_for: "rails" } } + end + + expect(article1.reload.cached_tag_list).to eq "ruby, rails" + expect(article2.reload.cached_tag_list).to eq "rails, webdev" + end end end