From b6c0d352bd83ab2a8b34f13099fb36e08755be67 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 24 Nov 2017 16:25:12 +0100 Subject: [PATCH 1/3] Adds task to copy images [fixes SCI-1650] --- lib/tasks/tiny_mce_asset.rake | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake index 275e0722b..ec2fcf5a9 100644 --- a/lib/tasks/tiny_mce_asset.rake +++ b/lib/tasks/tiny_mce_asset.rake @@ -6,4 +6,75 @@ namespace :tiny_mce_asset do 'result_text_id IS ? AND created_at < ?', nil, nil, 7.days.ago) end + + desc 'Generate new tiny_mce_assets and replace old assets in RTE' + task regenerage_images: :environment do + regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ + replaced_images = 0 + failed_attempts = 0 + all_images = TinyMceAsset.count + failed_attempts_ids = [] + puts 'Start processing steps...' + Step.find_each do |step| + next unless step.description + images = step.description.match(regex) + next unless images + team = step.protocol.team + step.description.gsub!(regex) do |el| + match = el.match(regex) + old_img = TinyMceAsset.find_by_id(match[1]) + new_img = TinyMceAsset.create(image: old_img.image, + team: team, + reference: step) + if new_img + # This image will be removed by `remove_obsolete_images` rake task + # until all the steps are not updated we still need this image + # in case it appears on some other step + old_img.update_attributes(team_id: nil, step_id: nil) + replaced_images += 1 + "[~tiny_mce_id:#{new_img.id}]" + else + failed_attempts += 1 + failed_attempts_ids << old_img.id + "[~tiny_mce_id:#{old_img.id}]" # return the old img + end + end + step.save + end + puts 'Completed processing steps...' + puts 'Start processing result_texts...' + ResultText.find_each do |result_text| + next unless result_text.text + images = result_text.text.match(regex) + next unless images + team = result_text.result.my_module.protocol.team + result_text.text.gsub!(regex) do |el| + match = el.match(regex) + old_img = TinyMceAsset.find_by_id(match[1]) + new_img = TinyMceAsset.create(image: old_img.image, + team: team, + reference: result_text) + if new_img + # This image will be removed by `remove_obsolete_images` rake task + # until all the steps are not updated we still need this image + # in case it appears on some other step + old_img.update_attributes(team_id: nil, step_id: nil) + replaced_images += 1 + "[~tiny_mce_id:#{new_img.id}]" + else + failed_attempts += 1 + failed_attempts_ids << old_img.id + "[~tiny_mce_id:#{old_img.id}]" # return the old img + end + end + result_text.save + end + puts 'Completed processing result_texts...' + puts '----------- TASK REPORT -----------------' + puts "All images: #{all_images}" + puts "Recreated images: #{replaced_images}" + puts "Failed attempts: #{failed_attempts}" + puts "TinyMceAsset ids of failed attempts: #{failed_attempts_ids}" + puts '-----------------------------------------' + end end From dec1e53b533a3b5037bc2e9b0d420cf9f9261e83 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 28 Nov 2017 10:09:06 +0100 Subject: [PATCH 2/3] fix banana --- lib/tasks/tiny_mce_asset.rake | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/tasks/tiny_mce_asset.rake b/lib/tasks/tiny_mce_asset.rake index ec2fcf5a9..a668cee5b 100644 --- a/lib/tasks/tiny_mce_asset.rake +++ b/lib/tasks/tiny_mce_asset.rake @@ -4,11 +4,11 @@ namespace :tiny_mce_asset do task remove_obsolete_images: :environment do TinyMceAsset.where('step_id IS ? AND ' \ 'result_text_id IS ? AND created_at < ?', - nil, nil, 7.days.ago) + nil, nil, 7.days.ago).destroy_all end desc 'Generate new tiny_mce_assets and replace old assets in RTE' - task regenerage_images: :environment do + task regenerate_images: :environment do regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/ replaced_images = 0 failed_attempts = 0 @@ -16,9 +16,7 @@ namespace :tiny_mce_asset do failed_attempts_ids = [] puts 'Start processing steps...' Step.find_each do |step| - next unless step.description - images = step.description.match(regex) - next unless images + next unless step.description && step.description.match(regex) team = step.protocol.team step.description.gsub!(regex) do |el| match = el.match(regex) @@ -30,7 +28,7 @@ namespace :tiny_mce_asset do # This image will be removed by `remove_obsolete_images` rake task # until all the steps are not updated we still need this image # in case it appears on some other step - old_img.update_attributes(team_id: nil, step_id: nil) + old_img.update_attributes(result_text_id: nil, step_id: nil) replaced_images += 1 "[~tiny_mce_id:#{new_img.id}]" else @@ -44,9 +42,7 @@ namespace :tiny_mce_asset do puts 'Completed processing steps...' puts 'Start processing result_texts...' ResultText.find_each do |result_text| - next unless result_text.text - images = result_text.text.match(regex) - next unless images + next unless result_text.text && result_text.text.match(regex) team = result_text.result.my_module.protocol.team result_text.text.gsub!(regex) do |el| match = el.match(regex) @@ -58,7 +54,7 @@ namespace :tiny_mce_asset do # This image will be removed by `remove_obsolete_images` rake task # until all the steps are not updated we still need this image # in case it appears on some other step - old_img.update_attributes(team_id: nil, step_id: nil) + old_img.update_attributes(result_text_id: nil, step_id: nil) replaced_images += 1 "[~tiny_mce_id:#{new_img.id}]" else From e233fbe1d88d6e8759e31716c94dc3fb6292b975 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 28 Nov 2017 11:01:07 +0100 Subject: [PATCH 3/3] fix image removing from tinymce --- app/helpers/tiny_mce_helper.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/helpers/tiny_mce_helper.rb b/app/helpers/tiny_mce_helper.rb index d28750119..22609447e 100644 --- a/app/helpers/tiny_mce_helper.rb +++ b/app/helpers/tiny_mce_helper.rb @@ -54,6 +54,12 @@ module TinyMceHelper end def destroy_removed_tiny_mce_assets(ids, ref) - ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all + # need to check if the array is empty because if we pass the empty array + # in the SQL query it will not work properly + if ids.empty? + ref.tiny_mce_assets.destroy_all + else + ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all + end end end