Class: LcpRuby::Display::Renderers::AttachmentList

Inherits:
BaseRenderer
  • Object
show all
Includes:
Concerns::AttachmentHelpers
Defined in:
lib/lcp_ruby/display/renderers/attachment_list.rb

Instance Method Summary collapse

Instance Method Details

Returns:

  • (Boolean)


26
27
28
# File 'lib/lcp_ruby/display/renderers/attachment_list.rb', line 26

def link_producing?
  true
end

#render(value, options = {}, record: nil, view_context: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lcp_ruby/display/renderers/attachment_list.rb', line 7

def render(value, options = {}, record: nil, view_context: nil)
  attachment = resolve_attachment(value, record)
  return view_context.(:span, I18n.t("lcp_ruby.file_upload.no_file", default: "No file"), class: "lcp-no-attachment") unless attachment_present?(attachment)

  items = attachment.respond_to?(:each) ? attachment.to_a : [ attachment ]
  view_context.(:ul, class: "lcp-attachment-list") do
    view_context.safe_join(items.select { |att| att.respond_to?(:blob) }.map { |att|
      view_context.(:li) do
        view_context.link_to(att.blob.filename.to_s, attachment_url(att), target: "_blank", rel: "noopener") +
          " " +
          view_context.(:span, "(#{view_context.number_to_human_size(att.blob.byte_size)})", class: "lcp-attachment-size")
      end
    })
  end
rescue ActiveStorage::Error, ActionController::UrlGenerationError, NoMethodError => e
  LcpRuby.record_error(e, subsystem: "renderer")
  view_context.(:span, "Attachments", class: "lcp-attachment-list")
end