Created
February 8, 2018 09:35
-
-
Save gregogalante/7f715e6eeeef2f2af81c484c9d58bdc1 to your computer and use it in GitHub Desktop.
Set custom Paperclip attachments style based on specific attribute extra value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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