Sha256: 72e562dd649d660b2f8ebcfa97c352cc70b1ba3024cf0e898ad4c7eae931de8c

Contents?: true

Size: 916 Bytes

Versions: 3

Compression:

Stored size: 916 Bytes

Contents

module Headmin
  module Attachment
    extend ActiveSupport::Concern
    included do
      scope :not_a_variant, -> { where.not(record_type: "ActiveStorage::VariantRecord") }

      def record_hierarchy
        hierarchy = []

        current = record

        current, partial_hierarchy = process_fieldable(current)
        hierarchy << partial_hierarchy if partial_hierarchy.present?

        current, partial_hierarchy = process_blockable(current)
        hierarchy << partial_hierarchy if partial_hierarchy.present?

        hierarchy << current if current.present?

        hierarchy.flatten
      end

      private

      def process_blockable(field)
        field.respond_to?(:blockable) ? [field.blockable, field] : [field, nil]
      end

      def process_fieldable(field)
        field.respond_to?(:fieldable) ? [field.root.fieldable, field.self_and_ancestors] : [field, nil]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
headmin-0.6.3 app/models/concerns/headmin/attachment.rb
headmin-0.6.2 app/models/concerns/headmin/attachment.rb
headmin-0.6.1 app/models/concerns/headmin/attachment.rb