Class: Ecoportal::API::GraphQL::Model::Template::Section

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

Overview

One section. kind is :content (single fields list) or :split (left_fields / right_fields, each independently addressable — a SplitSection has NO dataFields of its own, see Read.call's header). #fields always returns the flattened, document-ordered list (left then right, for a split section) — the ONE place callers should iterate unless they specifically need a side.

Constant Summary collapse

PLACEHOLDER_PREFIX =
'sec'.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:, stage:, id: nil, kind: :content, layout: nil, heading: nil, left_heading: nil, right_heading: nil) ⇒ Section

Returns a new instance of Section.



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

def initialize(template:, stage:, id: nil, kind: :content, layout: nil,
               heading: nil, left_heading: nil, right_heading: nil)
  @template      = template
  @stage         = stage
  @id            = id
  @kind          = kind
  @layout        = layout
  @heading       = heading
  @left_heading  = left_heading
  @right_heading = right_heading
  @left_fields   = []
  @right_fields  = []
  @pending_move  = nil
  @snapshot      = snapshot_attrs.freeze
end

Instance Attribute Details

#heading ⇒ Object

Returns the value of attribute heading.



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

def heading
  @heading
end

#kind ⇒ Object (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

#layout ⇒ Object (readonly)

Returns the value of attribute layout.



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

def layout
  @layout
end

#left_fields ⇒ Object (readonly)

Returns the value of attribute left_fields.



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

def left_fields
  @left_fields
end

#left_heading ⇒ Object

Returns the value of attribute left_heading.



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

def left_heading
  @left_heading
end

#pending_move ⇒ Object (readonly)

Returns the value of attribute pending_move.



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

def pending_move
  @pending_move
end

#right_fields ⇒ Object (readonly)

Returns the value of attribute right_fields.



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

def right_fields
  @right_fields
end

#right_heading ⇒ Object

Returns the value of attribute right_heading.



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

def right_heading
  @right_heading
end

#stage ⇒ Object (readonly)

Returns the value of attribute stage.



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

def stage
  @stage
end

#template ⇒ Object (readonly)

Returns the value of attribute template.



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

def template
  @template
end

Instance Method Details

#add_field(field_type:, label: nil, column: nil, side: nil, **kargs) ⇒ Object



42
43
44
45
46
47
# File 'lib/ecoportal/api/graphql/model/template/section.rb', line 42

def add_field(field_type:, label: nil, column: nil, side: nil, **kargs)
  field = Field.new(template: template, section: self, stage: stage,
                    field_type: field_type, label: label, column: column, **kargs)
  (side == :right ? right_fields : left_fields) << field
  field
end

#changed_attributes ⇒ Object



66
67
68
69
70
# File 'lib/ecoportal/api/graphql/model/template/section.rb', line 66

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)


62
63
64
# File 'lib/ecoportal/api/graphql/model/template/section.rb', line 62

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

#fields ⇒ Object

Document-ordered fields. For a :content section this IS left_fields; for a :split section it is left-then-right (matches Read.call's own document-order flattening, so #fields here and the loaded order agree).



38
39
40
# File 'lib/ecoportal/api/graphql/model/template/section.rb', line 38

def fields
  kind == :split ? left_fields + right_fields : left_fields
end

#move(anchor:, position:) ⇒ Object

Records the intent to reposition this (existing) section relative to anchor (another Section) — emitted as ONE reorderSection command as a per-stage TRAILER (after every structural add/remove in the same stage), because the server rewrites EVERY sibling's weight on a reorder (18_template_editor_save_path.md section C.1) — running it before a same-batch addStageSection would have its effect immediately overwritten by the next section's own weight assignment.

Raises:

  • (ArgumentError)


56
57
58
59
60
# File 'lib/ecoportal/api/graphql/model/template/section.rb', line 56

def move(anchor:, position:)
  raise ArgumentError, "position must be 'BEFORE' or 'AFTER'" unless %w[BEFORE AFTER].include?(position)

  @pending_move = { anchor: anchor, position: position }
end