preview_card_provider.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. ICON_MIME_TYPES = %w(image/x-icon image/vnd.microsoft.icon image/png).freeze
  23. LIMIT = 1.megabyte
  24. validates :domain, presence: true, uniqueness: true, domain: true
  25. 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
  26. validates_attachment :icon, content_type: { content_type: ICON_MIME_TYPES }, size: { less_than: LIMIT }
  27. remotable_attachment :icon, LIMIT
  28. scope :trendable, -> { where(trendable: true) }
  29. scope :not_trendable, -> { where(trendable: false) }
  30. scope :reviewed, -> { where.not(reviewed_at: nil) }
  31. scope :pending_review, -> { where(reviewed_at: nil) }
  32. def requires_review?
  33. reviewed_at.nil?
  34. end
  35. def reviewed?
  36. reviewed_at.present?
  37. end
  38. def requested_review?
  39. requested_review_at.present?
  40. end
  41. def requires_review_notification?
  42. requires_review? && !requested_review?
  43. end
  44. def self.matching_domain(domain)
  45. segments = domain.split('.')
  46. where(domain: segments.map.with_index { |_, i| segments[i..].join('.') }).by_domain_length.first
  47. end
  48. end