diff --git a/app/services/projects/import_export/project_tree_restorer.rb b/app/services/projects/import_export/project_tree_restorer.rb index 980a376be16c40c59b11bbe6bd0f7a7a3a3d27ff..3ad42f41457441fb9c51d25d2ab6d518b6b58ed2 100644 --- a/app/services/projects/import_export/project_tree_restorer.rb +++ b/app/services/projects/import_export/project_tree_restorer.rb @@ -13,19 +13,23 @@ module Projects def restore json = IO.read(@path) tree_hash = ActiveSupport::JSON.decode(json) - project_params = tree_hash.reject { |_key, value| value.is_a?(Array) } - project = Projects::ImportExport::ProjectFactory.create(project_params: project_params, user: @user) - project.save - relation_hash = {} + project = create_project(tree_hash) ImportExport.project_tree.each do |relation| next if tree_hash[relation.to_s].empty? - relation_hash[relation.to_s] = create_relation(relation, tree_hash[relation.to_s], project.id) - project.update_attribute(relation, relation_hash[relation.to_s]) + relation_hash = create_relation(relation, tree_hash[relation.to_s], project.id) + project.update_attribute(relation, relation_hash) end end private + def create_project(tree_hash) + project_params = tree_hash.reject { |_key, value| value.is_a?(Array) } + project = Projects::ImportExport::ProjectFactory.create(project_params: project_params, user: @user) + project.save + project + end + def create_relation(relation, relation_hash_list, project_id) relation_hash_list.map do |relation_hash| Projects::ImportExport::RelationFactory.create(