Skip to content

Instantly share code, notes, and snippets.

@tonysm
Last active July 13, 2022 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonysm/36d2c413b479b29741af9613fb9808ad to your computer and use it in GitHub Desktop.
Save tonysm/36d2c413b479b29741af9613fb9808ad to your computer and use it in GitHub Desktop.
Migrate Stored Hashes Using Rich Text Laravel
# Pseudo-code based on the PHP version...
namespace :action_text do
task refresh_embeds: :environment do
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[sgid]") 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
end
<?php
use Illuminate\Support\Facades\Artisan;
use Tonysm\GlobalId\Facades\Locator;
use Tonysm\GlobalId\Verifier;
use Tonysm\RichTextLaravel\Attachment;
use Tonysm\RichTextLaravel\Models\RichText;
Artisan::command('richtext:rotate', function () {
$oldVerifier = new Verifier(
fn () => config('rich-text-laravel.old_key'),
salt: 'signed_global_ids',
);
RichText::query()->oldest()->eachById(function (RichText $entry) use ($oldVerifier) {
$entry->forceFill([
'body' => $entry->body->fragment->replace('//rich-text-attachment[@sgid]', function (DOMElement $node) use ($oldVerifier) {
$attributes = [];
foreach ($node->attributes as $attr) {
$attributes[$attr->name] = $attr->value;
}
return Attachment::fromAttachable(Locator::locateSigned($node->getAttribute('sgid'), [
'for' => 'rich-text-laravel',
'verifier' => $oldVerifier,
]), $attributes);
})->toHtml(),
])->save();
});
})->purpose('Loops through the Rich Text Models and migrates the attachments.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment