Class: Ecoportal::API::GraphQL::Base::Page::DataField::Select

Inherits:
DataField
  • Object
show all
Defined in:
lib/ecoportal/api/graphql/base/page/data_field/select.rb

Instance Method Summary collapse

Instance Method Details

#as_inputObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ecoportal/api/graphql/base/page/data_field/select.rb', line 27

def as_input
  return nil unless dirty?

  {
    select: {
      id:      id,
      # rubocop:disable Style/DoubleNegation
      options: (doc['options'] || []).map { |opt| { id: opt['id'], selected: !!opt['selected'] } }
      # rubocop:enable Style/DoubleNegation
    }
  }
end

#clear_selectionObject

Deselect all options.



19
20
21
# File 'lib/ecoportal/api/graphql/base/page/data_field/select.rb', line 19

def clear_selection
  (doc['options'] || []).each { |opt| opt['selected'] = false }
end

#select_option(*id_or_values) ⇒ Object

Select an option by id, name, or value. Deselects all others (single-select). For multi-select fields, call with multiple values.



9
10
11
12
13
14
15
16
# File 'lib/ecoportal/api/graphql/base/page/data_field/select.rb', line 9

def select_option(*id_or_values)
  targets = id_or_values.flatten.map(&:to_s)
  (doc['options'] || []).each do |opt|
    opt['selected'] = targets.include?(opt['id'].to_s) ||
                      targets.include?(opt['name'].to_s) ||
                      targets.include?(opt['value'].to_s)
  end
end

#selected_optionsObject



23
24
25
# File 'lib/ecoportal/api/graphql/base/page/data_field/select.rb', line 23

def selected_options
  (doc['options'] || []).select { |opt| opt['selected'] }
end