Sha256: 8bb76221488e99f2538d1a6041c531772c098fd0ce4fac11cc1e08d5795d0122

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module Ecoportal
  module API
    class V2
      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: "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
end

require 'ecoportal/api/v2/page/component/selection_option'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ecoportal-api-oozes-0.5.7 lib/ecoportal/api/v2/page/component/selection_field.rb
ecoportal-api-oozes-0.5.6 lib/ecoportal/api/v2/page/component/selection_field.rb