Class: Ecoportal::API::GraphQL::Base::Page::DataField::FileField

Inherits:
DataField
  • Object
show all
Defined in:
lib/ecoportal/api/graphql/base/page/data_field/file_field.rb

Overview

File attachment field. READ: the fragment selects items { id fileContainer { id ... } } — the container id lives at items.fileContainer.id, NOT in a fileContainers array (live-verified; the previous reader keyed on a doc key the API never returns). WRITE: live FileInput accepts items: [ItemInput] (fileContainerId, position), NOT fileContainerIds. The backend applies items as FULL-REPLACE — any existing item whose id is not echoed back is destroyed — so as_input sends kept items as {id:} and new containers as {fileContainerId:}. items is deliberately a plain doc reader, not passarray: materialising an ArrayModel writes items: [] into the doc of a field read without $content, and a phantom-dirty empty items here doesn't no-op — it DELETES every attachment on the next update. Container ids come from a prior upload — see FileUpload::Client#upload_all.

Instance Method Summary collapse

Instance Method Details

#as_inputObject



43
44
45
46
47
# File 'lib/ecoportal/api/graphql/base/page/data_field/file_field.rb', line 43

def as_input
  return nil unless dirty?

  { file: { id: id, items: items_input } }
end

#file_container_idsObject



21
22
23
24
25
# File 'lib/ecoportal/api/graphql/base/page/data_field/file_field.rb', line 21

def file_container_ids
  items.map do |item|
    item.is_a?(Hash) ? item.dig('fileContainer', 'id') : item
  end.compact
end

#file_container_ids=(ids) ⇒ Object

Full-replace: containers already on the field keep their item identity; ids not present in ids are removed server-side by omission.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ecoportal/api/graphql/base/page/data_field/file_field.rb', line 29

def file_container_ids=(ids)
  kept = {}
  items.each do |item|
    next unless item.is_a?(Hash) && (fc_id = item.dig('fileContainer', 'id'))

    kept[fc_id] ||= item['id']
  end
  doc['items'] = Array(ids).map do |fc_id|
    new_item = { 'fileContainer' => { 'id' => fc_id } }
    new_item['id'] = kept[fc_id] if kept[fc_id]
    new_item
  end
end

#itemsObject

each: { 'id' =>, 'fileContainer' => { 'id' =>, 'activeFileAttachment' => ... } }



17
18
19
# File 'lib/ecoportal/api/graphql/base/page/data_field/file_field.rb', line 17

def items
  Array(doc['items'])
end