Skip to content

Commit

Permalink
fix(Events): Rake task to refresh trix attachments on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
shayani committed Oct 9, 2023
1 parent 2287014 commit 9b16c52
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 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

0 comments on commit 9b16c52

Please sign in to comment.