lib/ecoportal/api/v2/page/component/selection_field.rb in ecoportal-api-v2-0.9.1 vs lib/ecoportal/api/v2/page/component/selection_field.rb in ecoportal-api-v2-0.9.2
- old
+ new
@@ -5,64 +5,63 @@
class Component
class SelectionField < Page::Component
passboolean :multiple, :flat, :other
passthrough :other_desc
passthrough :data_type
+ embeds_many :options, enum_class: "Ecoportal::API::V2::Page::Component::SelectionOptions"
- embeds_many :options, klass: "Ecoportal::API::V2::Page::Component::SelectionOption", order_key: :weight
-
def numeric?
self.data_type == "num"
end
def text?
self.data_type == "txt"
end
def numeric!(&block)
- ordered_options.each {|opt| opt.numeric!(&block)}
+ options.ordered.each {|opt| opt.numeric!(&block)}
self.data_type = "num"
end
def text!(&block)
- ordered_options.each {|opt| opt.text!(&block)}
+ options.ordered.each {|opt| opt.text!(&block)}
self.data_type = "str"
end
def switch_type!(&block)
numeric? ? text!(&block) : numeric!(&block)
end
def select(value_name, by_name: false)
- opt = find_option(value_name, by_name: by_name)
+ 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 unless !opt
end
def deselect(value_name, by_name: false)
- if opt = find_option(value_name, by_name: by_name)
+ if opt = options.find_option(value_name, by_name: by_name)
opt.selected = false
end
end
def selected(by_name: false, by_value: false, value: false, name: false)
case
when by_value
elems = [selected].flatten.compact
- options_hash(elems) do |option|
+ options.hash(elems) do |option|
name ? option.name : option
end
when by_name
elems = [selected].flatten.compact
- options_hash(elems, by_name: true) do |option|
+ options.hash(elems, by_name: true) do |option|
value ? option.value : option
end
else
if multiple
- ordered_options.select {|opt| opt.selected}
+ options.ordered.select {|opt| opt.selected}
else
options.find {|opt| opt.selected}
end
end
end
@@ -89,47 +88,27 @@
def names
[name].flatten.compact
end
- 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
+ # @see Ecoportal::API::V2::Component::SelectionOptions#add
+ def add_option(**kargs, &block)
+ options.add(**kargs, &block)
end
def ordered_options
- options.sort_by.with_index do |option, index|
- [option.weight, index]
- end
+ options.ordered
end
- # @param name [Boolean] whether or not the values of the `Hash` should be the `names`
- # @return [Hash] of **key** _value_ and **value**:
- # 1. _option_ if `name` is `false`
- # 2. _name_ if `name` is `true`
+ # @see Ecoportal::API::V2::Component::SelectionOptions#by_value
def options_by_value(name: false)
- options_hash do |option|
- name ? option.name : option
- end
+ options.by_value(name: name)
end
- # @param value [Boolean] whether or not the values of the `Hash` should be the `values`
- # @return [Hash] of **key** _name_ and **value**:
- # 1. _option_ if `value` is `false`
- # 2. _value_ if `value` is `true`
+ # @see Ecoportal::API::V2::Component::SelectionOptions#by_name
def options_by_name(value: false)
- options_hash(by_name: true) do |option|
- value ? option.value : option
- end
+ options.by_name(value: value)
end
def to_s(value: true, delimiter: "\n")
[selected].flatten.compact.map do |opt|
value ? opt.value : opt.name
@@ -196,41 +175,14 @@
else
add_option(value: val, name: nm)
end
end
end
-
- def fix_option_weights!
- ordered_options.each_with_index do |option, index|
- option.weight = index
- end
- end
-
- def previous_option(value)
- opts = ordered_options
- pos = opts.index(value) - 1
- return if pos < 0
- opts[pos]
- end
-
- def find_option(value_name, by_name: false)
- if by_name
- opt = options.find {|opt| same_string?(opt.name, value_name)}
- else
- opt = options.find {|opt| opt.value == value_name}
- end
- end
-
- def options_hash(elems = ordered_options, by_name: false)
- elems.each_with_object({}) do |option, hash|
- key = by_name ? option.name : option.value
- hash[key] = block_given?? yield(option) : option
- end
- end
end
end
end
end
end
end
+require 'ecoportal/api/v2/page/component/selection_options'
require 'ecoportal/api/v2/page/component/selection_option'