Sha256: 2f360483051136736bae16fb155e6a667018710d7c127c634dde6ea9c80caa2f

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 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 :attached_to, polymorphic: true

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

    # 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.
    def url
      file.url
    end

    # 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

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.1.0 app/models/decidim/attachment.rb
decidim-core-0.0.8.1 app/models/decidim/attachment.rb
decidim-core-0.0.7 app/models/decidim/attachment.rb
decidim-core-0.0.6 app/models/decidim/attachment.rb
decidim-core-0.0.5 app/models/decidim/attachment.rb
decidim-core-0.0.3 app/models/decidim/attachment.rb