class Ecoportal::API::V2::Page::Component::SelectionField

Public Instance Methods

add_option(name:, value:, pos: NOT_USED, before: NOT_USED, after: NOT_USED) { |option| ... } click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 36
def add_option(name:, value:, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  opt_doc = options.items_class.new_doc              
  options.upsert!(opt_doc, pos: pos, before: before, after: after) do |option|
    option.name  = name
    option.value = value
    if prev = previous_option(option)
      option.weight = prev.weight
    end
    yield(option) if block_given?
    fix_option_weights!
  end
end
ordered_options() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 49
def ordered_options
  options.each_with_index.sort_by do |option, index|
    (option.weight >= 9999) ? [index, index] : [option.weight, index]
  end.map(&:first)
end
select(value) click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 12
def select(value)
  opt = options.find {|opt| opt.value == value}
  sel = selected
  return true if !multiple && opt == sel
  sel.selected = false if !multiple && sel
  opt.selected = true unless !opt
end
selected() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 20
def selected
  if multiple
    options.select {|opt| opt.selected}
  else
    options.find {|opt| opt.selected}
  end
end
value() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 28
def value
  if multiple
    selected.map {|opt| opt.value}
  else
    selected&.value
  end
end

Private Instance Methods

fix_option_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 57
def fix_option_weights!
  ordered_options.each_with_index do |option, index|
    option.weight = index
  end
end
previous_option(value) click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 63
def previous_option(value)
  opts = ordered_options
  pos  = opts.index(value) - 1
  return if pos < 0
  opts[pos]
end