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



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 868

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



859
860
861
862
863
864
865
866
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 859

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