Automatically mark tag supported when adding mod (#11151)

* Automatically mark tag a supported when adding mod

* Add data update script
This commit is contained in:
Michael Kohl 2020-10-30 20:46:10 +07:00 committed by GitHub
parent caad342033
commit 32f2b2d58f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 1 deletions

View file

@ -17,6 +17,7 @@ module AssignTagModerator
add_tag_mod_role(user, tag)
add_trusted_role(user)
add_to_chat_channels(user, tag)
tag.update(supported: true) unless tag.supported?
NotifyMailer.with(user: user, tag: tag, channel_slug: chat_channel_slug(tag))
.tag_moderator_confirmation_email

View file

@ -0,0 +1,25 @@
module DataUpdateScripts
class MakeTagsWithModsSupported
def run
ActiveRecord::Base.connection.execute(<<~SQL.squish)
WITH unsupported_tags_with_mods_ids AS (
SELECT
tags.id
FROM
tags
JOIN roles ON (roles.name = 'tag_moderator'
AND resource_id = tags.id)
WHERE
supported = FALSE)
UPDATE
tags
SET
supported = TRUE
FROM
unsupported_tags_with_mods_ids AS cte
WHERE
tags.id = cte.id;
SQL
end
end
end

View file

@ -5,7 +5,7 @@ RSpec.describe AssignTagModerator, type: :labor do
let(:user_two) { create(:user) }
let(:mod_relator) { create(:user) }
let(:tag_one) { create(:tag) }
let(:tag_two) { create(:tag) }
let(:tag_two) { create(:tag, supported: false) }
let!(:channel) do
create(:chat_channel,
slug: "tag-moderators",
@ -67,4 +67,10 @@ RSpec.describe AssignTagModerator, type: :labor do
expect { add_tag_moderators }.not_to raise_error(NoMethodError)
end
end
it "marks tags as supported if they aren't already" do
expect do
add_tag_moderators
end.to change { tag_two.reload.supported? }.from(false).to(true)
end
end

View file

@ -0,0 +1,19 @@
require "rails_helper"
require Rails.root.join(
"lib/data_update_scripts/20201030015634_make_tags_with_mods_supported.rb",
)
describe DataUpdateScripts::MakeTagsWithModsSupported do
let!(:tag1) { create(:tag, name: "Test1", supported: false) }
let!(:tag2) { create(:tag, name: "Test2", supported: false) }
let!(:user) { create(:user) }
it "sets tags with moderators to supported", :aggregate_failures do
user.add_role(:tag_moderator, tag1)
expect do
described_class.new.run
end.to change { tag1.reload.supported? }.from(false).to(true)
expect(tag2.reload.supported).to be false
end
end