mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-06 23:15:34 +08:00
Merge pull request #924 from ZmagoD/tiny_mce_asset_nil_guard
Nil image guard [fixes SCI-1907]
This commit is contained in:
commit
a2280a4075
1 changed files with 53 additions and 12 deletions
|
@ -1,4 +1,5 @@
|
||||||
namespace :tiny_mce_asset do
|
namespace :tiny_mce_asset do
|
||||||
|
REGEX = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/
|
||||||
desc 'Remove obsolete images that were created on new steps or '\
|
desc 'Remove obsolete images that were created on new steps or '\
|
||||||
'results and the step/result didn\'t get saved.'
|
'results and the step/result didn\'t get saved.'
|
||||||
task remove_obsolete_images: :environment do
|
task remove_obsolete_images: :environment do
|
||||||
|
@ -7,20 +8,32 @@ namespace :tiny_mce_asset do
|
||||||
nil, nil, 7.days.ago).destroy_all
|
nil, nil, 7.days.ago).destroy_all
|
||||||
end
|
end
|
||||||
|
|
||||||
desc 'Generate new tiny_mce_assets and replace old assets in RTE'
|
desc 'Generate new tiny_mce_assets and replace old assets in RTE for ' \
|
||||||
task regenerate_images: :environment do
|
'steps. Assign the last printed id if the script crashes or ' \
|
||||||
regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/
|
'id + 1 if there is a problematic asset'
|
||||||
|
task :regenerate_step_images, [:last_id] => :environment do |_, args|
|
||||||
replaced_images = 0
|
replaced_images = 0
|
||||||
failed_attempts = 0
|
failed_attempts = 0
|
||||||
all_images = TinyMceAsset.count
|
all_images = TinyMceAsset.where.not(step: nil).count
|
||||||
failed_attempts_ids = []
|
failed_attempts_ids = []
|
||||||
puts 'Start processing steps...'
|
puts 'Start processing steps...'
|
||||||
Step.find_each do |step|
|
params = { batch_size: 100 }
|
||||||
next unless step.description && step.description.match(regex)
|
if args.present? && args[:last_id].present?
|
||||||
|
# fetch all steps and sort them asc
|
||||||
|
params[:start] = args[:last_id].to_i
|
||||||
|
end
|
||||||
|
Step.find_each(params) do |step|
|
||||||
|
next unless step.description && step.description.match(REGEX)
|
||||||
team = step.protocol.team
|
team = step.protocol.team
|
||||||
step.description.gsub!(regex) do |el|
|
puts "******************************* \n\n\n\n"
|
||||||
match = el.match(regex)
|
puts "Processing step id => [#{step.id}] \n\n\n\n"
|
||||||
|
puts '*******************************'
|
||||||
|
step.description.gsub!(REGEX) do |el|
|
||||||
|
match = el.match(REGEX)
|
||||||
old_img = TinyMceAsset.find_by_id(match[1])
|
old_img = TinyMceAsset.find_by_id(match[1])
|
||||||
|
# skip other processing and deletes tiny_mce tag
|
||||||
|
# if image is not in database
|
||||||
|
next unless old_img
|
||||||
new_img = TinyMceAsset.create(image: old_img.image,
|
new_img = TinyMceAsset.create(image: old_img.image,
|
||||||
team: team,
|
team: team,
|
||||||
reference: step)
|
reference: step)
|
||||||
|
@ -40,13 +53,41 @@ namespace :tiny_mce_asset do
|
||||||
step.save
|
step.save
|
||||||
end
|
end
|
||||||
puts 'Completed processing steps...'
|
puts 'Completed processing steps...'
|
||||||
|
|
||||||
|
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
|
||||||
|
all_images = TinyMceAsset.where.not(result_text: nil).count
|
||||||
|
failed_attempts_ids = []
|
||||||
|
params = { batch_size: 100 }
|
||||||
|
if args.present? && args[:last_id].present?
|
||||||
|
params[:start] = args[:last_id].to_i
|
||||||
|
end
|
||||||
|
|
||||||
puts 'Start processing result_texts...'
|
puts 'Start processing result_texts...'
|
||||||
ResultText.find_each do |result_text|
|
ResultText.find_each(params) do |result_text|
|
||||||
next unless result_text.text && result_text.text.match(regex)
|
next unless result_text.text && result_text.text.match(REGEX)
|
||||||
team = result_text.result.my_module.protocol.team
|
team = result_text.result.my_module.protocol.team
|
||||||
result_text.text.gsub!(regex) do |el|
|
puts "******************************************* \n\n\n\n"
|
||||||
match = el.match(regex)
|
puts "Processing result_text id => [#{result_text.id}] \n\n\n\n"
|
||||||
|
puts '*******************************************'
|
||||||
|
result_text.text.gsub!(REGEX) do |el|
|
||||||
|
match = el.match(REGEX)
|
||||||
old_img = TinyMceAsset.find_by_id(match[1])
|
old_img = TinyMceAsset.find_by_id(match[1])
|
||||||
|
# skip other processing and deletes tiny_mce tag
|
||||||
|
# if image is not in database
|
||||||
|
next unless old_img
|
||||||
new_img = TinyMceAsset.create(image: old_img.image,
|
new_img = TinyMceAsset.create(image: old_img.image,
|
||||||
team: team,
|
team: team,
|
||||||
reference: result_text)
|
reference: result_text)
|
||||||
|
|
Loading…
Reference in a new issue