Merge pull request #891 from mlorb/ml-sci-1617

Add copying of TinyMce assets when cloning the protocol [SCI-1617]
This commit is contained in:
mlorb 2017-11-23 14:31:29 +01:00 committed by GitHub
commit c46d630f0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View file

@ -44,6 +44,15 @@ module TinyMceHelper
destroy_removed_tiny_mce_assets(ids, ref)
end
def replace_tiny_mce_assets(text, img_ids)
img_ids.each do |src_id, dest_id|
regex = /\[~tiny_mce_id:#{src_id}\]/
new_token = "[~tiny_mce_id:#{dest_id}]"
text.sub!(regex, new_token)
end
text
end
def destroy_removed_tiny_mce_assets(ids, ref)
ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all
end

View file

@ -1,6 +1,7 @@
class Protocol < ActiveRecord::Base
include SearchableModel
include RenamingUtil
extend TinyMceHelper
after_save :update_linked_children
after_destroy :decrement_linked_children
@ -323,6 +324,24 @@ class Protocol < ActiveRecord::Base
table2.team = dest.team
step2.tables << table2
end
# Copy tinyMce assets
cloned_img_ids = []
step.tiny_mce_assets.each do |tiny_img|
tiny_img2 = TinyMceAsset.new(
image: tiny_img.image,
estimated_size: tiny_img.estimated_size,
step: step2,
team: dest.team
)
tiny_img2.save
step2.tiny_mce_assets << tiny_img2
cloned_img_ids << [tiny_img.id, tiny_img2.id]
end
step2.update(
description: replace_tiny_mce_assets(step2.description, cloned_img_ids)
)
end
# Call clone helper