Sha256: c1ec1bce727674cd738782a60e6a8decb6fff978c31fe7227694d26b729ad05e

Contents?: true

Size: 1.29 KB

Versions: 30

Compression:

Stored size: 1.29 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, :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

30 entries across 30 versions & 2 rubygems

Version Path
decidim-core-0.6.8 app/models/decidim/attachment.rb
decidim-0.6.8 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.7 app/models/decidim/attachment.rb
decidim-0.6.7 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.6 app/models/decidim/attachment.rb
decidim-0.6.6 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.5 app/models/decidim/attachment.rb
decidim-0.6.5 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.4 app/models/decidim/attachment.rb
decidim-0.6.4 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.3 app/models/decidim/attachment.rb
decidim-0.6.3 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.2 app/models/decidim/attachment.rb
decidim-0.6.2 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.1 app/models/decidim/attachment.rb
decidim-0.6.1 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.6.0 app/models/decidim/attachment.rb
decidim-0.6.0 decidim-core/app/models/decidim/attachment.rb
decidim-core-0.5.1 app/models/decidim/attachment.rb
decidim-0.5.1 decidim-core/app/models/decidim/attachment.rb