Class: GOVUKDesignSystemFormBuilder::Elements::Document

Inherits:
Base
  • Object
show all
Includes:
FileElement
Defined in:
lib/govuk_design_system_formbuilder/elements/document.rb

Constant Summary collapse

MIME_TYPES =
%w[
  image/png image/gif image/jpeg image/webp
  application/pdf audio/*
].freeze

Instance Method Summary collapse

Methods included from FileElement

#html

Constructor Details

#initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
20
21
22
23
24
# File 'lib/govuk_design_system_formbuilder/elements/document.rb', line 15

def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, **kwargs, &block)
  super(builder, object_name, attribute_name, &block)

  @mime_types      = MIME_TYPES || kwargs[:mime_types]
  @label           = label
  @caption         = caption
  @hint            = hint
  @html_attributes = kwargs.merge(file_input_options)
  @form_group      = form_group
end

Instance Method Details

#previewObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/govuk_design_system_formbuilder/elements/document.rb', line 26

def preview
  options = {}
  add_option(options, :data, "#{stimulus_controller}_target", "preview")
  add_option(options, :class, "preview-file")
  add_option(options, :class, "hidden") unless preview?

  tag.div **options do
    filename = @builder.object.send(@attribute_name).filename.to_s
    tag.p(filename, class: "preview-filename") + destroy_element_trigger
  end
end