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

WIP (broken) - playing with import stuff

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