闭社主体 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.

24 lines
639 B

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