Fix tag alias problems (#12480)

This commit is contained in:
Michael Kohl 2021-02-03 23:58:59 +07:00 committed by GitHub
parent 2e6397d319
commit 60813703e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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