Module: Archsight::Web::Editor::Helpers

Defined in:
lib/archsight/web/editor/helpers.rb

Overview

Shared helpers for editor routes

Instance Method Summary collapse

Instance Method Details

#available_relations(kind) ⇒ Object

Get available relations for a resource kind



17
18
19
# File 'lib/archsight/web/editor/helpers.rb', line 17

def available_relations(kind)
  Archsight::Editor.available_relations(kind)
end

#build_form_metadata(kind, klass) ⇒ Object

Build form metadata response for a kind



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/archsight/web/editor/helpers.rb', line 52

def (kind, klass)
  {
    kind: kind,
    icon: klass.icon,
    layer: klass.layer,
    fields: serialize_fields(kind),
    relation_options: build_relation_options(kind),
    instances_by_kind: build_instances_by_kind(kind),
    inline_edit_enabled: settings.inline_edit_enabled
  }
end

#editor_fields(kind) ⇒ Object

Get form fields for a resource kind



12
13
14
# File 'lib/archsight/web/editor/helpers.rb', line 12

def editor_fields(kind)
  FormBuilder.fields_for(kind)
end

#extract_annotations(params) ⇒ Object

Extract annotations from JSON body or form params



22
23
24
25
# File 'lib/archsight/web/editor/helpers.rb', line 22

def extract_annotations(params)
  annotations = params["annotations"] || {}
  annotations.transform_values { |v| v.is_a?(String) ? v.strip : v }
end

#extract_instance_relations(instance) ⇒ Object

Extract relations from an existing instance into form format



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/archsight/web/editor/helpers.rb', line 65

def extract_instance_relations(instance)
  relations = []

  instance.spec.each do |verb, relation_groups|
    next unless relation_groups.is_a?(Hash)

    relation_groups.each do |relation_name, targets|
      next unless targets.is_a?(Array)

      target_class = Archsight::Editor.target_class_for_relation(instance.kind, verb, relation_name)
      next unless target_class

      targets.each do |target|
        target_name = target.respond_to?(:name) ? target.name : target.to_s
        relations << { verb: verb, kind: target_class, name: target_name }
      end
    end
  end

  relations
end

#parse_json_relations(relations_data) ⇒ Object

Parse relations from JSON array format Accepts: [{ verb, kind, names: […] }] or [{ verb, kind, name }]



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/archsight/web/editor/helpers.rb', line 29

def parse_json_relations(relations_data)
  return [] unless relations_data.is_a?(Array)

  relations_data.each_with_object([]) do |rel, result|
    next unless rel.is_a?(Hash)

    parsed = parse_single_relation(rel)
    next unless parsed

    merge_relation(result, parsed)
  end
end

#validate_content_hash(instance, expected_hash) ⇒ Object

Validate content hash for optimistic locking



43
44
45
46
47
48
49
# File 'lib/archsight/web/editor/helpers.rb', line 43

def validate_content_hash(instance, expected_hash)
  Archsight::Editor::ContentHasher.validate(
    path: instance.path_ref.path,
    start_line: instance.path_ref.line_no,
    expected_hash: expected_hash
  )
end