From fb9fcb9371913626818a0bce9b68cc2b6278ba3f Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 8 Jun 2022 15:31:48 +0200 Subject: [PATCH] Update team import/export scripts [SCI-6875] --- .../model_exporters/model_exporter.rb | 17 +++++-- app/services/team_importer.rb | 47 ++++++++++++------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/app/services/model_exporters/model_exporter.rb b/app/services/model_exporters/model_exporter.rb index a4e88424a..8845e4697 100644 --- a/app/services/model_exporters/model_exporter.rb +++ b/app/services/model_exporters/model_exporter.rb @@ -43,15 +43,26 @@ module ModelExporters @assets_to_copy.push(step.assets.to_a) if step.assets.present? { step: step, - checklists: step.checklists.map { |c| checklist(c) }, + step_orderable_elements: step.step_orderable_elements.map { |e| step_orderable_element(e) }, step_comments: step.step_comments, step_assets: step.step_assets, assets: step.assets.map { |a| assets_data(a) }, - step_tables: step.step_tables, - tables: step.tables.map { |t| table(t) } } end + def step_orderable_element(element) + element_json = element.as_json + case element.orderable_type + when 'StepText' + element_json['step_text'] = element.orderable.as_json + when 'Checklist' + element_json['checklist'] = checklist(element.orderable) + when 'StepTable' + element_json['table'] = table(element.orderable.table) + end + element_json + end + def assets_data(asset) return unless asset.file.attached? diff --git a/app/services/team_importer.rb b/app/services/team_importer.rb index 35ae086b9..6df4e75dc 100644 --- a/app/services/team_importer.rb +++ b/app/services/team_importer.rb @@ -808,27 +808,39 @@ class TeamImporter step_comment.save! end - step_json['tables'].each do |table_json| - table = Table.new(table_json) - orig_table_id = table.id - table.id = nil - table.created_by_id = user_id || find_user(table.created_by_id) - table.last_modified_by_id = - user_id || find_user(table.last_modified_by_id) - table.team = protocol.team - table.contents = Base64.decode64(table.contents) - table.data_vector = Base64.decode64(table.data_vector) - table.save! - @table_mappings[orig_table_id] = table.id - StepTable.create!(step: step, table: table) + step_json['step_orderable_elements'].each do |element_json| + if element_json['step_text'] + orderable = StepText.new(element_json['step_text']) + orderable.step_id = step.id + orderable.id = nil + orderable.save! + elsif element_json['table'] + table = Table.new(element_json['table']) + orig_table_id = table.id + table.id = nil + table.created_by_id = user_id || find_user(table.created_by_id) + table.last_modified_by_id = + user_id || find_user(table.last_modified_by_id) + table.team = protocol.team + table.contents = Base64.decode64(table.contents) + table.data_vector = Base64.decode64(table.data_vector) + table.save! + @table_mappings[orig_table_id] = table.id + orderable = StepTable.create!(step: step, table: table) + elsif element_json['checklist'] + orderable = create_step_checklist(element_json['checklist'], step, user_id) + end + StepOrderableElement.create!( + position: element_json['position'], + step: step, + orderable: orderable + ) end step_json['assets'].each do |asset_json| asset = create_asset(asset_json, protocol.team, user_id) StepAsset.create!(step: step, asset: asset) end - - create_step_checklists(step_json['checklists'], step, user_id) end end @@ -915,8 +927,7 @@ class TeamImporter asset end - def create_step_checklists(step_checklists_json, step, user_id = nil) - step_checklists_json.each do |checklist_json| + def create_step_checklist(checklist_json, step, user_id = nil) checklist = Checklist.new(checklist_json['checklist']) orig_checklist_id = checklist.id checklist.id = nil @@ -937,7 +948,7 @@ class TeamImporter user_id || find_user(checklist_item.last_modified_by_id) checklist_item.save! end - end + checklist end def create_reports(reports_json, team)