Sha256: d3754099794ac633a30f96df088fce44cd8ae26d93afc02584d6799097454a0e

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Decidim
  module Admin
    # A form object used to create attachments in a participatory process.
    #
    class AttachmentForm < Form
      include TranslatableAttributes

      attribute :file
      translatable_attribute :title, String
      translatable_attribute :description, String
      attribute :weight, Integer, default: 0
      attribute :attachment_collection_id, Integer
      attribute :link, String

      mimic :attachment

      validates :file, presence: true, unless: :persisted_or_link?
      validates :link, url: true
      validates :file, passthru: { to: Decidim::Attachment }
      validates :title, :description, translatable_presence: true
      validates :attachment_collection, presence: true, if: ->(form) { form.attachment_collection_id.present? }
      validates :attachment_collection_id, inclusion: { in: :attachment_collection_ids }, allow_blank: true

      delegate :attached_to, to: :context, prefix: false

      alias organization current_organization

      def persisted_or_link?
        persisted? || link.present?
      end

      def attachment_collections
        @attachment_collections ||= attached_to.attachment_collections
      end

      def attachment_collection
        @attachment_collection ||= attachment_collections.find_by(id: attachment_collection_id)
      end

      private

      def attachment_collection_ids
        @attachment_collection_ids ||= attachment_collections.pluck(:id)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-admin-0.29.1 app/forms/decidim/admin/attachment_form.rb
decidim-admin-0.29.0 app/forms/decidim/admin/attachment_form.rb
decidim-admin-0.29.0.rc4 app/forms/decidim/admin/attachment_form.rb
decidim-admin-0.29.0.rc3 app/forms/decidim/admin/attachment_form.rb
decidim-admin-0.29.0.rc2 app/forms/decidim/admin/attachment_form.rb
decidim-admin-0.29.0.rc1 app/forms/decidim/admin/attachment_form.rb