Update protocols.io importer and import from repository [SCI-6872] (#4138)

This commit is contained in:
artoscinote 2022-06-08 15:55:50 +02:00 committed by GitHub
parent 81773151a4
commit e114f9b880
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 14 deletions

View file

@ -268,7 +268,6 @@ class Protocol < ApplicationRecord
src.steps.each do |step|
step2 = Step.new(
name: step.name,
description: step.description,
position: step.position,
completed: false,
user: current_user,
@ -276,6 +275,24 @@ class Protocol < ApplicationRecord
)
step2.save!
position = 0
# Copy texts
step.step_texts.each do |step_text|
step_text2 = StepText.new(
text: step_text.text,
step: step2
)
step_text2.save!
step2.step_orderable_elements.create!(
position: position,
orderable: step_text2
)
position += 1
end
# Copy checklists
step.checklists.asc.each do |checklist|
checklist2 = Checklist.new(
@ -299,6 +316,13 @@ class Protocol < ApplicationRecord
end
step2.checklists << checklist2
step2.step_orderable_elements.create!(
position: position,
orderable: checklist2
)
position += 1
end
# "Shallow" Copy assets
@ -319,6 +343,13 @@ class Protocol < ApplicationRecord
table2.last_modified_by = current_user
table2.team = dest.team
step2.tables << table2
step2.step_orderable_elements.create!(
position: position,
orderable: table2.step_tables.first
)
position += 1
end
# Copy steps tinyMce assets

View file

@ -10,6 +10,8 @@ class StepText < ApplicationRecord
belongs_to :step, inverse_of: :step_texts, touch: true
has_many :step_orderable_elements, as: :orderable, dependent: :destroy
scope :asc, -> { order('step_texts.created_at ASC') }
def name
return if text.blank?

View file

@ -17,23 +17,32 @@ module ProtocolImporters
def call
return self unless valid?
@protocol = Protocol.new(@protocol_params.merge!(added_by: @user, team: @team))
ActiveRecord::Base.transaction do
@protocol = Protocol.create!(@protocol_params.merge!(added_by: @user, team: @team))
@protocol.steps << @steps_params.map do |step_params|
# Create step with nested attributes for tables
s = Step.new(step_params
.symbolize_keys
.slice(:name, :position, :description, :tables_attributes)
.merge(user: @user, completed: false)
.merge(last_modified_by_id: @user.id))
@steps_params.map do |step_params|
step_params.symbolize_keys!
# 'Manually' create assets here. "Accept nasted attributes" won't work for assets
s.assets << AttachmentsBuilder.generate(step_params.deep_symbolize_keys, user: @user, team: @team)
s
# Create step with nested attributes for tables
step = @protocol.steps.create!(step_params.slice(:name, :position, :tables_attributes)
.merge(user: @user, completed: false)
.merge(last_modified_by_id: @user.id))
# Add description text block
step_text = step.step_texts.create!(text: step_params[:description])
step.step_orderable_elements.create!(
position: 0,
orderable: step_text
)
# 'Manually' create assets here. "Accept nasted attributes" won't work for assets
step.assets << AttachmentsBuilder.generate(step_params.deep_symbolize_keys, user: @user, team: @team)
step
end
rescue StandardError => e
@errors[:protocol] = e.message
end
@errors[:protocol] = @protocol.errors.messages unless @protocol.save
self
end