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

50 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: preview_cards
  5. #
  6. # id :integer not null, primary key
  7. # status_id :integer
  8. # url :string default(""), not null
  9. # title :string
  10. # description :string
  11. # image_file_name :string
  12. # image_content_type :string
  13. # image_file_size :integer
  14. # image_updated_at :datetime
  15. # created_at :datetime not null
  16. # updated_at :datetime not null
  17. # type :integer default("link"), not null
  18. # html :text default(""), not null
  19. # author_name :string default(""), not null
  20. # author_url :string default(""), not null
  21. # provider_name :string default(""), not null
  22. # provider_url :string default(""), not null
  23. # width :integer default(0), not null
  24. # height :integer default(0), not null
  25. #
  26. class PreviewCard < ApplicationRecord
  27. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  28. self.inheritance_column = false
  29. enum type: [:link, :photo, :video, :rich]
  30. belongs_to :status
  31. has_attached_file :image, styles: { original: '120x120#' }, convert_options: { all: '-quality 80 -strip' }
  32. include Attachmentable
  33. validates :url, presence: true
  34. validates_attachment_content_type :image, content_type: IMAGE_MIME_TYPES
  35. validates_attachment_size :image, less_than: 1.megabytes
  36. def save_with_optional_image!
  37. save!
  38. rescue ActiveRecord::RecordInvalid
  39. self.image = nil
  40. save!
  41. end
  42. end