class Ecoportal::API::V2::Page::Section

Constants

INITIAL_WEIGHT

Public Class Methods

new_doc(split: false) click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 9
def new_doc(split: false)
  {
    "id"         => new_uuid,
    "type"       => split ? "split" : "content",
    "weight"     => INITIAL_WEIGHT
  }.tap do |out|
    component_ids = if split
        {
          "left_component_ids" => [],
          "right_component_ids" => []
        }
      else
        {
          "component_ids" => []
        }
      end
    out.merge!(component_ids)
  end
end

Public Instance Methods

add_component(field, after: nil, side: nil) click to toggle source

Adds `field` to the section @note

- To the specified `side`, when split section (default `:left`)
- To the end if `after` is not specified
- If `after` is specified, it searches field
  - On the specific `side`, if specified (and split section)
  - And adds the `field` after it, when found, or at the end if `after` is not found

@param field [Ecoportal::API::V2::Page::Component] the field to be added.

# File lib/ecoportal/api/v2/page/section.rb, line 81
def add_component(field, after: nil, side: nil)
  raise "field should be a Ecoportal::API::V2::Page::Component. Given: #{field.class}" unless field.is_a?(Ecoportal::API::V2::Page::Component)
  # IMPORTANT NOTE: The code below creates objects, because field.section does a search on section.component_ids
  if field.section == self
    puts "Field with id '#{field.id}' already belongs to this section"
  elsif sec = field.section
    # Field belongs to another section
    raise "Field with id '#{field.id}' belongs to section '#{sec.heading || "Unnamed"}' (id: '#{sec.id}')"
  end

  if split?
    ids_ary = side == :right ? right_component_ids : left_component_ids
    fields  = components(side: side || :left)
  else
    ids_ary = component_ids
    fields  = components
  end

  if after
    after_fld = fields.find do |fld|
      found   = nil
      found ||= !!after if after.is_a?(Ecoportal::API::V2::Page::Component)
      found ||= fld.id == after
      found ||= same_string?(fld.label, after)
    end
  end

  ids_ary.insert_one(field.id, after: after_fld&.id)
  self
end
all_component_ids() click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 41
def all_component_ids
  return component_ids.to_a unless split?
  left_component_ids.to_a | right_component_ids.to_a
end
component?(id) click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 46
def component?(id)
  all_component_ids.include?(id)
end
components(side: nil) click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 50
def components(side: nil)
  case side
  when :right
    components_right
  when :left
    components_left
  when NilClass
    components_by_id(*all_component_ids)
  else
    raise "Side should be one of [nil, :right, :left]. Given: #{side}"
  end
end
components_left() click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 63
def components_left
  raise "You are trying to retrieve side components in a Split Section" unless split?
  components_by_id(*left_component_ids)
end
components_right() click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 68
def components_right
  raise "You are trying to retrieve side components in a Split Section" unless split?
  components_by_id(*right_component_ids)
end
split?() click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 37
def split?
  doc && doc["type"] == "split"
end

Private Instance Methods

components_by_id(*ids) { |fld| ... } click to toggle source
# File lib/ecoportal/api/v2/page/section.rb, line 114
def components_by_id(*ids)
  root.components.values_at(*ids).select.with_index do |fld, i|
    puts "Warning: field id #{ids[i]} points to missing field" if !fld
    fld && (!block_given? || yield(fld))
  end
end