diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 6a231ce2d..f3a312470 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -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 diff --git a/app/models/step_text.rb b/app/models/step_text.rb index 2faf98b82..7044760b4 100644 --- a/app/models/step_text.rb +++ b/app/models/step_text.rb @@ -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? diff --git a/app/services/protocol_importers/import_protocol_service.rb b/app/services/protocol_importers/import_protocol_service.rb index ead924f01..1ef3ceac9 100644 --- a/app/services/protocol_importers/import_protocol_service.rb +++ b/app/services/protocol_importers/import_protocol_service.rb @@ -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