Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionText::Richtext attachments are broken after upgrading to 1.2.0 #177

Open
tsrivishnu opened this issue Mar 6, 2024 · 1 comment
Open

Comments

@tsrivishnu
Copy link

tsrivishnu commented Mar 6, 2024

The attachments in ActionText::RichText rely on attachable_gid. The ActionText::Attachable#attachable_sgid value on objects returns different values based for version 1.1.0 and 1.2.0

Example:

on v1.1.0

pry(main)> SignedGlobalID.create(OpenStruct.new(id: 1), { for: :attachable}).to_s
=> "BAh7CEkiCGdpZAY6BkVUSSIbZ2lkOi8vYXBwL09wZW5TdHJ1Y3QvMQY7AFRJIgxwdXJwb3NlBjsAVDoPYXR0YWNoYWJsZUkiD2V4cGlyZXNfYXQGOwBUSSIdMjAyNC0wNC0wNlQxNTo0NToyMi43MjJaBjsAVA==--ce7d34e5c08faa96507efeb501acd43da0e622d3"

on v1.2.0

irb(main):001:0> SignedGlobalID.create(OpenStruct.new(id: 1), { for: :attachable}).to_s
=> "eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaHRuYVdRNkx5OWhjSEF2VDNCbGJsTjBjblZqZEM4eEJqb0dSVlE9IiwiZXhwIjoiMjAyNC0wNC0wNlQxNTo0NjoyMi4wMTdaIiwicHVyIjoiYXR0YWNoYWJsZSJ9fQ==--440bcdc49d8a6390eb8793f2c4e0b310c6587ac1"

This breaks are the attachments in the rich text editor for us.

What would be the best way to make sure we get the same attachable_sgid?

@tsrivishnu
Copy link
Author

Looks like the attachments are not necessarily broken. ActionText is able to find the attachment records with either of the sgids.

The issue is with how we implemented the serialisation and rendering of the rich text. We serialise the attachments separately from the ActionText::Richtext body and match them the sgid in the richtext with the sgid of the separately serialised objects. The objects are serialised with ActionText::Attachable#attachable_sgid.

All the attachments that were attached before v1.2.0 are using the previous sgid in the rich text and the serialised objects are returning a newer sgids. Therefore, our frontend couldn't match them to render appropriately.

I would like to understand why this is happening and if we can make ActionText::Attachable#attachable_sgid to return the old format?

--
For those who might land here, I managed to update the references in the richtext by running the following to update all the attachments in rich text.

require_relative "config/environment"

updated_attachments = []
ActionText::RichText.where.not(body: nil).find_each do |trix|
  next unless trix.body.attachments.size.positive?

  trix.body.fragment.find_all("action-text-attachment").each do |node|
    attachment = ActionText::Attachment.from_node(node)
    next if !attachment.attachable.is_a?(SectionItem)
    puts "attachment: #{attachment}"

    if node.attributes["sgid"].value != attachment.attachable_sgid
      updated_attachments << [
        trix.id,
        node.attributes["sgid"].value,
        attachment.attachable_sgid, # new sgid
        attachment.attachable.class.name,
        attachment.attachable.id,
      ]
    end

    node.attributes["sgid"].value = attachment.attachable_sgid
  end

  trix.update_column :body, trix.body.to_s
end

File.open("updated_attachments.csv", "w") do |f|
  f.puts "trix_id,old_sgid,new_sgid,attachable_type,attachable_id"
  updated_attachments.each { |row| f.puts(row.join(",")) }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant