闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
559 B

  1. # frozen_string_literal: true
  2. class PreviewCard < ApplicationRecord
  3. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  4. belongs_to :status
  5. has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
  6. validates :url, presence: true
  7. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  8. validates_attachment_size :image, less_than: 1.megabytes
  9. def save_with_optional_image!
  10. save!
  11. rescue ActiveRecord::RecordInvalid
  12. self.image = nil
  13. save!
  14. end
  15. end