Skip to content

Instantly share code, notes, and snippets.

@tonysm
Last active Jul 13, 2022
Embed
What would you like to do?
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