Skip to content

Instantly share code, notes, and snippets.

@gregogalante
Created February 8, 2018 09:35
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 gregogalante/7f715e6eeeef2f2af81c484c9d58bdc1 to your computer and use it in GitHub Desktop.
Save gregogalante/7f715e6eeeef2f2af81c484c9d58bdc1 to your computer and use it in GitHub Desktop.
Set custom Paperclip attachments style based on specific attribute extra value.
class Attachment < ApplicationRecord
has_attached_file :file,
styles: ->(a) { a.instance.paperclip_styles },
path: 'public/system/:class/:id/:style_:filename',
url: '/system/:class/:id/:style_:filename'
validates_attachment_presence :file
def paperclip_styles
case typology
when 'profile'
{
thumb: '200x200>'
}
when 'cover'
{
thumb: '200x200>'
medium: '600x400>'
}
when 'post_image'
{
medium: '600x400>'
}
else
{}
end
end
after_create do
file.reprocess!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment