class Ecoportal::API::V2::Page::Component::ChecklistField

Public Instance Methods

add_item(label:, pos: NOT_USED, before: NOT_USED, after: NOT_USED) { |item| ... } click to toggle source
# File lib/ecoportal/api/v2/page/component/checklist_field.rb, line 9
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
ordered_items() click to toggle source
# File lib/ecoportal/api/v2/page/component/checklist_field.rb, line 21
def ordered_items
  items.sort_by.with_index do |item, index|
    [item.weight, index]
  end
end

Private Instance Methods

fix_item_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/component/checklist_field.rb, line 29
def fix_item_weights!
  ordered_items.each_with_index do |item, index|
    item.weight = index
  end
end
previous_item(value) click to toggle source
# File lib/ecoportal/api/v2/page/component/checklist_field.rb, line 35
def previous_item(value)
  itms = ordered_items
  pos  = itms.index(value) - 1
  return if pos < 0
  itms[pos]
end