account_avatar.rb 994 B

1234567891011121314151617181920212223242526272829303132
  1. # frozen_string_literal: true
  2. module AccountAvatar
  3. extend ActiveSupport::Concern
  4. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  5. class_methods do
  6. def avatar_styles(file)
  7. styles = { original: { geometry: '120x120#', file_geometry_parser: FastGeometryParser } }
  8. styles[:static] = { format: 'png', convert_options: '-coalesce', file_geometry_parser: FastGeometryParser } if file.content_type == 'image/gif'
  9. styles
  10. end
  11. private :avatar_styles
  12. end
  13. included do
  14. # Avatar upload
  15. has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail]
  16. validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
  17. validates_attachment_size :avatar, less_than: 2.megabytes
  18. end
  19. def avatar_original_url
  20. avatar.url(:original)
  21. end
  22. def avatar_static_url
  23. avatar_content_type == 'image/gif' ? avatar.url(:static) : avatar_original_url
  24. end
  25. end