From eb87cffa13c14cd8cf8d3e929659b638d33b203c Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 23 Feb 2016 12:18:52 -0500 Subject: [PATCH] FEATURE: Category/topic importer skips objects that it has already imported. --- lib/import_export/category_importer.rb | 38 ++++++++++++++++---------- lib/import_export/topic_importer.rb | 27 ++++++++++++------ 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/import_export/category_importer.rb b/lib/import_export/category_importer.rb index 48ceb23f5..9b21b7aa2 100644 --- a/lib/import_export/category_importer.rb +++ b/lib/import_export/category_importer.rb @@ -39,25 +39,33 @@ module ImportExport def import_categories id = @export_data[:category].delete(:id) - permissions = @export_data[:category].delete(:permissions_params) - parent = Category.new(@export_data[:category]) - parent.user_id = @topic_importer.new_user_id(@export_data[:category][:user_id]) # imported user's new id - parent.custom_fields["import_id"] = id - parent.permissions = permissions if permissions - parent.save! - set_category_description(parent, @export_data[:category][:description]) + parent = CategoryCustomField.where(name: 'import_id', value: id.to_s).first.try(:category) + + unless parent + permissions = @export_data[:category].delete(:permissions_params) + parent = Category.new(@export_data[:category]) + parent.user_id = @topic_importer.new_user_id(@export_data[:category][:user_id]) # imported user's new id + parent.custom_fields["import_id"] = id + parent.permissions = permissions if permissions + parent.save! + set_category_description(parent, @export_data[:category][:description]) + end @export_data[:subcategories].each do |cat_attrs| id = cat_attrs.delete(:id) - permissions = cat_attrs.delete(:permissions_params) - subcategory = Category.new(cat_attrs) - subcategory.parent_category_id = parent.id - subcategory.user_id = @topic_importer.new_user_id(cat_attrs[:user_id]) - subcategory.custom_fields["import_id"] = id - subcategory.permissions = permissions if permissions - subcategory.save! - set_category_description(subcategory, cat_attrs[:description]) + existing = CategoryCustomField.where(name: 'import_id', value: id.to_s).first.try(:category) + + unless existing + permissions = cat_attrs.delete(:permissions_params) + subcategory = Category.new(cat_attrs) + subcategory.parent_category_id = parent.id + subcategory.user_id = @topic_importer.new_user_id(cat_attrs[:user_id]) + subcategory.custom_fields["import_id"] = id + subcategory.permissions = permissions if permissions + subcategory.save! + set_category_description(subcategory, cat_attrs[:description]) + end end end diff --git a/lib/import_export/topic_importer.rb b/lib/import_export/topic_importer.rb index c101f768a..e7fc2d8f3 100644 --- a/lib/import_export/topic_importer.rb +++ b/lib/import_export/topic_importer.rb @@ -19,9 +19,11 @@ module ImportExport def import_users @export_data[:users].each do |u| existing = User.where(email: u[:email]).first - if existing && existing.custom_fields["import_id"] != u[:id] - existing.custom_fields["import_id"] = u[:id] - existing.save! + if existing + if existing.custom_fields["import_id"] != u[:id] + existing.custom_fields["import_id"] = u[:id] + existing.save! + end else u = create_user(u, u[:id]) # see ImportScripts::Base end @@ -38,15 +40,24 @@ module ImportExport first_post_attrs[:user_id] = new_user_id(first_post_attrs[:user_id]) first_post_attrs[:category] = new_category_id(t[:category_id]) - first_post = create_post( first_post_attrs, first_post_attrs[:id] ) + first_post = PostCustomField.where(name: "import_id", value: first_post_attrs[:id]).first.try(:post) + + unless first_post + first_post = create_post( first_post_attrs, first_post_attrs[:id] ) + end + topic_id = first_post.topic_id + t[:posts].each_with_index do |post_data, i| next if i == 0 print "." - create_post(post_data.merge({ - topic_id: topic_id, - user_id: new_user_id(post_data[:user_id]) - }), post_data[:id]) # see ImportScripts::Base + existing = PostCustomField.where(name: "import_id", value: post_data[:id]).first.try(:post) + unless existing + create_post(post_data.merge({ + topic_id: topic_id, + user_id: new_user_id(post_data[:user_id]) + }), post_data[:id]) # see ImportScripts::Base + end end end