lib/ecoportal/api/v2/page/sections.rb in ecoportal-api-v2-0.8.26 vs lib/ecoportal/api/v2/page/sections.rb in ecoportal-api-v2-0.8.27

- old
+ new

@@ -9,27 +9,37 @@ def ooze self._parent.ooze end + # @return [Boolean] `true` if there is one or more sections shared, `false` otherwise + def any_shared? + self.any? {|sec| sec.shared?} + end + # Creates a new `section` # @note - # - It won't fix weights unless all the sections of the ooze are present + # - It won't fix weights unless all the sections of the ooze are present or there aren't shared sections # - 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! + move(section, pos: pos, before: before, after: after) yield(section) if block_given? end end + # Moves an existing `section` + def move(section, pos: NOT_USED, before: NOT_USED, after: NOT_USED) + if weight = scope_weight(section, pos: pos, before: before, after: after) + section.weight = weight + end + fix_weights! + end + # @return [Ecoportal::API::V2::Page::Section] def get_by_id(id) self.find do |sec| sec.id == id end @@ -40,15 +50,16 @@ ordered.select do |sec| sec.type == type end end + # @param mild [Boolean] modifier to only compare alphabetic characters # @return [Array<Ecoportal::API::V2::Page::Section>] - def get_by_heading(heading) + def get_by_heading(heading, mild: false) ordered.select do |sec| value = heading == :unnamed ? nil : heading - same_string?(sec.heading, value) + same_string?(sec.heading, value, mild: mild) end end # Gets all the sections between `sec1` and `sec2` # @return [Array<Ecoportal::API::V2::Page::Section>] @@ -94,11 +105,11 @@ when used_param?(after) if after = to_section(after) after.weight end end.yield_self do |weight| - weight = ordered.reject do |sec| + weight ||= ordered.reject do |sec| sec.id == section.id end.last&.weight weight ||= section_class.const_get(:INITIAL_WEIGHT) end end @@ -113,10 +124,10 @@ get_by_heading(value).first end end def fix_weights! - unless self._parent.is_a?(Ecoportal::API::V2::Pages::PageStage) + unless self.any_shared? && self._parent.is_a?(Ecoportal::API::V2::Pages::PageStage) ordered.each_with_index do |section, index| section.weight = index end end end