2017-04-25 20:24:05 +08:00
|
|
|
namespace :tiny_mce_asset do
|
2018-01-05 22:37:58 +08:00
|
|
|
REGEX = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/
|
2017-04-25 20:24:05 +08:00
|
|
|
desc 'Remove obsolete images that were created on new steps or '\
|
|
|
|
'results and the step/result didn\'t get saved.'
|
2017-05-08 19:03:35 +08:00
|
|
|
task remove_obsolete_images: :environment do
|
2017-04-25 20:24:05 +08:00
|
|
|
TinyMceAsset.where('step_id IS ? AND ' \
|
|
|
|
'result_text_id IS ? AND created_at < ?',
|
2017-11-28 17:09:06 +08:00
|
|
|
nil, nil, 7.days.ago).destroy_all
|
2017-04-25 20:24:05 +08:00
|
|
|
end
|
2017-11-24 23:25:12 +08:00
|
|
|
|
2018-01-05 20:23:58 +08:00
|
|
|
desc 'Generate new tiny_mce_assets and replace old assets in RTE for ' \
|
|
|
|
'steps. Assign the last printed id if the script crashes or ' \
|
|
|
|
'id + 1 if there is a problematic asset'
|
|
|
|
task :regenerate_step_images, [:last_id] => :environment do |_, args|
|
2017-11-24 23:25:12 +08:00
|
|
|
replaced_images = 0
|
|
|
|
failed_attempts = 0
|
2018-01-05 22:37:58 +08:00
|
|
|
all_images = TinyMceAsset.where.not(step: nil).count
|
2017-11-24 23:25:12 +08:00
|
|
|
failed_attempts_ids = []
|
|
|
|
puts 'Start processing steps...'
|
2018-01-05 20:23:58 +08:00
|
|
|
params = { batch_size: 100 }
|
|
|
|
if args.present? && args[:last_id].present?
|
|
|
|
# fetch all steps and sort them asc
|
|
|
|
params[:start] = args[:last_id].to_i
|
|
|
|
end
|
2018-01-05 22:37:58 +08:00
|
|
|
Step.find_each(params) do |step|
|
|
|
|
next unless step.description && step.description.match(REGEX)
|
2017-11-24 23:25:12 +08:00
|
|
|
team = step.protocol.team
|
2018-01-05 20:23:58 +08:00
|
|
|
puts "******************************* \n\n\n\n"
|
|
|
|
puts "Processing step id => [#{step.id}] \n\n\n\n"
|
|
|
|
puts '*******************************'
|
2018-01-05 22:37:58 +08:00
|
|
|
step.description.gsub!(REGEX) do |el|
|
|
|
|
match = el.match(REGEX)
|
2017-11-24 23:25:12 +08:00
|
|
|
old_img = TinyMceAsset.find_by_id(match[1])
|
2018-01-04 21:49:52 +08:00
|
|
|
# skip other processing and deletes tiny_mce tag
|
|
|
|
# if image is not in database
|
|
|
|
next unless old_img
|
2017-11-24 23:25:12 +08:00
|
|
|
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
|
2017-11-28 17:09:06 +08:00
|
|
|
old_img.update_attributes(result_text_id: nil, step_id: nil)
|
2017-11-24 23:25:12 +08:00
|
|
|
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...'
|
2018-01-05 20:23:58 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
desc 'Generate new tiny_mce_assets and replace old assets in RTE ' \
|
|
|
|
'for results. Assign the last printed id if the script crashes or ' \
|
|
|
|
'id + 1 if there is a problematic asset'
|
|
|
|
task :regenerate_results_images, [:last_id] => :environment do |_, args|
|
|
|
|
replaced_images = 0
|
|
|
|
failed_attempts = 0
|
2018-01-05 22:37:58 +08:00
|
|
|
all_images = TinyMceAsset.where.not(result_text: nil).count
|
2018-01-05 20:23:58 +08:00
|
|
|
failed_attempts_ids = []
|
|
|
|
params = { batch_size: 100 }
|
|
|
|
if args.present? && args[:last_id].present?
|
|
|
|
params[:start] = args[:last_id].to_i
|
|
|
|
end
|
|
|
|
|
2017-11-24 23:25:12 +08:00
|
|
|
puts 'Start processing result_texts...'
|
2018-01-05 20:23:58 +08:00
|
|
|
ResultText.find_each(params) do |result_text|
|
2018-01-05 22:37:58 +08:00
|
|
|
next unless result_text.text && result_text.text.match(REGEX)
|
2017-11-24 23:25:12 +08:00
|
|
|
team = result_text.result.my_module.protocol.team
|
2018-01-05 20:23:58 +08:00
|
|
|
puts "******************************************* \n\n\n\n"
|
|
|
|
puts "Processing result_text id => [#{result_text.id}] \n\n\n\n"
|
|
|
|
puts '*******************************************'
|
2018-01-05 22:37:58 +08:00
|
|
|
result_text.text.gsub!(REGEX) do |el|
|
|
|
|
match = el.match(REGEX)
|
2017-11-24 23:25:12 +08:00
|
|
|
old_img = TinyMceAsset.find_by_id(match[1])
|
2018-01-04 21:49:52 +08:00
|
|
|
# skip other processing and deletes tiny_mce tag
|
|
|
|
# if image is not in database
|
|
|
|
next unless old_img
|
2017-11-24 23:25:12 +08:00
|
|
|
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
|
2017-11-28 17:09:06 +08:00
|
|
|
old_img.update_attributes(result_text_id: nil, step_id: nil)
|
2017-11-24 23:25:12 +08:00
|
|
|
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
|
2017-04-25 20:24:05 +08:00
|
|
|
end
|