Skip to content

Commit

Permalink
consolidate and remove unassociated tags
Browse files Browse the repository at this point in the history
  • Loading branch information
SanthiParakal133 committed Aug 28, 2023
1 parent db8c17f commit ff893f1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/tasks/tags.rake
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

0 comments on commit ff893f1

Please sign in to comment.