Class: Glib::JsonUi::ViewBuilder::Fields::Upload

Inherits:
AbstractField show all
Includes:
FileUploadErrorHandler, Upload
Defined in:
app/helpers/glib/json_ui/view_builder/fields.rb

Overview

File upload field with direct-upload support and a progress list of already-attached files.

When re-editing a record, the field pre-populates multiProgressView.files from the model's attachments. Apps that disable ActiveStorage's default routes (config.active_storage.draw_routes = false) must define a glib_attachment_url_for(attachment) view helper returning their own authorized file URL -- see AbstractField#existing_attachment_url.

Instance Attribute Summary

Attributes inherited from JsonUiElement

#json, #page

Instance Method Summary collapse

Methods inherited from AbstractField

#autoValidate, #context, #default_url_options, #determine_value, #disableDirtyCheck, #existing_attachment_url, #hint, #hint_args, #label, #label_args, #name, #placeholder, #placeholder_args, #prop, #validation, #value

Methods inherited from View

component_name

Methods inherited from JsonUiElement

action, any, array, badgeable, bool, color, component_name, date, date_time, enum, float, hash, icon, #initialize, int, length, menu, panels_builder, #props, required, singleton_array, string, text, url, views

Constructor Details

This class inherits a constructor from Glib::JsonUi::JsonUiElement

Instance Method Details

#createdObject



814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 814

def created
  super
  file_upload_error_handler_on_create

  @placeholder ||= I18n.t('glib.multi_upload.placeholder', default: nil)
  @hint ||= I18n.t('glib.multi_upload.hint', default: nil)

  if @prop && context && @multi_progress
    value = context.field_value(@prop, collect_ids: false)
    attachments = value.is_a?(::ActiveStorage::Attached::One) ? Array.wrap(value.attachment) : value.to_a
    @multi_progress['files'] ||= attachments.map do |file|
      { name: file.blob&.filename, signed_id: file.signed_id, url: existing_attachment_url(file) }
    end
  end

  if @multi_progress
    # Copy before writing: `stringify_keys` is shallow, so the nested
    # hash may still be the caller's own object, and the I18n merge
    # below must not mutate (or accumulate state in) a hash the
    # consumer may freeze or reuse across renders.
    messages = (@multi_progress['responseMessages'] || {}).dup
    ['200', '403', '401', 'else'].each do |status|
      key = "glib.multi_upload.responseMessages.#{status}"
      messages[status] = I18n.t(key) if I18n.exists?(key)
    end
    @multi_progress['responseMessages'] = messages

    json.responseMessages(
      messages.reverse_merge(
        '200' => 'Completed',
        '403' => 'Forbidden',
        '401' => 'Session expired',
        'else' => 'Failed'
      )
    )

    json.placeholder @placeholder if @placeholder
    json.hint @hint if @hint

    json.multiProgressView @multi_progress
  end
end

#multiProgressView(values) ⇒ Object



805
806
807
808
809
810
811
812
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 805

def multiProgressView(values)
  # Consumers naturally write jbuilder hashes with symbol keys, but the
  # reads in `created` use string keys. Normalize so a symbol-keyed
  # `files:`/`responseMessages:` is honored instead of silently ignored
  # (an ignored `files:` used to send the field down the url_for
  # pre-population path even when the app supplied its own list).
  @multi_progress = values&.stringify_keys
end