Sha256: 3c343329adcdf2ccb4038460fa7f6b203afe4b41f18b2918a6df3afc0e60090c

Contents?: true

Size: 1.84 KB

Versions: 28

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # A concern with the components needed when you want a model to be able to create
  # links from it to another resource.
  module HasAttachments
    extend ActiveSupport::Concern

    included do
      has_many :attachments,
               class_name: "Decidim::Attachment",
               dependent: :destroy,
               inverse_of: :attached_to,
               as: :attached_to

      # The first attachment that is a photo for this model.
      #
      # Returns an Attachment
      def photo
        @photo ||= photos.first
      end

      # All the attachments that are photos for this model.
      #
      # Returns an Array<Attachment>
      def photos
        @photos ||= attachments.with_attached_file.order(:weight).select(&:photo?)
      end

      # All the attachments that are documents for this model.
      #
      # Returns an Array<Attachment>
      def documents
        @documents ||= attachments.with_attached_file.order(:weight).includes(:attachment_collection).select(&:document?)
      end

      # All the attachments that are documents for this model that has a collection.
      #
      # Returns an Array<Attachment>
      def documents_with_collection
        documents.select(&:attachment_collection_id?)
      end

      # All the attachments that are documents for this model that not has a collection.
      #
      # Returns an Array<Attachment>
      def documents_without_collection
        documents.reject(&:attachment_collection_id?)
      end
    end

    # Attachment context for the file uploaders checks (e.g. which kind of files
    # the user is allowed to upload in this context).
    #
    # Override this in the model class if it is for a different context.
    #
    # Returns a Symbol.
    def attachment_context
      :participant
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
decidim-core-0.30.0.rc1 lib/decidim/has_attachments.rb
decidim-core-0.29.2 lib/decidim/has_attachments.rb
decidim-core-0.28.5 lib/decidim/has_attachments.rb
decidim-core-0.29.1 lib/decidim/has_attachments.rb
decidim-core-0.28.4 lib/decidim/has_attachments.rb
decidim-core-0.27.9 lib/decidim/has_attachments.rb
decidim-core-0.29.0 lib/decidim/has_attachments.rb
decidim-core-0.28.3 lib/decidim/has_attachments.rb
decidim-core-0.27.8 lib/decidim/has_attachments.rb
decidim-core-0.29.0.rc4 lib/decidim/has_attachments.rb
decidim-core-0.29.0.rc3 lib/decidim/has_attachments.rb
decidim-core-0.29.0.rc2 lib/decidim/has_attachments.rb
decidim-core-0.29.0.rc1 lib/decidim/has_attachments.rb
decidim-core-0.28.2 lib/decidim/has_attachments.rb
decidim-core-0.27.7 lib/decidim/has_attachments.rb
decidim-core-0.28.1 lib/decidim/has_attachments.rb
decidim-core-0.27.6 lib/decidim/has_attachments.rb
decidim-core-0.28.0 lib/decidim/has_attachments.rb
decidim-core-0.27.5 lib/decidim/has_attachments.rb
decidim-core-0.28.0.rc5 lib/decidim/has_attachments.rb