Skip to content

Instantly share code, notes, and snippets.

@brenogazzola
Last active February 14, 2022 20:29
Show Gist options
  • Save brenogazzola/8b636689d6a4eda76cfa1f370e6b9a1c to your computer and use it in GitHub Desktop.
Save brenogazzola/8b636689d6a4eda76cfa1f370e6b9a1c to your computer and use it in GitHub Desktop.
Fix ActionText Images
# After active storage urls are changed, use this to recreate all trix attachments
def self.refresh_trixes
ActionText::RichText.where.not(body: nil).find_each do |trix|
Trix::RefreshJob.perform_later(trix)
end
end
# After active storage urls are changed, use this to recreate a specific strix attachments
def self.refresh_trix(trix)
return unless trix.embeds.size.positive?
trix.body.fragment.find_all("action-text-attachment").each do |node|
embed = trix.embeds.find { |attachment| attachment.filename.to_s == node["filename"] && attachment.byte_size.to_s == node["filesize"] }
node.attributes["url"].value = Rails.application.routes.url_helpers.rails_storage_redirect_url(embed.blob, host: "example.com")
node.attributes["sgid"].value = embed.attachable_sgid
end
trix.update_column :body, trix.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment