Sha256: d65d4d8cc23355bd0ab098407bf11dca2b347e3303318ee2400650f485f7ec0d

Contents?: true

Size: 1.92 KB

Versions: 25

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  # A set of convenience methods to deal with attachment attributes for Virtus
  # models that may set the attachment records through the original model
  # (Decidim::Attachment) or through the user submitted form data (String).
  module AttachmentAttributes
    extend ActiveSupport::Concern

    class_methods do
      # Public: Mirrors Virtus' `attribute` interface to define attachment
      # attributes for form objects.
      #
      # name - The attribute's name
      #
      #
      # Example:
      #
      #   attachment_attribute :photos
      #   # This will create two attributes of the following types:
      #   #   attribute :photos, Array[String]
      #   #   attribute :add_photos, Array
      #   # In addition, it will generate the getter method for the attribute
      #   # returning an array of the Decidim::Attachment records.
      #
      # Returns nothing.
      def attachments_attribute(name)
        attribute name, Array[String]
        attribute "add_#{name}".to_sym, Array

        # Define the getter method that fetches the attachment records based on
        # their types. For Strings and Integers, assumes they are IDs and will
        # fetch the attachment record matching that ID.
        variable_name = "@#{name}_records"
        define_method name do
          return instance_variable_get(variable_name) if instance_variable_defined?(variable_name)

          original = instance_variable_get("@#{name}")
          return original unless original.is_a?(Array)

          instance_variable_set(
            variable_name,
            original.map do |attachment|
              if attachment.is_a?(String) || attachment.is_a?(Integer)
                Decidim::Attachment.find_by(id: attachment)
              else
                attachment
              end
            end.compact
          )
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.26.10 lib/decidim/attachment_attributes.rb
decidim-core-0.26.9 lib/decidim/attachment_attributes.rb
decidim-core-0.26.8 lib/decidim/attachment_attributes.rb
decidim-core-0.26.7 lib/decidim/attachment_attributes.rb
decidim-core-0.26.5 lib/decidim/attachment_attributes.rb
decidim-core-0.26.4 lib/decidim/attachment_attributes.rb
decidim-core-0.26.3 lib/decidim/attachment_attributes.rb
decidim-core-0.26.2 lib/decidim/attachment_attributes.rb
decidim-core-0.26.1 lib/decidim/attachment_attributes.rb
decidim-core-0.26.0 lib/decidim/attachment_attributes.rb
decidim-core-0.26.0.rc2 lib/decidim/attachment_attributes.rb
decidim-core-0.26.0.rc1 lib/decidim/attachment_attributes.rb
decidim-core-0.25.2 lib/decidim/attachment_attributes.rb
decidim-core-0.25.1 lib/decidim/attachment_attributes.rb
decidim-core-0.25.0 lib/decidim/attachment_attributes.rb
decidim-core-0.25.0.rc4 lib/decidim/attachment_attributes.rb
decidim-core-0.25.0.rc3 lib/decidim/attachment_attributes.rb
decidim-core-0.25.0.rc2 lib/decidim/attachment_attributes.rb
decidim-core-0.25.0.rc1 lib/decidim/attachment_attributes.rb
decidim-core-0.24.3 lib/decidim/attachment_attributes.rb