preview_card_provider.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: preview_card_providers
  5. #
  6. # id :bigint(8) not null, primary key
  7. # domain :string default(""), not null
  8. # icon_file_name :string
  9. # icon_content_type :string
  10. # icon_file_size :bigint(8)
  11. # icon_updated_at :datetime
  12. # trendable :boolean
  13. # reviewed_at :datetime
  14. # requested_review_at :datetime
  15. # created_at :datetime not null
  16. # updated_at :datetime not null
  17. #
  18. class PreviewCardProvider < ApplicationRecord
  19. include Paginable
  20. include DomainNormalizable
  21. include Attachmentable
  22. include Reviewable
  23. ICON_MIME_TYPES = %w(image/x-icon image/vnd.microsoft.icon image/png).freeze
  24. LIMIT = 1.megabyte
  25. validates :domain, presence: true, uniqueness: true, domain: true
  26. has_attached_file :icon, styles: { static: { format: 'png', convert_options: '-coalesce +profile "!icc,*" +set date:modify +set date:create +set date:timestamp' } }, validate_media_type: false
  27. validates_attachment :icon, content_type: { content_type: ICON_MIME_TYPES }, size: { less_than: LIMIT }
  28. remotable_attachment :icon, LIMIT
  29. scope :trendable, -> { where(trendable: true) }
  30. scope :not_trendable, -> { where(trendable: false) }
  31. def self.matching_domain(domain)
  32. segments = domain.split('.')
  33. where(domain: segments.map.with_index { |_, i| segments[i..].join('.') }).by_domain_length.first
  34. end
  35. end