module Ecoportal module API class V2 class Page class Sections < Common::Content::CollectionModel class_resolver :section_class, "Ecoportal::API::V2::Page::Section" self.klass = :section_class # Creates a new `section` 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(pos: pos, before: before, after: after) section.weight = weight end fix_weights! yield(section) if block_given? end end def get_by_type(type) ordered.select do |sec| sec.type == type end end def get_by_heading(heading) ordered.select do |sec| value = heading == :unnamed ? nil : heading same_string?(sec.heading, value) end end # Gets all the sections between `sec1` and `sec2` 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 # Gets the sections ordered by `weight` (as they appear in the page) def ordered self.sort_by.with_index do |section, index| (section.weight >= 9999) ? [index, index] : [section.weight, index] end end private def scope_weight(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 end 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 def fix_weights! ordered.each_with_index do |section, index| section.weight = index end end def previous_section(value) secs = ordered pos = secs.index(value) - 1 return if pos < 0 secs[pos] end end end end end end