# Reads the field catalog (which frontmatter fields "methoden"/"varianten" # have, with which label, whether required) straight out of # admin/config.template.yml instead of duplicating it - that file is # already the richest, most densely commented source (widget type, hint # text, pattern, required flag, list sub-fields, rationale comments for # every design decision), so it stays the one hand-maintained source; # everything else (scripts/validate.rb's required-field lists, # hugo/data/feldkatalog.json for aenderungssatz.html) reads from here # instead of keeping its own copy. See ableger/website#2. require 'yaml' module Feldkatalog CONFIG_TEMPLATE = File.expand_path('../../admin/config.template.yml', __dir__) # Ordered {name:, label:, required:} per collection ("methoden"/ # "varianten"), as YAML.safe_load sees it directly - the "{{VARIABLE}}" # placeholders elsewhere in this file (backend/site_url) don't appear # inside "fields:", so no preprocessing is needed before parsing. # required is true unless the field explicitly sets "required: false" # (Decap's own default). The "body" field (varianten' markdown widget, # the actual document content) is excluded - it isn't a frontmatter # field at all, "format: frontmatter" maps it to the document body # instead. def self.felder(collection) daten = YAML.safe_load(File.read(CONFIG_TEMPLATE, encoding: 'UTF-8')) eintrag = daten.fetch('collections').find { |c| c['name'] == collection } raise "Feldkatalog: Collection #{collection.inspect} nicht gefunden" unless eintrag eintrag.fetch('fields').reject { |f| f['name'] == 'body' }.map do |f| { name: f.fetch('name'), label: f.fetch('label'), required: f['required'] != false } end end def self.pflichtfelder(collection) felder(collection).select { |f| f[:required] }.map { |f| f[:name] } end end