Module: Hecks::Forms::ReferenceOptions

Defined in:
lib/hecks/forms/reference_options.rb

Overview

A Field tree (field_shape.rb) -> {path => [[id, label], ...]} for every :reference field in it — the options a <select> needs. Shared by CommandFormRenderer and QueryFormRenderer alike: a command's own reference field and a query's own reference parameter resolve against the same repository the same way, and neither word owns this more than the other.

Class Method Summary collapse

Class Method Details

.collect(registry, domain, fields) ⇒ Object



10
11
12
13
14
# File 'lib/hecks/forms/reference_options.rb', line 10

def self.collect(registry, domain, fields)
  targets = {}
  walk(fields) { |field| targets[field.path] = field.target_aggregate if field.kind == :reference }
  targets.transform_values { |aggregate| aggregate && options_for(registry, domain, aggregate) }
end

.options_for(registry, domain, aggregate) ⇒ Object



23
24
25
26
27
# File 'lib/hecks/forms/reference_options.rb', line 23

def self.options_for(registry, domain, aggregate)
  registry.repository(domain, aggregate).all.first(200).map { |instance| [instance.id, instance.id] }
rescue StandardError
  nil # a wiring gap (no repository bound) degrades to a plain text id input, not a 500
end

.walk(fields, &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/hecks/forms/reference_options.rb', line 16

def self.walk(fields, &block)
  fields.each do |field|
    block.call(field)
    walk(field.children, &block) if field.children
  end
end