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 included from Common::Content::DoubleModel::Diffable

Common::Content::DoubleModel::Diffable::DIFF_CLASS

Constants included from Common::Content::DoubleModel::Base

Common::Content::DoubleModel::Base::NOT_USED

Instance Attribute Summary

Attributes included from Common::Content::DoubleModel::Parented

#_parent, #_parent_key

Instance Method Summary collapse

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

#attached?, #bindings, #bindings?, #delete!, #forces, get_class, #indexable_label, #move, #multi_section?, new_doc, #ooze, #ref, #ref_backend, #replace, #replace_bindings, #section, #unattach!

Methods included from Common::Content::StringDigest

#hash_label, #indexable_label

Methods inherited from Common::Content::DoubleModel

#initialize, new_uuid

Methods included from Common::Content::DoubleModel::Diffable

#as_update, #dirty?

Methods included from Common::Content::Includer

#include_missing

Constructor Details

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

Instance Method Details

#add_option(**kargs, &block) ⇒ Object

See Also:

  • Component::SelectionOptions#add


96
97
98
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 96

def add_option(**kargs, &block)
  options.add(**kargs, &block)
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>) or update option names
    • :type to define the type
      • :num
      • :str


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 130

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 = %i[flat options type]
      unless (rest = hash_except!(cnf.dup, *supported)).empty?
        unused.push(rest)
      end

      self.flat = cnf[:flat] if cnf.key?(:flat)

      if cnf.key?(:options) && (opts = cnf[:options])
        configure_options opts
      end

      if cnf.key?(:type)
        if cnf[:type] == :str
          text!
        elsif cnf[:type] == :num
          numeric!
        end
      end
    else
      unused.push(cnf)
    end
  end.then do |unused|
    super(*unused)
  end
end

#deselect(value_name, by_name: false) ⇒ Object



47
48
49
50
51
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 47

def deselect(value_name, by_name: false)
  return unless (opt = options.find_option(value_name, by_name: by_name))

  opt.selected = false
end

#empty?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 12

def empty?
  values.empty?
end

#nameObject



83
84
85
86
87
88
89
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 83

def name
  if multiple
    selected.map(&:name)
  else
    selected&.name
  end
end

#namesObject



91
92
93
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 91

def names
  [name].flatten.compact
end

#numeric!(&block) ⇒ Object



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

def numeric!(&block)
  options.ordered.each {|opt| opt.numeric!(&block)}
  self.data_type = 'num'
end

#numeric?Boolean

Returns:

  • (Boolean)


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

def numeric?
  data_type == 'num'
end

#options_by_name(value: false) ⇒ Object

See Also:

  • Component::SelectionOptions#by_name


110
111
112
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 110

def options_by_name(value: false)
  options.by_name(value: value)
end

#options_by_value(name: false) ⇒ Object

See Also:

  • Component::SelectionOptions#by_value


105
106
107
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 105

def options_by_value(name: false)
  options.by_value(name: name)
end

#ordered_optionsObject



100
101
102
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 100

def ordered_options
  options.ordered
end

#select(value_name, by_name: false) ⇒ Object



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

def select(value_name, by_name: false)
  opt = options.find_option(value_name, by_name: by_name)
  sel = selected
  return true if !multiple && opt == sel

  sel.selected = false if !multiple && sel
  opt.selected = true  if opt
end

#selected(by_name: false, by_value: false, value: false, name: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 53

def selected(by_name: false, by_value: false, value: false, name: false)
  if by_value
    elems = [selected].flatten.compact
    options.hash(elems) do |option|
      name ? option.name : option
    end
  elsif by_name
    elems = [selected].flatten.compact
    options.hash(elems, by_name: true) do |option|
      value ? option.value : option
    end
  elsif multiple
    options.ordered.select(&:selected)
  else
    options.find(&:selected)
  end
end

#switch_type!(&block) ⇒ Object



34
35
36
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 34

def switch_type!(&block)
  numeric? ? text!(&block) : numeric!(&block)
end

#text!(&block) ⇒ Object



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

def text!(&block)
  options.ordered.each {|opt| opt.text!(&block)}
  self.data_type = 'str'
end

#text?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 20

def text?
  data_type == 'txt'
end

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



114
115
116
117
118
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 114

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

#valueObject



71
72
73
74
75
76
77
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 71

def value
  if multiple
    selected.map(&:value)
  else
    selected&.value
  end
end

#valuesObject



79
80
81
# File 'lib/ecoportal/api/v2/page/component/selection_field.rb', line 79

def values
  [value].flatten.compact
end