Module: Plutonium::Helpers::AttachmentHelper

Defined in:
lib/plutonium/helpers/attachment_helper.rb

Instance Method Summary collapse

Instance Method Details

#attachment_preview(attachments, **options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plutonium/helpers/attachment_helper.rb', line 4

def attachment_preview(attachments, **options)
  clamp_content begin
    tag.div class: [options[:identity_class], "attachment-preview-container d-flex flex-wrap gap-1 my-1"],
      data: {controller: "attachment-preview-container"} do
      Array(attachments).each do |attachment|
        next unless attachment.url.present?

        concat begin
          tag.div class: [options[:identity_class], "attachment-preview d-inline-block text-center"],
            title: attachment.filename,
            data: {
              controller: "attachment-preview",
              attachment_preview_mime_type_value: attachment.content_type,
              attachment_preview_thumbnail_url_value: _attachment_thumbnail_url(attachment)
            } do
            tag.figure class: "figure my-1", style: "width: 160px;" do
              concat attachment_preview_thumnail(attachment)
              concat begin
                tag.figcaption class: "figure-caption text-truncate" do
                  if options[:caption]
                    caption = options[:caption].is_a?(String) ? options[:caption] : attachment.filename
                    concat link_to(caption, attachment.url, class: "text-decoration-none", target: :blank)
                  end

                  if block_given?
                    elements = Array(yield attachment).compact
                    elements.each { |elem| concat elem }
                  end
                end
              end
            end
          end
        end
      end
    end
  end
end

#attachment_preview_thumnail(attachment) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/plutonium/helpers/attachment_helper.rb', line 42

def attachment_preview_thumnail(attachment)
  return unless attachment.url.present?

  # Any changes made here must be reflected in attachment_input_controller#buildPreviewTemplate

  tag.div class: "bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700", data: {attachment_preview_target: "thumbnail"} do
    thumbnail_url = _attachment_thumbnail_url(attachment)
    link_body = if thumbnail_url
      image_tag thumbnail_url, style: "width:100%; height:100%; object-fit: contain;"
    else
      _attachment_extension(attachment)
    end

    link_to link_body, attachment.url, style: "width:150px; height:150px; line-height: 150px;",
      class: "d-block text-decoration-none user-select-none fs-5 font-monospace text-body-secondary",
      target: :blank,
      data: {attachment_preview_target: "thumbnailLink"}
  end
end