From a28be02e88f1d213649ef2c25858313caeb83b59 Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Fri, 8 Apr 2016 18:13:19 +0200
Subject: [PATCH] starting to use the new dynamic stuff on the import

---
 .../import_export/import_export_reader.rb     |  8 ++--
 .../import_export/project_tree_restorer.rb    | 47 +++++++++++--------
 2 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/app/services/projects/import_export/import_export_reader.rb b/app/services/projects/import_export/import_export_reader.rb
index e0617ce851c..001d24af604 100644
--- a/app/services/projects/import_export/import_export_reader.rb
+++ b/app/services/projects/import_export/import_export_reader.rb
@@ -7,6 +7,10 @@ module Projects
         { only: atts_only[:project], include: build_hash(tree) }
       end
 
+      def tree
+        config[:project_tree]
+      end
+
       private
 
       def config
@@ -21,10 +25,6 @@ module Projects
         config[:attributes_except]
       end
 
-      def tree
-        config[:project_tree]
-      end
-
       def build_hash(array)
         array.map do |el|
           if el.is_a?(Hash)
diff --git a/app/services/projects/import_export/project_tree_restorer.rb b/app/services/projects/import_export/project_tree_restorer.rb
index 3a767c1980e..ca68840e988 100644
--- a/app/services/projects/import_export/project_tree_restorer.rb
+++ b/app/services/projects/import_export/project_tree_restorer.rb
@@ -25,32 +25,41 @@ module Projects
       #TODO Definitely refactor this method!
       #TODO Think about having a yaml file to describe the tree instead of just hashes?
       def create_relations(relation_list = default_relation_list, tree_hash = @tree_hash)
+        members_map # TODO remove this and fix project_members
         relation_list.each do |relation|
-          relation_hash = nil
-          # FIXME
-          # next if tree_hash[relation.to_s].blank?
-          if (relation.is_a?(Hash) && relation.values.first[:include])
-            #TODO name stuff properly
-            relation_sym = relation.keys.first
-            #TODO remove sub-relation hashes from here so we can save the parent relation first
-            relation_hash = create_relation(relation_sym, tree_hash[relation_sym.to_s])
-            sub_relations = []
-            sub_relation = relation.values.first[:include]
-            sub_relation_hash_list = tree_hash[relation.keys.first.to_s]
-            sub_relation_hash_list.each do |sub_relation_hash|
-              sub_relations << create_relation(relation, sub_relation_hash[relation.to_s])
+          if relation.is_a?(Hash)
+            relation.values.each do |value|
+              create_relations(value, @tree_hash[relation.to_s])
             end
-            relation_hash.update_attribute(sub_relation, sub_relations)
           end
-            relation_hash ||= create_relation(relation, tree_hash[relation.to_s])
-            project.update_attribute(relation, relation_hash)
+          relation_hash = create_relation(relation, tree_hash[relation.to_s])
+          project.update_attribute(relation, relation_hash)
+          # relation_hash = nil
+          # # FIXME
+          # # next if tree_hash[relation.to_s].blank?
+          # if (relation.is_a?(Hash) && relation.values.first[:include])
+          #   #TODO name stuff properly
+          #   relation_sym = relation.keys.first
+          #   #TODO remove sub-relation hashes from here so we can save the parent relation first
+          #   relation_hash = create_relation(relation_sym, tree_hash[relation_sym.to_s])
+          #   sub_relations = []
+          #   sub_relation = relation.values.first[:include]
+          #   sub_relation_hash_list = tree_hash[relation.keys.first.to_s]
+          #   sub_relation_hash_list.each do |sub_relation_hash|
+          #     sub_relations << create_relation(relation, sub_relation_hash[relation.to_s])
+          #   end
+          #   relation_hash.update_attribute(sub_relation, sub_relations)
+          # end
+          #   relation_hash ||= create_relation(relation, tree_hash[relation.to_s])
+          #   project.update_attribute(relation, relation_hash)
         end
       end
 
       def default_relation_list
-        ImportExport.project_tree.reject do |rel|
-          rel.is_a?(Hash) && !rel[:project_members].blank?
-        end
+        Projects::ImportExport::ImportExportReader.tree
+        # ImportExport.project_tree.reject do |rel|
+        #   rel.is_a?(Hash) && !rel[:project_members].blank?
+        # end
       end
 
       def project
-- 
2.30.9