Sha256: 7842a73cbcbb068df3cbbdcde5ca73b2136de3572013ee52c79b8e3182134bb5

Contents?: true

Size: 1.44 KB

Versions: 26

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Decidim
  # Attachment can be any type of document or images related to a partcipatory
  # process.
  class Attachment < ApplicationRecord
    belongs_to :attachment_collection, class_name: "Decidim::AttachmentCollection", optional: true
    belongs_to :attached_to, polymorphic: true

    validates :file, :content_type, presence: true
    validates :file, file_size: { less_than_or_equal_to: ->(_attachment) { Decidim.maximum_attachment_size } }
    mount_uploader :file, Decidim::AttachmentUploader

    default_scope { order(arel_table[:weight].asc) }

    # Whether this attachment is a photo or not.
    #
    # Returns Boolean.
    def photo?
      @photo ||= content_type.start_with? "image"
    end

    # Whether this attachment is a document or not.
    #
    # Returns Boolean.
    def document?
      !photo?
    end

    # Which kind of file this is.
    #
    # Returns String.
    def file_type
      file.url&.split(".")&.last&.downcase
    end

    # The URL to download the file.
    #
    # Returns String.
    delegate :url, to: :file

    # The URL to download the thumbnail of the file. Only works with images.
    #
    # Returns String.
    def thumbnail_url
      return unless photo?
      file.thumbnail.url
    end

    # The URL to download the a big version of the file. Only works with images.
    #
    # Returns String.
    def big_url
      return unless photo?
      file.big.url
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
decidim-core-0.11.2 app/models/decidim/attachment.rb
decidim-core-0.12.0.pre app/models/decidim/attachment.rb
decidim-core-0.11.1 app/models/decidim/attachment.rb
decidim-core-0.11.0.pre1 app/models/decidim/attachment.rb
decidim-core-0.10.1 app/models/decidim/attachment.rb
decidim-core-0.10.0 app/models/decidim/attachment.rb