Class: Ecoportal::API::V2::Page::Component::SelectionField

Inherits:
Ecoportal::API::V2::Page::Component show all
Defined in:
lib/ecoportal/api/v2/page/component/selection_field.rb

Constant Summary

Constants included from Common::Content::StringDigest

Common::Content::StringDigest::MAX_HASH_LABEL

Constants inherited from Common::Content::DoubleModel

Common::Content::DoubleModel::NOT_USED

Constants included from Common::Content::ClassHelpers

Common::Content::ClassHelpers::NOT_USED

Instance Attribute Summary

Attributes inherited from Common::Content::DoubleModel

#_key, #_parent

Instance Method Summary collapse

Methods inherited from Ecoportal::API::V2::Page::Component

get_class, #indexable_label, new_doc, #ref, #section

Methods included from Common::Content::StringDigest

#hash_label, #indexable_label

Methods inherited from Common::Content::DoubleModel

#_doc_key, #as_json, #as_update, #consolidate!, #dirty?, #doc, embeds_multiple, embeds_one, #initialize, #key, #key=, key?, new_uuid, #original_doc, pass_reader, pass_writer, passarray, passdate, passkey, passthrough, #pretty_print, #replace_doc, #reset!, #root, #to_json

Methods included from Common::Content::ClassHelpers

#instance_variable_name, #new_class, #resolve_class, #to_constant, #to_time, #used_param?

Constructor Details

This class inherits a constructor from Ecoportal::API::Common::Content::DoubleModel

Instance Method Details

#add_option(name:, value:, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 37

def add_option(name:, value:, pos: NOT_USED, before: NOT_USED, after: NOT_USED)
  opt_doc = Ecoportal::API::V2::Page::Component::SelectionOption.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_optionsObject



50
51
52
53
54
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 50

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) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 13

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

#selectedObject



21
22
23
24
25
26
27
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 21

def selected
  if multiple
    options.select {|opt| opt.selected}
  else
    options.find {|opt| opt.selected}
  end
end

#valueObject



29
30
31
32
33
34
35
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 29

def value
  if multiple
    selected.map {|opt| opt.value}
  else
    selected&.value
  end
end