From d9823f69c6b3b4ee91f6611bd021c3affd56652e Mon Sep 17 00:00:00 2001 From: Vinoth Kanan Date: Sat, 11 Nov 2017 18:52:22 +0530 Subject: [PATCH] FEATURE: Option to export multiple categories using export_category method --- lib/import_export/category_exporter.rb | 7 ++----- lib/import_export/import_export.rb | 6 +++--- lib/tasks/export.rake | 12 ++++++++++-- script/discourse | 16 +++++++++++----- spec/import_export/category_exporter_spec.rb | 19 ++++++++++++------- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/lib/import_export/category_exporter.rb b/lib/import_export/category_exporter.rb index 4ea7209c19f..5568fb5180d 100644 --- a/lib/import_export/category_exporter.rb +++ b/lib/import_export/category_exporter.rb @@ -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 diff --git a/lib/import_export/import_export.rb b/lib/import_export/import_export.rb index 53ed9f591ca..4523bb779d8 100644 --- a/lib/import_export/import_export.rb +++ b/lib/import_export/import_export.rb @@ -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) diff --git a/lib/tasks/export.rake b/lib/tasks/export.rake index cfa59540277..d0b80c88192 100644 --- a/lib/tasks/export.rake +++ b/lib/tasks/export.rake @@ -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 diff --git a/script/discourse b/script/discourse index 1f8fe1275f7..67d4bfec6e7 100755 --- a/script/discourse +++ b/script/discourse @@ -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 diff --git a/spec/import_export/category_exporter_spec.rb b/spec/import_export/category_exporter_spec.rb index 3c226175709..785f520c85f 100644 --- a/spec/import_export/category_exporter_spec.rb +++ b/spec/import_export/category_exporter_spec.rb @@ -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)