lib/ecoportal/api/v2/page/component/selection_field.rb in ecoportal-api-oozes-0.5.5 vs lib/ecoportal/api/v2/page/component/selection_field.rb in ecoportal-api-oozes-0.5.6
- old
+ new
@@ -4,11 +4,36 @@
class Page
class Component
class SelectionField < Page::Component
passthrough :multiple, :other, :other_desc
- class_resolver :selection_option_class, "Ecoportal::API::V2::Page::Component::SelectionOption"
- embeds_multiple :options, klass: :selection_option_class, order_key: :weight
+ #class_resolver :selection_option_class, "Ecoportal::API::V2::Page::Component::SelectionOption"
+ embeds_multiple :options, klass: "Ecoportal::API::V2::Page::Component::SelectionOption", order_key: :weight
+
+ 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
+
+ def selected
+ if multiple
+ options.select {|opt| opt.selected}
+ else
+ options.find {|opt| opt.selected}
+ end
+ end
+
+ def value
+ if multiple
+ selected.map {|opt| opt.value}
+ else
+ selected&.value
+ end
+ end
+
end
end
end
end
end