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

Public Instance Methods

add_option(value:, name: nil, 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 47
def add_option(value:, name: nil, 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
configure(*conf) click to toggle source

Quick config helper @param conf [Symbol, Array<Symbol>]

- `:flat` to display in flat mode
- `:multiple` to allow multiple selection
- `:single` to set to singular selection
- `:other` to enable `other` button
- `:options` to add options (`Hash<value, name>`)
- `:type` to define the type
  - `:num`
  - `:str`
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 76
def configure(*conf)
  conf.each_with_object([]) do |cnf, unused|
    case cnf
    when :flat
      self.flat  = true
    when :multiple
      self.multiple = true
    when :single
      self.multiple = false
    when :other
      self.other = true
    when Hash
      supported = [:flat, :options, :type]
      unless (rest = hash_except(cnf.dup, *supported)).empty?
        unused.push(rest)
      end

      if cnf.key?(:flat) then self.flat = cnf[:flat] end
      if cnf.key?(:options)
        if opts = cnf[:options]
          configure_options opts
        end
      end
      if cnf.key?(:type)
        if cnf[:type] == :str
          self.text!
        elsif cnf[:type] == :num
          self.numeric!
        else
          # Unknown type
        end
      end
    else
      unused.push(cnf)
    end
  end.yield_self do |unused|
    super(*unused)
  end
end
numeric!(&block) click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 13
def numeric!(&block)
  ordered_options.each {|opt| opt.numeric!(&block)}
  self.data_type = "num"
end
ordered_options() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 60
def ordered_options
  options.sort_by.with_index do |option, index|
    [option.weight, index]
  end
end
select(value) click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 23
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 31
def selected
  if multiple
    options.select {|opt| opt.selected}
  else
    options.find {|opt| opt.selected}
  end
end
text!(&block) click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 18
def text!(&block)
  ordered_options.each {|opt| opt.text!(&block)}
  self.data_type = "str"
end
value() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 39
def value
  if multiple
    selected.map {|opt| opt.value}
  else
    selected&.value
  end
end

Private Instance Methods

configure_options(opts) click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 118
def configure_options(opts)
  opts.each do |val, nm|
    add_option(value: val, name: nm)
  end
end
fix_option_weights!() click to toggle source
# File lib/ecoportal/api/v2/page/component/selection_field.rb, line 124
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 130
def previous_option(value)
  opts = ordered_options
  pos  = opts.index(value) - 1
  return if pos < 0
  opts[pos]
end