preview_card_provider.rb 1.7 KB

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