Sha256: ac9dcd4173ce6e6397a6523872d407b4b9fc885537a208016d7f94cd1461be94

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 KB

Contents

module Ecoportal
  module API
    class V2
      class Page
        class Component
          class ChecklistField < Page::Component
            embeds_many :items, klass: "Ecoportal::API::V2::Page::Component::ChecklistItem", order_key: :weight

            def add_item(label:, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
              itm_doc = items.items_class.new_doc
              items.upsert!(itm_doc, pos: pos, before: before, after: after) do |item|
                item.label  = label
                if prev = previous_item(item)
                  item.weight = prev.weight
                end
                yield(item) if block_given?
                fix_item_weights!
              end
            end

            def ordered_items
              items.sort_by.with_index do |item, index|
                [item.weight, index]
              end
            end

            private

            def fix_item_weights!
              ordered_items.each_with_index do |item, index|
                item.weight = index
              end
            end

            def previous_item(value)
              itms = ordered_items
              pos  = itms.index(value) - 1
              return if pos < 0
              itms[pos]
            end

          end
        end
      end
    end
  end
end

require 'ecoportal/api/v2/page/component/checklist_item'

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ecoportal-api-v2-0.8.20 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.19 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.18 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.17 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.16 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.15 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.14 lib/ecoportal/api/v2/page/component/checklist_field.rb
ecoportal-api-v2-0.8.13 lib/ecoportal/api/v2/page/component/checklist_field.rb