lib/ecoportal/api/v2/page/sections.rb in ecoportal-api-v2-0.8.12 vs lib/ecoportal/api/v2/page/sections.rb in ecoportal-api-v2-0.8.13

- old
+ new

@@ -6,15 +6,19 @@ class_resolver :section_class, "Ecoportal::API::V2::Page::Section" self.klass = :section_class # 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(pos: pos, before: before, after: after) + if weight = scope_weight(section, pos: pos, before: before, after: after) section.weight = weight end fix_weights! yield(section) if block_given? end @@ -49,17 +53,17 @@ 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] + [section.weight, index] end end private - def scope_weight(pos: NOT_USED, before: NOT_USED, after: NOT_USED) + 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 @@ -69,10 +73,15 @@ 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 @@ -84,11 +93,13 @@ get_by_heading(value).first end end def fix_weights! - ordered.each_with_index do |section, index| - section.weight = index + 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