mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-09 13:28:53 +08:00
Update protocols.io importer and import from repository [SCI-6872] (#4138)
This commit is contained in:
parent
81773151a4
commit
e114f9b880
3 changed files with 56 additions and 14 deletions
|
@ -268,7 +268,6 @@ class Protocol < ApplicationRecord
|
||||||
src.steps.each do |step|
|
src.steps.each do |step|
|
||||||
step2 = Step.new(
|
step2 = Step.new(
|
||||||
name: step.name,
|
name: step.name,
|
||||||
description: step.description,
|
|
||||||
position: step.position,
|
position: step.position,
|
||||||
completed: false,
|
completed: false,
|
||||||
user: current_user,
|
user: current_user,
|
||||||
|
@ -276,6 +275,24 @@ class Protocol < ApplicationRecord
|
||||||
)
|
)
|
||||||
step2.save!
|
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
|
# Copy checklists
|
||||||
step.checklists.asc.each do |checklist|
|
step.checklists.asc.each do |checklist|
|
||||||
checklist2 = Checklist.new(
|
checklist2 = Checklist.new(
|
||||||
|
@ -299,6 +316,13 @@ class Protocol < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
step2.checklists << checklist2
|
step2.checklists << checklist2
|
||||||
|
|
||||||
|
step2.step_orderable_elements.create!(
|
||||||
|
position: position,
|
||||||
|
orderable: checklist2
|
||||||
|
)
|
||||||
|
|
||||||
|
position += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
# "Shallow" Copy assets
|
# "Shallow" Copy assets
|
||||||
|
@ -319,6 +343,13 @@ class Protocol < ApplicationRecord
|
||||||
table2.last_modified_by = current_user
|
table2.last_modified_by = current_user
|
||||||
table2.team = dest.team
|
table2.team = dest.team
|
||||||
step2.tables << table2
|
step2.tables << table2
|
||||||
|
|
||||||
|
step2.step_orderable_elements.create!(
|
||||||
|
position: position,
|
||||||
|
orderable: table2.step_tables.first
|
||||||
|
)
|
||||||
|
|
||||||
|
position += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
# Copy steps tinyMce assets
|
# Copy steps tinyMce assets
|
||||||
|
|
|
@ -10,6 +10,8 @@ class StepText < ApplicationRecord
|
||||||
belongs_to :step, inverse_of: :step_texts, touch: true
|
belongs_to :step, inverse_of: :step_texts, touch: true
|
||||||
has_many :step_orderable_elements, as: :orderable, dependent: :destroy
|
has_many :step_orderable_elements, as: :orderable, dependent: :destroy
|
||||||
|
|
||||||
|
scope :asc, -> { order('step_texts.created_at ASC') }
|
||||||
|
|
||||||
def name
|
def name
|
||||||
return if text.blank?
|
return if text.blank?
|
||||||
|
|
||||||
|
|
|
@ -17,23 +17,32 @@ module ProtocolImporters
|
||||||
def call
|
def call
|
||||||
return self unless valid?
|
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|
|
@steps_params.map do |step_params|
|
||||||
# Create step with nested attributes for tables
|
step_params.symbolize_keys!
|
||||||
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))
|
|
||||||
|
|
||||||
# 'Manually' create assets here. "Accept nasted attributes" won't work for assets
|
# Create step with nested attributes for tables
|
||||||
s.assets << AttachmentsBuilder.generate(step_params.deep_symbolize_keys, user: @user, team: @team)
|
step = @protocol.steps.create!(step_params.slice(:name, :position, :tables_attributes)
|
||||||
s
|
.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
|
end
|
||||||
|
|
||||||
@errors[:protocol] = @protocol.errors.messages unless @protocol.save
|
|
||||||
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue