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
# File lib/ecoportal/api/v2/page/sections.rb, line 10
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, pos: pos, before: before, after: after) do |section|
    section.heading  = name
    if prev = previous_section(section)
      section.weight = prev.weight
    end
    yield(section) if block_given?
    #fix_weights! # a server bug prevents to set the weight of existing sections
  end
end
ordered() click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 22
def ordered
  each_with_index.sort_by do |section, index|
    (section.weight >= 9999) ? [index, index] : [section.weight, index]
  end.map(&:first)
end

Private Instance Methods

fix_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 30
def fix_weights!
  ordered.each_with_index do |section, index|
    section.weight = index
  end
end
previous_section(value) click to toggle source
# File lib/ecoportal/api/v2/page/sections.rb, line 36
def previous_section(value)
  secs = ordered
  pos  = secs.index(value) - 1
  return if pos < 0
  secs[pos]
end