From 9b16c523398f6a8b8d6546ced1439a23c5801ed8 Mon Sep 17 00:00:00 2001 From: Fernando Shayani Date: Mon, 9 Oct 2023 15:03:50 -0300 Subject: [PATCH] fix(Events): Rake task to refresh trix attachments on demand --- .../refresh_action_text_attachments.rake | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/tasks/refresh_action_text_attachments.rake diff --git a/lib/tasks/refresh_action_text_attachments.rake b/lib/tasks/refresh_action_text_attachments.rake new file mode 100644 index 00000000..e6df06ce --- /dev/null +++ b/lib/tasks/refresh_action_text_attachments.rake @@ -0,0 +1,27 @@ +namespace :action_text do + desc "Refresh action text attachments" + task refresh_attachments: :environment do + def refresh_trix(trix) + return unless trix.embeds.size.positive? + + trix.body.fragment.find_all("action-text-attachment").each do |node| + embed = trix.embeds.find do |attachment| + attachment.filename.to_s == node["filename"] && attachment.byte_size.to_s == node["filesize"] + end + + node.attributes["url"].value = Rails.application.routes.url_helpers.rails_storage_redirect_url(embed.blob) + node.attributes["sgid"].value = embed.attachable_sgid + end + + trix.update_column :body, trix.body.to_s + end + + ActionText::RichText.where.not(body: nil).find_each do |trix| + print "Processing ActionText::RichText #{trix.id} \r" + refresh_trix(trix) + rescue => e + puts "ActionText::RichText #{trix.id}: #{e.message}" + next + end + end +end