Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/admin_post_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class AdminPostTag < ApplicationRecord
include AsyncWithResque
@queue = :utilities

belongs_to :language
has_many :admin_post_taggings
has_many :admin_posts, through: :admin_post_taggings
Expand All @@ -18,4 +21,7 @@ def self.fetch(name, language_id)
tag.valid? ? tag : nil
end

def self.delete_unused
left_outer_joins(:admin_post_taggings).where(admin_post_taggings: { id: nil }).delete_all
end
end
7 changes: 7 additions & 0 deletions config/resque_schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ cleanup_work_original_creators:
Remove original_creators for works orphaned/moved more than
ORIGINAL_CREATOR_TTL_HOURS hours ago.

delete_unused_admin_post_tags:
cron: "0 0 * * 0"
class: "AdminPostTag"
queue: utilities
args: delete_unused
description: "Delete admin post tags not used on any admin post."

disable_admin_post_comments:
every: 1d
class: "AdminPost"
Expand Down
8 changes: 8 additions & 0 deletions factories/admin_post_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "faker"

FactoryBot.define do
factory :admin_post_tag do
name { Faker::Lorem.unique.word }
language { Language.default }
end
end
2 changes: 1 addition & 1 deletion lib/tasks/tag_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace :Tag do

desc "Delete unused admin post tags"
task(delete_unused_admin_post_tags: :environment) do
AdminPostTag.joins("LEFT JOIN `admin_post_taggings` ON admin_post_taggings.admin_post_tag_id = admin_post_tags.id").where("admin_post_taggings.id IS NULL").destroy_all
AdminPostTag.delete_unused
end

desc "Clean up orphaned taggings"
Expand Down
44 changes: 44 additions & 0 deletions spec/models/admin_post_tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "spec_helper"

describe AdminPostTag do
describe ".delete_unused" do
let!(:admin_post) { create(:admin_post) }
let!(:tag) { create(:admin_post_tag) }

context "when a tag is used on an admin post" do
before do
admin_post.admin_post_taggings.create!(admin_post_tag: tag)
end

it "does not delete the tag" do
expect { AdminPostTag.delete_unused }
.not_to change { AdminPostTag.count }
end
end

context "when a tag is not used on any admin post" do
it "deletes the tag" do
expect { AdminPostTag.delete_unused }
.to change { AdminPostTag.count }
.by(-1)
expect(AdminPostTag.find_by(name: tag.name)).to be_nil
end
end

context "when both used and unused tags exist" do
let!(:unused_tag) { create(:admin_post_tag) }

before do
admin_post.admin_post_taggings.create!(admin_post_tag: tag)
end

it "only deletes the unused tag" do
expect { AdminPostTag.delete_unused }
.to change { AdminPostTag.count }
.by(-1)
expect(AdminPostTag.find_by(name: tag.name)).to be_present
expect(AdminPostTag.find_by(name: unused_tag.name)).to be_nil
end
end
end
end
Loading