class Ecoportal::API::V2::Page::Sections

Public Instance Methods

add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED) { |section| ... } click to toggle source

Creates a new `section` @note

- It won't fix weights unless all the sections of the ooze are present
- This means that it doesn't fix section weights on stages,
  as shared sections could change order in other stages
# File lib/ecoportal/api/v2/page/sections.rb, line 15
def add(name: nil, split: false, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  sec_doc = section_class.new_doc(split: split)
  upsert!(sec_doc) do |section| #, pos: pos, before: before, after: after) do |section|
    section.heading  = name
    if weight = scope_weight(section, pos: pos, before: before, after: after)
      section.weight = weight
    end
    fix_weights!
    yield(section) if block_given?
  end
end
between(sec1, sec2, included: false) click to toggle source

Gets all the sections between `sec1` and `sec2`

# File lib/ecoportal/api/v2/page/sections.rb, line 41
def between(sec1, sec2, included: false)
  sorted_secs = ordered
  pos1 = (sec1 = to_section(sec1)) && sorted_secs.index(sec1)
  pos2 = (sec2 = to_section(sec2)) && sorted_secs.index(sec2)
  if included
    pos1 = pos1 ? pos1 : 0
    pos2 = pos2 ? pos2 : -1
  else
    pos1 = pos1 ? (pos1 + 1) : 0
    pos2 = pos2 ? (pos2 - 1) : -1
  end
  sorted_secs[pos1..pos2]
end
get_by_heading(heading) click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 33
def get_by_heading(heading)
  ordered.select do |sec|
    value = heading == :unnamed ? nil : heading
    same_string?(sec.heading, value)
  end
end
get_by_type(type) click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 27
def get_by_type(type)
  ordered.select do |sec|
    sec.type == type
  end
end
ordered() click to toggle source

Gets the sections ordered by `weight` (as they appear in the page)

# File lib/ecoportal/api/v2/page/sections.rb, line 56
def ordered
  self.sort_by.with_index do |section, index|
    [section.weight, index]
  end
end

Private Instance Methods

fix_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 97
def fix_weights!
  unless self._parent.is_a?(Ecoportal::API::V2::Pages::PageStage)
    ordered.each_with_index do |section, index|
      section.weight = index
    end
  end
end
previous_section(value) click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 105
def previous_section(value)
  secs = ordered
  pos  = secs.index(value) - 1
  return if pos < 0
  secs[pos]
end
scope_weight(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED) click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 64
def scope_weight(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  case
  when used_param?(pos)
    if pos    = to_section(pos)
      pos.weight - 1
    end
  when used_param?(before)
    if before = to_section(before)
      before.weight - 1
    end
  when used_param?(after)
    if after  = to_section(after)
      after.weight
    end
  end.yield_self do |weight|
    weight = ordered.reject do |sec|
      sec.id == section.id
    end.last&.weight
    weight ||= section_class.const_get(:INITIAL_WEIGHT)
  end
end
to_section(value) click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 86
def to_section(value)
  case value
  when Ecoportal::API::V2::Page::Section
    value
  when Numeric
    ordered[value]
  else
    get_by_heading(value).first
  end
end