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 def ooze self._parent.ooze end # 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 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 # @return [Ecoportal::API::V2::Page::Section] def get_by_id(id) self.find do |sec| sec.id == id end end # @return [Array] def get_by_type(type) ordered.select do |sec| sec.type == type end end # @return [Array] 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` # @return [Array] 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) # @return [Array] section sorted by `weight` def ordered self.sort_by.with_index do |section, index| [section.weight, index] end end # @return [Array] sections not attached to any stage def unattached select {|sec| !sec.attached?} end private 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 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! unless self._parent.is_a?(Ecoportal::API::V2::Pages::PageStage) ordered.each_with_index do |section, index| section.weight = index end 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