Tony Messias

Joined

910 Experience
9 Lessons Completed
0 Questions Solved

Activity

Posted in How To Update ActionText Attachments Discussion

If the app key changed, the locator wouldn't be able to verify the SGID, so that would return null. I think you'd need to create a new verifier that still uses the previous key, so something like:

GlobalID::Locator.locate_signed(
  node.attributes['sgid'].value,
  verifier: Verifier.new(oldVerifierHash),
  for: "attachable"
)

Then you could use the replace method from the Fragment and do something like:

ActionText::RichText.where.not(body: nil).find_each do |trix|
  next unless trix.embeds.size.positive?

  trix.update_column :body, trix.body.fragment.replace("action-text-attachment").each do |node|
    return ActionText::Attachment.from_attachable(
      GlobalID::Locator.locate_signed(
        node.attributes['sgid'].value,
        verifier: Verifier.new(oldVerifierHash),
        for: "attachable"
      ),
      node.attributes
    )
  end.to_s
end

Sorry, this is pseudo-code, I don't write Ruby that often. This code was actually a port of a piece of code I wrote using my PHP-port of the ActionText gem, here's a gist:

https://gist.github.com/tonysm/36d2c413b479b29741af9613fb9808ad

There might be better ways, this was just my first take on solving it.

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2023 GoRails, LLC. All rights reserved.