Sha256: 32d940f6bcea8c4f067a85d4cac7a6c8b5eb9452014ee2dcb9f6b2f530f3de7c

Contents?: true

Size: 1.39 KB

Versions: 7

Compression:

Stored size: 1.39 KB

Contents

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 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

          def ordered
            each_with_index.sort_by do |section, index|
              (section.weight >= 9999) ? [index, index] : [section.weight, index]
            end.map(&:first)
          end

          private

          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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
ecoportal-api-v2-0.8.8 lib/ecoportal/api/v2/page/sections.rb
ecoportal-api-v2-0.8.7 lib/ecoportal/api/v2/page/sections.rb
ecoportal-api-v2-0.8.6 lib/ecoportal/api/v2/page/sections.rb
ecoportal-api-v2-0.8.5 lib/ecoportal/api/v2/page/sections.rb
ecoportal-api-v2-0.8.4 lib/ecoportal/api/v2/page/sections.rb
ecoportal-api-oozes-0.7.5 lib/ecoportal/api/v2/page/sections.rb
ecoportal-api-oozes-0.7.4 lib/ecoportal/api/v2/page/sections.rb