Skip to content

Instantly share code, notes, and snippets.

@j-dexx
Created June 18, 2020 13:25
Show Gist options
  • Select an option

  • Save j-dexx/b7028ac581978ea1cb54846fc7ca8be3 to your computer and use it in GitHub Desktop.

Select an option

Save j-dexx/b7028ac581978ea1cb54846fc7ca8be3 to your computer and use it in GitHub Desktop.
Calculating paperclip attachment url
/**
* https://www.rubydoc.info/github/thoughtbot/paperclip/Paperclip%2FInterpolations:id_partition
*/
protected function calculateIdPartition(int $modelId): string
{
$idString = $this->convertIntegerToString($modelId);
$zeroPaddedString = str_pad($idString, 9, "0", STR_PAD_LEFT);
$parts = str_split($zeroPaddedString, 3);
return implode('/', $parts);
}
/**
* https://www.rubydoc.info/gems/paperclip/Paperclip%2FClassMethods:has_attached_file
*
* The default value is “/system/:class/:attachment/:id_partition/:style/:filename
*
* Ruby class name would just be the base class name e.g. NewsArticle not App/Models/NewsArticle
* It looks like the attachment is pluralized
* For style we'll use original to get the original uploaded image
*/
protected function calculateAttachmentUrl(string $rubyClassName, string $attachmentName, int $modelId, string $filename): string
{
$pluralizedClassName = Str::plural(Str::snake($rubyClassName));
$idPartition = $this->calculateIdPartition($modelId);
$pluralizedAttachementName = Str::plural($attachmentName);
$filename = urlencode($filename);
$parts = [
'https://orbit.brightbox.com/v1/acc-jqzwj/Marquis-Leisure',
$pluralizedClassName,
$pluralizedAttachementName,
$idPartition,
'original',
$filename,
];
return implode('/', $parts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment