2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-08 05:35:12 -04:00
|
|
|
task "categories:move_topics", %i[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)
|
|
|
|
|
2018-05-08 05:44:19 -04:00
|
|
|
if from_category.present? && to_category.present?
|
|
|
|
puts "Moving topics from #{from_category.slug} to #{to_category.slug}..."
|
2021-11-02 23:27:51 -04:00
|
|
|
|
|
|
|
Topic
|
|
|
|
.where(category_id: from_category.id)
|
|
|
|
.where.not(id: from_category.topic_id)
|
|
|
|
.update_all(category_id: to_category.id)
|
|
|
|
|
2018-05-08 05:35:12 -04:00
|
|
|
from_category.update_attribute(:topic_count, 0)
|
2018-05-08 05:44:19 -04:00
|
|
|
|
|
|
|
puts "Updating category stats..."
|
2018-05-08 05:35:12 -04:00
|
|
|
Category.update_stats
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "", "Done!", ""
|
|
|
|
end
|
2018-08-16 04:20:52 -04:00
|
|
|
|
|
|
|
task "categories:create_definition" => :environment do
|
|
|
|
puts "Creating category definitions"
|
|
|
|
puts
|
|
|
|
|
2018-08-16 04:30:07 -04:00
|
|
|
Category.where(topic_id: nil).each(&:create_category_definition)
|
2018-08-16 04:20:52 -04:00
|
|
|
|
2018-08-16 04:30:07 -04:00
|
|
|
puts "", "Done!", ""
|
2018-08-16 04:20:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def print_status(current, max)
|
|
|
|
print "\r%9d / %d (%5.1f%%)" % [current, max, ((current.to_f / max.to_f) * 100).round(1)]
|
|
|
|
end
|
2019-07-17 14:41:13 -04:00
|
|
|
|
|
|
|
desc "Output a list of categories"
|
|
|
|
task "categories:list" => :environment do
|
2019-07-18 21:15:01 -04:00
|
|
|
categories = Category.where(parent_category_id: nil).order(:slug).pluck(:id, :slug)
|
|
|
|
puts "id category-slug"
|
|
|
|
puts "-- -----------------"
|
2019-07-17 14:41:13 -04:00
|
|
|
categories.each do |c|
|
2019-07-18 21:15:01 -04:00
|
|
|
puts "#{c[0]} #{c[1]}"
|
|
|
|
Category
|
|
|
|
.where(parent_category_id: c[0])
|
|
|
|
.order(:slug)
|
|
|
|
.pluck(:id, :slug)
|
|
|
|
.each { |s| puts " #{s[0]} #{s[1]}" }
|
2019-07-17 14:41:13 -04:00
|
|
|
end
|
|
|
|
end
|