Created
January 18, 2022 17:52
-
-
Save fabiode/f79faf59e27a0d07d5e60c7e91b3bcc6 to your computer and use it in GitHub Desktop.
Revisions
-
fabiode revised this gist
Jan 18, 2022 . 2 changed files with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ ### Example of Use class User < ApplicationRecord include ImageVariants has_one_attached :avatar This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -55,5 +55,4 @@ def variant_image(size) end end -
fabiode created this gist
Jan 18, 2022 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ class User < ApplicationRecord include ImageVariants has_one_attached :avatar image_variants_for :avatar, sizes: { custom_size: }, default_image: '/path/to/default/image' end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ require 'active_support/concern' module ImageVariants extend ActiveSupport::Concern class_methods do attr_reader :_attachment_name, :_image_sizes, :_default_image def image_variants_for(name, sizes: _default_sizes, default_image: _image) @_attachment_name = name @_image_sizes = sizes @_default_image = default_image end def _default_sizes { avatar: { combine_options: { resize: '80x80^', extent: '80x80', gravity: 'Center' } }, thumb: { combine_options: { resize: '256x256^', crop: '128x128+0-10', gravity: 'Center' } }, card: { combine_options: { resize: '640x360^', crop: '640x360+0+0', gravity: 'Center' } }, cover: { combine_options: { resize: '1280x720^', crop: '1280x720+0+0', gravity: 'Center' } } } end def _image '/default.png' end end included do after_initialize do define_singleton_method("#{self.class._attachment_name}_image") do |size = nil| _attachment.attached? ? variant_image(size) : self.class._default_image end end end private def image_variants(size) self.class._image_sizes[size.to_sym] end def _attachment public_send(self.class._attachment_name) end def variant_image(size) size = image_variants(size) if size && _attachment.blob.variable? variant = _attachment.variant(size) variant.processed.service_url else _attachment.service_url end end end ### Example of Use