BigW Consortium Gitlab

appearance.rb 916 Bytes
Newer Older
1
class Appearance < ActiveRecord::Base
2 3 4 5
  include CacheMarkdownField

  cache_markdown_field :description

6 7 8 9 10
  validates :title,       presence: true
  validates :description, presence: true
  validates :logo,        file_size: { maximum: 1.megabyte }
  validates :header_logo, file_size: { maximum: 1.megabyte }

11 12
  validate :single_appearance_row, on: :create

13 14
  mount_uploader :logo,         AttachmentUploader
  mount_uploader :header_logo,  AttachmentUploader
15
  has_many :uploads, as: :model, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

  CACHE_KEY = 'current_appearance'.freeze

  after_commit :flush_redis_cache

  def self.current
    Rails.cache.fetch(CACHE_KEY) { first }
  end

  def flush_redis_cache
    Rails.cache.delete(CACHE_KEY)
  end

  def single_appearance_row
    if self.class.any?
      errors.add(:single_appearance_row, 'Only 1 appearances row can exist')
    end
  end
34
end