Sha256: 9675602d7da05d29b875ef473c06bf06cd89b7427eb6004923fe060a407fa9ca

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

class Attachment < ApplicationRecord
  include Dis::Model
  include PagesCore::Sweepable

  belongs_to :user, optional: true

  validates_data_presence
  validates :content_type, presence: true
  validates :filename, presence: true
  validates :content_length, presence: true

  localizable do
    attribute :name
    attribute :description
  end

  before_validation :set_name_from_filename

  class << self
    def verifier
      @verifier ||= PagesCore::DigestVerifier.new(
        Rails.application.key_generator.generate_key("attachments")
      )
    end

    def formats
      { "audio/mpeg" => :mp3,
        "image/gif" => :gif,
        "image/jpeg" => :jpg,
        "image/jpg" => :jpg,
        "image/pjpeg" => :jpg,
        "image/png" => :png,
        "application/pdf" => :pdf }
    end
  end

  def digest
    return unless id

    self.class.verifier.generate(id.to_s)
  end

  def format?
    content_type && self.class.formats.key?(content_type)
  end

  def format
    self.class.formats[content_type]
  end

  def filename_extension
    return "" unless filename_extension?

    filename.match(/\.([^.]+)$/)[1]
  end

  def filename_extension?
    filename.include?(".")
  end

  # Includes a timestamp fingerprint in the URL param, so
  # that rendered images can be cached indefinitely.
  def to_param
    [id, updated_at.utc.to_fs(cache_timestamp_format)].join("-")
  end

  private

  def set_name_from_filename
    self.name ||= File.basename(filename, ".*") if filename? && locale
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/models/attachment.rb