FEATURE: Option to export multiple categories using export_category method

This commit is contained in:
Vinoth Kanan 2017-11-11 18:52:22 +05:30
parent ed16cba77f
commit d9823f69c6
5 changed files with 38 additions and 22 deletions

View File

@ -4,10 +4,8 @@ require "import_export/topic_exporter"
module ImportExport
class CategoryExporter < BaseExporter
def initialize(category_id)
@category = Category.find(category_id)
@categories = Category.where(parent_category_id: category_id).to_a
@categories << @category
def initialize(category_ids)
@categories = Category.where(id: category_ids).or(Category.where(parent_category_id: category_ids)).to_a
@export_data = {
categories: [],
groups: [],
@ -17,7 +15,6 @@ module ImportExport
end
def perform
puts "Exporting category #{@category.name}...", ""
export_categories!
export_category_groups!
export_topics_and_users

View File

@ -11,12 +11,12 @@ module ImportExport
ImportExport::Importer.new(data).perform
end
def self.export_categories(include_users, filename = nil)
def self.export_category_structure(include_users, filename = nil)
ImportExport::CategoryStructureExporter.new(include_users).perform.save_to_file(filename)
end
def self.export_category(category_id, filename = nil)
ImportExport::CategoryExporter.new(category_id).perform.save_to_file(filename)
def self.export_categories(category_ids, filename = nil)
ImportExport::CategoryExporter.new(category_ids).perform.save_to_file(filename)
end
def self.export_topics(topic_ids, filename = nil)

View File

@ -1,7 +1,15 @@
desc 'Export all the categories'
task 'export:categories', [:include_group_users, :file_name] => [:environment] do |_, args|
task 'export:categories', [:category_ids] => [:environment] do |_, args|
require "import_export/import_export"
ImportExport.export_categories(args[:include_group_users], args[:file_name])
ImportExport.export_categories(args[:category_ids])
puts "", "Done", ""
end
desc 'Export only the structure of all categories'
task 'export:category_structure', [:include_group_users, :file_name] => [:environment] do |_, args|
require "import_export/import_export"
ImportExport.export_category_structure(args[:include_group_users], args[:file_name])
puts "", "Done", ""
end

View File

@ -181,16 +181,22 @@ class DiscourseCLI < Thor
puts 'Requests sent. Clients will refresh on next navigation.'
end
desc "export_category", "Export a category, all its topics, and all users who posted in those topics"
def export_category(category_id, filename = nil)
raise "Category id argument is missing!" unless category_id
desc "export_categories", "Export categories, all its topics, and all users who posted in those topics"
def export_categories(*category_ids)
puts "Starting export of categories...", ""
load_rails
load_import_export
ImportExport.export_category(category_id, filename)
ImportExport.export_categories(category_ids)
puts "", "Done", ""
end
desc "export_category", "Export a category, all its topics, and all users who posted in those topics"
def export_category(category_id)
raise "Category id argument is missing!" unless category_id
export_categories([category_id])
end
desc "import_category", "Import a category, its topics and the users from the output of the export_category command"
def import_category(filename)
raise "File name argument missing!" unless filename

View File

@ -12,12 +12,8 @@ describe ImportExport::CategoryExporter do
end
context '.perform' do
it 'raises an error when the category is not found' do
expect { ImportExport::CategoryExporter.new(100).perform }.to raise_error(ActiveRecord::RecordNotFound)
end
it 'export the category when it is found' do
data = ImportExport::CategoryExporter.new(category.id).perform.export_data
data = ImportExport::CategoryExporter.new([category.id]).perform.export_data
expect(data[:categories].count).to eq(1)
expect(data[:groups].count).to eq(0)
@ -25,16 +21,25 @@ describe ImportExport::CategoryExporter do
it 'export the category with permission groups' do
category_group = Fabricate(:category_group, category: category, group: group)
data = ImportExport::CategoryExporter.new(category.id).perform.export_data
data = ImportExport::CategoryExporter.new([category.id]).perform.export_data
expect(data[:categories].count).to eq(1)
expect(data[:groups].count).to eq(1)
end
it 'export multiple categories' do
category2 = Fabricate(:category)
category_group = Fabricate(:category_group, category: category, group: group)
data = ImportExport::CategoryExporter.new([category.id, category2.id]).perform.export_data
expect(data[:categories].count).to eq(2)
expect(data[:groups].count).to eq(1)
end
it 'export the category with topics and users' do
topic1 = Fabricate(:topic, category: category, user_id: -1)
topic2 = Fabricate(:topic, category: category, user: user)
data = ImportExport::CategoryExporter.new(category.id).perform.export_data
data = ImportExport::CategoryExporter.new([category.id]).perform.export_data
expect(data[:categories].count).to eq(1)
expect(data[:groups].count).to eq(0)