-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consolidate and remove unassociated tags
- Loading branch information
1 parent
db8c17f
commit ff893f1
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :tags do | ||
desc "remove tags with no document associated with them" | ||
task detag: :environment do | ||
tags_to_remove = Tag.includes(:documents_tags).where(documents_tags: { tag_id: nil }) | ||
|
||
if tags_to_remove.empty? | ||
puts "All tags are associated with Documents." | ||
else | ||
tags_to_remove.each do |tag| | ||
puts "Removing tag: #{tag.text}" | ||
tag.destroy | ||
end | ||
puts "Tags without associated documents have been removed." | ||
end | ||
end | ||
|
||
desc "merge tags where the differences are capitalization" | ||
task merge: :environment do | ||
tag_groups = Tag.all.group_by { |tag| tag.text.downcase } | ||
tag_groups.each do |_ ,tags| | ||
next if tags.length <= 1 | ||
first_tag = tags.shift | ||
tags.each do |tag| | ||
unless first_tag.documents_tags.exists?(document_id: tag.documents.pluck(:id)) | ||
first_tag.documents_tags << tag.documents_tags | ||
end | ||
tag.destroy | ||
end | ||
end | ||
puts "Tags with capitalization differences have been merged." | ||
end | ||
end |