This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/lib/tasks/categories.rake
Blake Erickson 092eeb5ca3 FEATURE: Create a rake task for destroying categories
Created a rake task for destroying multiple categories along with any
subcategories and topics the belong to those categories.

Also created a rake task for listing all of your categories.

Refactored existing destroy rake tasks to use new logging method, that
allows for puts output in the console but prevents it from showing in
the specs.
2019-07-17 12:44:14 -06:00

47 lines
1.3 KiB
Ruby

# frozen_string_literal: true
task "categories:move_topics", [:from_category, :to_category] => [:environment] do |_, args|
from_category_id = args[:from_category]
to_category_id = args[:to_category]
if !from_category_id || !to_category_id
puts "ERROR: Expecting categories:move_topics[from_category_id,to_category_id]"
exit 1
end
from_category = Category.find(from_category_id)
to_category = Category.find(to_category_id)
if from_category.present? && to_category.present?
puts "Moving topics from #{from_category.slug} to #{to_category.slug}..."
Topic.where(category_id: from_category.id).update_all(category_id: to_category.id)
from_category.update_attribute(:topic_count, 0)
puts "Updating category stats..."
Category.update_stats
end
puts "", "Done!", ""
end
task "categories:create_definition" => :environment do
puts "Creating category definitions"
puts
Category.where(topic_id: nil).each(&:create_category_definition)
puts "", "Done!", ""
end
def print_status(current, max)
print "\r%9d / %d (%5.1f%%)" % [current, max, ((current.to_f / max.to_f) * 100).round(1)]
end
desc "Output a list of categories"
task "categories:list" => :environment do
categories = Category.pluck(:id, :slug, :parent_category_id)
categories.each do |c|
puts "id: #{c[0]}, slug: #{c[1]}, parent: #{c[2]}"
end
end