Class: Ecoportal::API::GraphQL::Model::Template::Field

Inherits:
Object
  • Object
show all
Includes:
Node
Defined in:
lib/ecoportal/api/graphql/model/template/field.rb

Overview

One data field. field_type is immutable after creation on purpose: the platform has no editFieldType command (confirmed, the repo's internal docs section C.1) — giving this class no setter for it makes a destructive remove+add structurally unreachable rather than a rule #as_commands has to remember to enforce.

Constant Summary collapse

PLACEHOLDER_PREFIX =
'fld'.freeze

Instance Attribute Summary collapse

Attributes included from Node

#id

Instance Method Summary collapse

Methods included from Node

#new?, #placeholder, #ref, #remove!, #removed?, #resolve!

Constructor Details

#initialize(template:, section:, stage:, field_type:, id: nil, label: nil, column: nil, tooltip: nil, description: nil, required: nil, hidden: nil, options: []) ⇒ Field

Returns a new instance of Field.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 20

def initialize(template:, section:, stage:, field_type:, id: nil, label: nil, column: nil,
               tooltip: nil, description: nil, required: nil, hidden: nil, options: [])
  @template    = template
  @section     = section
  @stage       = stage
  @id          = id
  @field_type  = field_type
  @column      = column
  @label       = label
  @tooltip     = tooltip
  @description = description
  @required    = required
  @hidden      = hidden
  @options     = options
  @snapshot    = snapshot_attrs.freeze
end

Instance Attribute Details

#column ⇒ Object (readonly)

Returns the value of attribute column.



17
18
19
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 17

def column
  @column
end

#description ⇒ Object

Returns the value of attribute description.



18
19
20
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 18

def description
  @description
end

#field_type ⇒ Object (readonly)

Returns the value of attribute field_type.



17
18
19
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 17

def field_type
  @field_type
end

#hidden ⇒ Object

Returns the value of attribute hidden.



18
19
20
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 18

def hidden
  @hidden
end

#label ⇒ Object

Returns the value of attribute label.



18
19
20
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 18

def label
  @label
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 17

def options
  @options
end

#required ⇒ Object

Returns the value of attribute required.



18
19
20
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 18

def required
  @required
end

#section ⇒ Object (readonly)

Returns the value of attribute section.



17
18
19
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 17

def section
  @section
end

#stage ⇒ Object (readonly)

Returns the value of attribute stage.



17
18
19
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 17

def stage
  @stage
end

#template ⇒ Object (readonly)

Returns the value of attribute template.



17
18
19
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 17

def template
  @template
end

#tooltip ⇒ Object

Returns the value of attribute tooltip.



18
19
20
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 18

def tooltip
  @tooltip
end

Instance Method Details

#add_option(value:, label: nil, weight: nil) ⇒ Object

Appends a new (in-memory) option. Only meaningful for a Select-shaped field, but this class does not enforce field_type — the server does, on addSelectFieldOption.



39
40
41
42
43
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 39

def add_option(value:, label: nil, weight: nil)
  opt = Option.new(template: template, field: self, label: label, value: value, weight: weight)
  options << opt
  opt
end

#changed_attributes ⇒ Object

The subset of changed_attributes that maps onto editFieldConfiguration's top-level keys (label/tooltip/description/required/hidden) — empty when nothing changed, so callers can commands << ... unless field_config_changes.empty?.



52
53
54
55
56
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 52

def changed_attributes
  return {} if new? || removed?

  snapshot_attrs.each_with_object({}) { |(k, v), h| h[k] = v if v != @snapshot[k] }
end

#dirty? ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/ecoportal/api/graphql/model/template/field.rb', line 45

def dirty?
  !removed? && !new? && snapshot_attrs != @snapshot
end