mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-06 23:15:34 +08:00
Merge pull request #894 from ZmagoD/zd_SCI_1650
Adds task to copy images [fixes SCI-1650]
This commit is contained in:
commit
205d3b3d8f
2 changed files with 75 additions and 2 deletions
|
@ -54,6 +54,12 @@ module TinyMceHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy_removed_tiny_mce_assets(ids, ref)
|
def destroy_removed_tiny_mce_assets(ids, ref)
|
||||||
|
# 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
|
ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -4,6 +4,73 @@ namespace :tiny_mce_asset do
|
||||||
task remove_obsolete_images: :environment do
|
task remove_obsolete_images: :environment do
|
||||||
TinyMceAsset.where('step_id IS ? AND ' \
|
TinyMceAsset.where('step_id IS ? AND ' \
|
||||||
'result_text_id IS ? AND created_at < ?',
|
'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 regenerate_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 && step.description.match(regex)
|
||||||
|
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(result_text_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 && result_text.text.match(regex)
|
||||||
|
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(result_text_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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue