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

#attached?, get_class, #indexable_label, #multi_section?, new_doc, #ooze, #ref, #ref_backend, #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_many, embeds_one, enforce!, #initialize, #key, #key=, key?, new_uuid, #original_doc, pass_reader, pass_writer, passarray, passboolean, passdate, passforced, passkey, passthrough, #print_pretty, #replace_doc, #reset!, #root, #to_json

Methods included from Common::Content::ClassHelpers

#inheritable_attrs, #inheritable_class_vars, #inherited, #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(value:, name: nil, pos: NOT_USED, before: NOT_USED, after: NOT_USED) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object

Quick config helper

Parameters:

  • 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


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 82

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



13
14
15
16
# 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_optionsObject



60
61
62
63
64
# 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) ⇒ Object



23
24
25
26
27
28
29
# 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

#selectedObject



31
32
33
34
35
36
37
# 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) ⇒ Object



18
19
20
21
# 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

#to_s(name: true, delimiter: "\n") ⇒ Object



66
67
68
69
70
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 66

def to_s(name: true, delimiter: "\n")
  [selected].flatten.compact.map do |opt|
    name ? opt.name : opt.value
  end.join(delimiter)
end

#valueObject



39
40
41
42
43
44
45
# 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