Adding polymorphic image styles using paperclip
Paperclip is one my favorite gem which makes life so easy to upload and process images. Within an application we have a lot of model having images like Product has many pictures, User has many pictures. Now think about a situation where thumb style for the product has size 300> and User model need a size for the thumb is 200>. Here is a quick tip if you want to style the images based on the model.
Before
Now we need to modify this model to support polymorphic and styles based on the model. Here is the new code.
So this piece of small code will resize you images based upon the model it belongs to. If that image belongs to product then “thumb” will be resize in the format of 300>, else it will be 200. Medium style will be always 500> i.e. independent of model it belongs to. Pretty nice and simple.
Now from previous learning of image optimization i.e. How to add cache-control, expires headers to images (Content) served by S3 and Save image as the progressive image using paperclip and imageMagick, here is the final piece of code.