diff --git a/app/helpers/tiny_mce_helper.rb b/app/helpers/tiny_mce_helper.rb index 62431604d..d28750119 100644 --- a/app/helpers/tiny_mce_helper.rb +++ b/app/helpers/tiny_mce_helper.rb @@ -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 diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 8f4049561..b81177dc3 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -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