Commit 56fc5893 authored by James Lopez's avatar James Lopez

more refactoring to use dynamic import

parent a28be02e
# Class relationships to be included in the project import/export
# Model relationships to be included in the project import/export
:project_tree:
- :issues
- :labels
......@@ -13,6 +13,7 @@
- :commit_statuses:
- :commit
# Only include the following attributes for the models specified.
:attributes_only:
:project:
- :name
......@@ -30,6 +31,7 @@
- :email
- :username
# Do not include the following attributes for the models specified.
:attributes_except:
:snippets:
- :expired_at
\ No newline at end of file
......@@ -11,29 +11,25 @@ module Projects
def restore
json = IO.read(@path)
@tree_hash = ActiveSupport::JSON.decode(json)
@project_members = @tree_hash.delete('project_members')
create_relations
puts project.inspect
end
private
def members_map
@members ||= Projects::ImportExport::MembersMapper.map(
exported_members: @tree_hash.delete('project_members'), user: @user, project_id: project.id)
exported_members: @project_members, user: @user, project_id: project.id)
end
#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|
if relation.is_a?(Hash)
relation.values.each do |value|
create_relations(value, @tree_hash[relation.to_s])
create_sub_relations(relation, tree_hash)
end
end
relation_hash = create_relation(relation, tree_hash[relation.to_s])
project.update_attribute(relation, relation_hash)
relation_key = relation.is_a?(Hash) ? relation.keys.first : relation
relation_hash = create_relation(relation_key, tree_hash[relation_key.to_s])
project.update_attribute(relation_key, relation_hash)
# relation_hash = nil
# # FIXME
# # next if tree_hash[relation.to_s].blank?
......@@ -56,10 +52,7 @@ module Projects
end
def default_relation_list
Projects::ImportExport::ImportExportReader.tree
# ImportExport.project_tree.reject do |rel|
# rel.is_a?(Hash) && !rel[:project_members].blank?
# end
Projects::ImportExport::ImportExportReader.tree.reject { |model| model.is_a?(Hash) && model[:project_members] }
end
def project
......@@ -74,6 +67,20 @@ module Projects
project
end
def create_sub_relations(relation, tree_hash)
# TODO refactor this
relation_key = relation.keys.first
tree_hash[relation_key.to_s].each do |relation_item|
relation.values.each do |sub_relation|
relation_hash = relation_item[sub_relation.to_s]
next if relation_hash.blank?
sub_relation_object = Projects::ImportExport::RelationFactory.create(
relation_sym: sub_relation, relation_hash: relation_hash, members_map: members_map)
relation_item[sub_relation.to_s] = sub_relation_object
end
end
end
def create_relation(relation, relation_hash_list)
[relation_hash_list].flatten.map do |relation_hash|
Projects::ImportExport::RelationFactory.create(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment