Module: Katalyst::GOVUK::FormBuilder::Traits::Attachment

Extended by:
ActiveSupport::Concern
Includes:
ActionView::Helpers::NumberHelper
Included in:
Elements::Attachment
Defined in:
app/helpers/katalyst/govuk/form_builder/traits/attachment.rb

Overview

Generates attachment inputs and previews for ActiveStorage associations.

Constant Summary collapse

I18N_SCOPE =

The attachment strings' canonical home (config/locales).

%i[katalyst govuk attachment].freeze
BUNDLED_DEFAULTS =

The gem's own en strings, read straight from its locale file: resolving them through I18n would absorb a host app's en overrides, and this table is the baseline those overrides are detected against.

YAML.load_file(
  Engine.root.join("config/locales/en.yml"),
).dig("en", *I18N_SCOPE.map(&:to_s)).freeze

Instance Method Summary collapse

Parameters:

Returns:



87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 87

def attachment_input_for(blob)
  @builder.select(
    @attribute_name,
    [[blob.filename.to_s, blob.signed_id],
     [remove_button_label(blob), ""]],
    { selected: blob.signed_id },
    id:   attachment_id_for(blob, :input),
    name: @builder.field_name(@attribute_name, multiple: many?),
    aria: { labelledby: attachment_id_for(blob, :filename) },
  )
end

#attachment_preview_for(blob) ⇒ ActiveSupport::SafeBuffer?

The representation is rendered lazily: the variant is processed when the browser requests it, never during the form render. If the blob's bytes turn out to be missing or unprocessable the preview simply fails to load — validating attachment content is the model's responsibility, not the form's.

Parameters:

Returns:



147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 147

def attachment_preview_for(blob)
  return unless blob.representable?

  url = @builder.attachment_preview_url(
    blob.representation(config.attachment_preview_representation),
  )
  return if url.nil?

  # Setting alt to "" as the details already describe the attachment, equivalent to role="presentation"
  @builder.image_tag(url, alt: "", class: "preview")
end

#attachment_remove_for(blob) ⇒ ActiveSupport::SafeBuffer?

A

Parameters:

Returns:



102
103
104
105
106
107
108
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 102

def attachment_remove_for(blob)
  tag.button(remove_button_content,
             type:  "button",
             class: "#{brand}-button #{brand}-button--secondary #{brand}-attachment__remove",
             aria:  { label: remove_button_label(blob) },
             data:  { action: "govuk-attachment#destroy", module: "govuk-button" })
end

#attachment_translation(key) ⇒ String

The current locale's translation, falling back to the gem's en defaults rather than a "translation missing" marker.

Returns:



135
136
137
138
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 135

def attachment_translation(key, **)
  I18n.t(key, scope: I18N_SCOPE, default: nil, **) ||
    I18n.t(key, scope: I18N_SCOPE, locale: :en, **)
end

#many?Boolean

Returns:



34
35
36
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 34

def many?
  value.is_a?(ActiveStorage::Attached::Many)
end

#one?Boolean

Returns:



30
31
32
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 30

def one?
  value.is_a?(ActiveStorage::Attached::One)
end

#remove_button_contentString

Returns:



128
129
130
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 128

def remove_button_content
  @remove_button_content_text || attachment_translation(:remove_button_content)
end

#remove_button_label(blob) ⇒ String

The remove strings render here and in the JS figure template, so both draw from the same options (or matching defaults) — the two figure sources must stay string-identical.

Parameters:

Returns:



115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 115

def remove_button_label(blob)
  if @remove_button_text
    # %{filename} is the option placeholder (govuk-frontend's i18n
    # convention), substituted directly — not a Ruby format token.
    # rubocop:disable Style/FormatStringToken
    @remove_button_text.gsub("%{filename}", blob.filename.to_s)
    # rubocop:enable Style/FormatStringToken
  else
    attachment_translation(:remove_button, filename: blob.filename.to_s)
  end
end

#valueObject



38
39
40
# File 'app/helpers/katalyst/govuk/form_builder/traits/attachment.rb', line 38

def value
  @builder.object.send(@attribute_name)
end