Commit 2cc6aaec authored by James Lopez's avatar James Lopez

WIP (broken) - playing with import stuff

parent 2dc2ce45
module Projects
module ImportExport
class ProjectTreeRestorer
attr_reader :full_path
attr_reader :project
def initialize(path: )
def initialize(path: , user: user)
@path = path
@user = user
end
#TODO deal with ID issues.
#TODO refactor this method
def restore
json = IO.read(@path)
tree_hash = ActiveSupport::JSON.decode(json)
relation_hash = {}
ImportExport.project_tree.each do |relation|
next if tree_hash[relation.to_s].empty?
tree_hash[relation.to_s] = create_relation(relation, tree_hash[relation.to_s])
relation_hash[relation.to_s] = create_relation(relation, tree_hash[relation.to_s])
end
project = Project.new(tree_hash)
project
project_params = tree_hash.delete_if { |_key, value | value.is_a?(Array)}
@project = ::Projects::CreateService.new(@user, project_params).execute
@project.saved?
end
private
def create_relation(relation, tree_hash)
Projects::ImportExport::RelationFactory.create(
relation_sym: relation, relation_hash: tree_hash[relation.to_s])
def create_relation(relation, relation_hash_list)
relation_hash_list.map do |relation_hash|
Projects::ImportExport::RelationFactory.create(
relation_sym: relation, relation_hash: relation_hash)
end
end
end
end
end
......@@ -3,8 +3,12 @@ module Projects
module RelationFactory
extend self
def create(relation_sym: , relation_hash:)
OVERRIDES = { snippets: :project_snippets }
def create(relation_sym:, relation_hash:)
relation_sym = parse_relation_sym(relation_sym)
klass = relation_class(relation_sym)
relation_hash.delete('id') #screw IDs for now
klass.new(relation_hash)
end
......@@ -13,6 +17,10 @@ module Projects
def relation_class(relation_sym)
relation_sym.to_s.classify.constantize
end
def parse_relation_sym(relation_sym)
OVERRIDES[relation_sym] || relation_sym
end
end
end
end
......@@ -4,17 +4,11 @@ describe Projects::ImportExport::ProjectTreeRestorer, services: true do
describe :restore do
let(:user) { create(:user) }
let(:project_tree_restorer) { Projects::ImportExport::ProjectTreeRestorer.new(path: "fixtures/import_export/project.json") }
before(:each) do
#allow(project_tree_restorer)
# .to receive(:full_path).and_return("fixtures/import_export/project.json")
end
let(:project_tree_restorer) { Projects::ImportExport::ProjectTreeRestorer.new(path: "fixtures/import_export/project.json", user: user) }
context 'JSON' do
let(:restored_project_json) do
project_tree_restorer.restore
#project_json(project_tree_restorer.full_path)
end
it 'restores models based on JSON' do
......@@ -22,8 +16,4 @@ describe Projects::ImportExport::ProjectTreeRestorer, services: true do
end
end
end
def project_json
JSON.parse(IO.read("fixtures/import_export/project.json"))
end
end
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