1
0

account_header.rb 877 B

1234567891011121314151617181920212223242526272829303132
  1. # frozen_string_literal: true
  2. module AccountHeader
  3. extend ActiveSupport::Concern
  4. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  5. class_methods do
  6. def header_styles(file)
  7. styles = { original: '700x335#' }
  8. styles[:static] = { format: 'png', convert_options: '-coalesce' } if file.content_type == 'image/gif'
  9. styles
  10. end
  11. private :header_styles
  12. end
  13. included do
  14. # Header upload
  15. has_attached_file :header, styles: ->(f) { header_styles(f) }, convert_options: { all: '-quality 80 -strip' }
  16. validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
  17. validates_attachment_size :header, less_than: 2.megabytes
  18. end
  19. def header_original_url
  20. header.url(:original)
  21. end
  22. def header_static_url
  23. header_content_type == 'image/gif' ? header.url(:static) : header_original_url
  24. end
  25. end